求解過程:
1、建立微分方程求解函式
function f = func(x,y)
f=【y(2);x+y(1)】;
2、根據尤拉法的遞推公式建立求解函式,用於求解微分方程的數值解
3、確定初值,求解,繪圖
x0=0;xf=1;
y0=[1,2];
h=0.1;
[x,y]=euler(@(x,y)func(x,y),x0,y0,xf,h); %尤拉法
plot(x,y(1,:)),hold on
[x,y]=Euler_Cauchy(@(x,y)func(x,y),x0,y0,xf,h); %尤拉-柯西法(改進的尤拉法)
plot(x,y(1,:),"o"),hold on
y =2*exp(x) - exp(-x) - x; %解析解
plot(x,y,"*-")
legend("尤拉法","尤拉-柯西法","精確值")
xlabel("x"),ylabel("y(x)");
4、執行後的圖形https://pic4.zhimg.com/50/v2-7a268cae00b27e99b53d9d976b65fd51_hd.gif" data-caption="" data-size="normal
https://pic1.zhimg.com/50/v2-f35e2a6f6d4eff8197981da9076b31ad_hd.jpg" data-caption="" data-size="normal
求解過程:
1、建立微分方程求解函式
function f = func(x,y)
f=【y(2);x+y(1)】;
2、根據尤拉法的遞推公式建立求解函式,用於求解微分方程的數值解
3、確定初值,求解,繪圖
x0=0;xf=1;
y0=[1,2];
h=0.1;
[x,y]=euler(@(x,y)func(x,y),x0,y0,xf,h); %尤拉法
plot(x,y(1,:)),hold on
[x,y]=Euler_Cauchy(@(x,y)func(x,y),x0,y0,xf,h); %尤拉-柯西法(改進的尤拉法)
plot(x,y(1,:),"o"),hold on
y =2*exp(x) - exp(-x) - x; %解析解
plot(x,y,"*-")
legend("尤拉法","尤拉-柯西法","精確值")
xlabel("x"),ylabel("y(x)");
4、執行後的圖形https://pic4.zhimg.com/50/v2-7a268cae00b27e99b53d9d976b65fd51_hd.gif" data-caption="" data-size="normal
https://pic1.zhimg.com/50/v2-f35e2a6f6d4eff8197981da9076b31ad_hd.jpg" data-caption="" data-size="normal