Initial Value Problem in matlab
2 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187992/image.jpeg)
I need help with the question posted:
This is what I have so far:
clc
clear all
f=inline('y*t^3-1.5*y','t','y');
t0=0;
y0=1;
tf=2;
tspan = [t0 tf];
[t,y]=ode45(f,tspan,y0);
hold on
plot (t,y,'r')
clear t
h=0.5;
t(1)=t0;
euler5(1)=y0;
for i=1:4
t(i+1)=t(i)+h;
euler5(i+1)=euler5(i)+h*f(t(i),euler5(i));
end;
plot(t,euler5,'g')
clear t
h=0.25;
t(1)=t0;
euler25(1)=y0;
for i=1:8
t(i+1)=t(i)+h;
euler25(i+1)=euler25(i)+h*f(t(i),euler25(i));
end;
plot(t,euler25,'b')
clear t
h = 0.5;
midpoint(1) = y0;
t(1) = t0;
for i = 1 : 4
t(i+1) = t(i) + h;
z = midpoint(i) + h/2* f(t(i),midpoint(i));
midpoint(i+1) =midpoint(i) + h * f(t(i)+h/2,z);
end;
plot(t,midpoint,'y')
% Displays title information
h=0.5 ;
ta(1)=t0 ;
ya(1)=y0 ;
for i=1:4
% Adding Step Size
ta(i+1)=ta(i)+h ;
% Calculating k1, k2, k3, and k4
k1 = f(ta(i),ya(i)) ;
k2 = f(ta(i)+0.5*h,ya(i)+0.5*k1*h) ;
k3 = f(ta(i)+0.5*h,ya(i)+0.5*k2*h) ;
k4 = f(ta(i)+h,ya(i)+k3*h) ;
% Using 4th Order Runge-Kutta formula
ya(i+1)=ya(i)+1/6*(k1+2*k2+2*k3+k4)*h ;
end
plot(t,ya,'c')
legend('Exact','Eluer ,width 0.5','Euler width 0.25','Midpoint','RK fourth Order')
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!