Solving ODE using Euler Method and 4th Order Runge Kutta Method
이전 댓글 표시
I am trying to learn how to solve differential equations provided the intial conditions, I have already made the matlab code for both the euler and runge kutta method as follows:
%Euler method
function y=elrl(t,x,n,h)
%t:Time
%x:Variables
%n:Number of variables
%h:Step
d=f(t,x);
for i = 1:n
p(i)=x(i)+h*d(i);
end
d=f(t+h,p);
for i = 1:n
q(i)=x(i)+h*d(i);
y(i)=0.5*(p(i)+q(i));
end
%Runge Kutta method
function y=rktl(t,x,n,h)
%t:Time
%x:Variables
%n:Number of variables
%h:Step
k0=f(t,x);
k1=f(t+0.5*h,x+k0*0.5*h);
k2=f(t+0.5*h,x+k1*0.5*h);
k3=f(t+h,x+k2*h);
y=x+h/6*(k0+2*k1+2*k2+k3);
My problem is that I don't know how to define the function using the initial conditions, I have the following system, can anybody help ?

댓글 수: 9
Torsten
2021년 12월 21일
What are the x-variables in your code ?
Adnane Ait Zidane
2021년 12월 21일
James Tursa
2021년 12월 21일
편집: James Tursa
2021년 12월 21일
None of the methods is coded correctly. In the first place, you have three 1st order differential equations, so you will need a 3-element state vector at each time point to hold the solution. But you have coded a 1-element scalar as your solution at each time step. Secondly, your Euler schemes apparently attempt to use the same incoming state for all steps when they should be using a calculated state iteravely at each step. And it looks like your RK4 scheme only takes one step. Can you show us the code you have written for your f( ) function? I suspect that has errors also.
Torsten
2021년 12월 21일
Yes, that's the explicit Euler method for your system.
But you should replace the plot command by
plot (transpose(0.01:0.01:10),p)
Do you see where the initial conditions are set ?
Adnane Ait Zidane
2021년 12월 21일
James Tursa
2021년 12월 21일
These two lines are not quite correct:
y0=y0+dt*y1;
y1=y1+dt*(-y0);
The problem is that you are using the current y1 to propagate the y0 variable (which is correct), but you are using the next y0 to propagate the y1 variable (which is incorrect). You should do something like this instead:
y0save = y0;
y0=y0+dt*y1;
y1=y1+dt*(-y0save);
That way you are using the current states on the right hand side consistently.
Adnane Ait Zidane
2021년 12월 21일
Adnane Ait Zidane
2021년 12월 21일
채택된 답변
추가 답변 (1개)
function main
tstart = 0.0;
tend = 10.0;
h = 0.01;
T = (tstart:h:tend).';
Y0 = [-1 0 1];
f = @(t,y) [y(2) -y(1) -y(3)];
Y = euler_explicit(f,T,Y0); % Euler explicit solution
plot(T,Y)
hold on
Y = runge_kutta_RK4(f,T,Y0);
plot(T,Y) % Runge Kutta solution
%hold on
%plot(T,[-cos(T) sin(T) exp(-T)]) % Analytical solution
end
function Y = euler_explicit(f,T,Y0)
N = numel(T);
n = numel(Y0);
Y = zeros(N,n);
Y(1,:) = Y0;
for i = 2:N
t = T(i-1);
y = Y(i-1,:);
h = T(i) - T(i-1);
Y(i,:) = y + h*f(t,y);
end
end
function Y = runge_kutta_RK4(f,T,Y0)
N = numel(T);
n = numel(Y0);
Y = zeros(N,n);
Y(1,:) = Y0;
for i = 2:N
t = T(i-1);
y = Y(i-1,:);
h = T(i) - T(i-1);
k0 = f(t,y);
k1 = f(t+0.5*h,y+k0*0.5*h);
k2 = f(t+0.5*h,y+k1*0.5*h);
k3 = f(t+h,y+k2*h);
Y(i,:) = y + h/6*(k0+2*k1+2*k2+k3);
end
end
You must improve your programming skills.
So I hope you do not only copy the code and submit it as your homework, but also try to understand what's going on here.
댓글 수: 2
Adnane Ait Zidane
2021년 12월 21일
Torsten
2021년 12월 21일
The main principle you have to follow is that the variables you use in a certain line of a function must be defined or calculated in the lines before.
So you should first consider what you want the function to supply as output, what variables are known (input to the function) and what variables you need to calculate from these known quantities to produce the desired output.
카테고리
도움말 센터 및 File Exchange에서 Runge Kutta Methods에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
