Write a MATLAB implementation that applies the classical fourth order RK method

2 次查看(过去 30 天)
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 个评论
Alper Sahin
Alper Sahin 2021-12-27
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 length
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
for i=1:n
k0 = f2(t(i),u(i),v(i));
l0 = f1(t(i),u(i),v(i));
k1 = f2(t(i)+0.5*h,u(i)+k0*0.5*h,v(i)+k0*0.5*h);
l1 = f1(t(i)+0.5*h,u(i)+k0*0.5*h,v(i)+k0*0.5*h);
k2 = f2(t(i)+0.5*h,u(i)+k1*0.5*h,v(i)+k0*0.5*h);
l2 = f1(t(i)+0.5*h,u(i)+k1*0.5*h,v(i)+k0*0.5*h);
k3 = f2(t(i)+h,u(i)+k2*h,v(i)+k2*h);
l3 = f1(t(i)+h,u(i)+k2*h,v(i)+k2*h);
u(i+1) = u(i) + h/6*(k0+2*k1+2*k2+k3);
v(i+1) = v(i) + h/6*(l0+2*l1+2*l2+l3);
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 edited it this way, do you think it's correct? @Torsten
Torsten
Torsten 2021-12-27
编辑:Torsten 2021-12-27
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.

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2021-12-27
编辑:Torsten 2021-12-27
Here is your corrected code:
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;
f_exact = @(t)exp(-2*t)+t;
a=0;% initial point of t
b=1;% final point of t
N=[20,40,80,160]; % step length
for s = 1:numel(N)
n = N(s);
y=zeros(1,n+1);
u=zeros(1,n+1);
v=zeros(1,n+1);
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
for i=1:n
k0 = f1(t(i),u(i),v(i));
l0 = f2(t(i),u(i),v(i));
k1 = f1(t(i)+0.5*h,u(i)+k0*0.5*h,v(i)+l0*0.5*h);
l1 = f2(t(i)+0.5*h,u(i)+k0*0.5*h,v(i)+l0*0.5*h);
k2 = f1(t(i)+0.5*h,u(i)+k1*0.5*h,v(i)+l1*0.5*h);
l2 = f2(t(i)+0.5*h,u(i)+k1*0.5*h,v(i)+l1*0.5*h);
k3 = f1(t(i)+h,u(i)+k2*h,v(i)+l2*h);
l3 = f2(t(i)+h,u(i)+k2*h,v(i)+l2*h);
u(i+1) = u(i) + h/6*(k0+2*k1+2*k2+k3);
v(i+1) = v(i) + h/6*(l0+2*l1+2*l2+l3);
end
err(s)=max(abs(u-f_exact(t)))
end
But keep in mind how many lines of code you had to write if your ODE system was bigger than just two equations. And the alphabet does not have an unlimited number of letters :-)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by