Azzera filtri
Azzera filtri

Initial Value Problem in matlab

1 visualizzazione (ultimi 30 giorni)
Nick Haufler
Nick Haufler il 22 Apr 2018
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')

Risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by