Write a MATLAB implementation that applies the classical fourth order RK method
Mostrar comentarios más antiguos
clc
clear
B=4;
f1=@(t,u,v)v;
f2=@(t,u,v)5*exp(-2*t)+2*exp(-(B+2)*t)+exp(-(B*t))+t-v*exp(-B*t)-u;
a=0; % intial point of t
b=1;% final point of t
n=20; % step legth
s=1;
y=zeros(1,n+1);
while(n<=160)
h=(b-a)/n; % number of intervals
t=a:h:b; % t runing from a to b
u(1)=1; % initial value of Q
v(1)=-1;% initial value of I
% Rk2 method for two variable
for i=1:n
k1=h*f1(t(i),u(i),v(i));
k2=h*f2(t(i),u(i),v(i));
l1=h*f1(t(i)+h,u(i)+k1,v(i)+k2);
l2=h*f2(t(i)+h,u(i)+k1,v(i)+k2);
u(i+1)=u(i)+(1/2)*(k1+l1);
v(i+1)=v(i)+(1/2)*(k2+l2);
end
for i=1:n+1
y(1,i)=exp(-2*t(i))+t(i);
end
err(s)=max(abs(u-y));
N(s)=n;
n=2*n;
s=s+1;
end
varNames = {'N','Relative Error'};
table(N',err','VariableNames',varNames)

I'm trying to make the code I wrote above similar to the solution in this question, but I failed, can you help?
6 comentarios
Modify the Runge-Kutta code supplied here according to your needs:
Alper Sahin
el 27 de Dic. de 2021
Alper Sahin
el 27 de Dic. de 2021
Alper Sahin
el 27 de Dic. de 2021
If your task were to solve 100 ODEs at a time, would you code the Runge-Kutta scheme as above by using 100 lines of code to evaluate the functions, 100 lines of code per Runge-Kutta coefficient k0,k1,k2,... and 100 lines of code to update the 100 functions ? Use arrays instead.
Take a look at the code I linked to on how to do this.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!