[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.用预报-校正法解下面常微分方程的初值问题。
2.用r-k方法求解初值问题:
import math as mh import matplotlib.pyplot as plt def g(x): ni=1/((x**2)+1)
return ni def no(jk1,h2):#question one with prediction correction method global
y def f(x,y): xv=-2*x*(y**2) return xv x1=jk1;x2=h2;h=0.01;y0=1
t=(x2-x1)/h;hi=int(t) for i in range(hi): n1=f(x1+i*h,y0); y1=y0+h*n1;
n2=f(x1+(i+1)*h,y1); y=y0+h/2*(n1+n2) y0=y return y x0=0#x的初值点 x1=2#想求的X值
r=1/5;y=0;no(x0,x1)
print('问题1:(保留6位小数)\n求解X={}时,y值\n预报校正法求解:{:.6};步长为0.01'.format(x1,y))
print('准确求解微分方程:{:.6}'.format(r))
print('预报和准确值的相对误差为:{:.6}%,绝对误差为:{:.6}'.format(abs(y-r)/r*100,abs(y-r)))
x1=[0];y1=[1];y2=[1] for i in range(300): y1.append(g(i*0.01))
x1.append(i*0.01) y2.append(no(x0,0.01+i*0.01))
plt.plot(x1,y1,c='r',label='Value') plt.plot(x1,y2,c='k',label='prediction')
plt.xlabel('X');plt.ylabel('Y');plt.title('P-M trace') plt.legend() plt.show()
def f(x,y):#question second with R-K.way xv=mh.sin(x)+mh.cos(y) return xv
c1=[];c2=[] for j in range(1000): x1=0;x2=10;h=0.01;y0=0;x0=0
t=(x2-x1)/h;hi=int(t)-j for i in range(hi): n1=f(x0,y0) n2=f(x0+h/2,y0+n1*h/2)
n3=f(x0+h/2,y0+n2*h/2) n4=f(x0+h,y0+n3*h) y=y0+h/6*(n1+2*n2+2*n3+n4) y0=y
x0=x0+h c2.append(y);c1.append(hi*h)
print('问题2:(保留6位小数)\n求解X={}时,y值\nR-K求解:{:.6};步长为0.01'.format(c1[900],c2[900]))
plt.plot(c1,c2,c='k') plt.xlabel('X');plt.ylabel('Y');plt.title('R-K trace')
plt.show()
结果:
问题1:(保留6位小数) 求解X=2时,y值 预报校正法求解:0.200007;步长为0.01 准确求解微分方程:0.2
预报和准确值的相对误差为:0.00325958%,绝对误差为:6.51915e-06 问题2:(保留6位小数)
求解X=1.2307313059275027时,y值 R-K求解:1.23073;步长为0.01