Using Euler method to solve second order ODE

조회 수: 14 (최근 30일)
Yuval Levy
Yuval Levy 2022년 7월 19일
편집: Torsten 2022년 7월 20일
Hello,
I'm trying to write a program that usese Euler method to solve second order ODE. (represnts the movment of a package hanging from the roof with a spring).
I eventually will need to solve a more complex one, but as long as the simple one doesn't work, I have no reason to continue.
My ODE is: y'' = -25*y + 440.19 ;
For a reason I don't understand, my plot is not converging with the calculated solution for the ODE.
The code and the plot I'm getting are in the pictures. (the expected result was done with Runge-Kutta but since I can't manege to use it so solve a second order ODE I'm trying to understand the Euler method).
Zp_dotaim = y'' ; Zp_dot = y' ; Zp = y
Initial values : y'(0) = 0 ; y(0) = 18 ;
Thank you!
Zp_dot = zeros(1,1000) ; % prealocating vectors [m/sec]
Zp = zeros(1,1000) ; % prealocating vectors [m]
Zp(1) = 18 ; % initial condition
t = linspace(1,100,1000) ; % creating time vector [sec]
h = 0.01 ; % time step
for i = 1:999
% Zp_dotaim =@(Zp) 25*((2/(20-Zp))-1)*(Zp-20)-9.81 ;
Zp_dotaim =@(Zp) -25*Zp + 440.19 ;
Zp_dot(i+1) = Zp_dot(i) + h*Zp_dotaim(Zp(i)) ;
Zp(i+1) = Zp(i) + h*Zp_dot(i) ;
end
exact = (0.3924*cos(5*t)+17.6076);
plot(t,Zp,'r',t,exact,'b--') ; title('Zp(t)') ; grid on ;
xlabel('t [sec]') ; ylabel('Zp(t) [m]') ;

채택된 답변

Sam Chak
Sam Chak 2022년 7월 19일
편집: Sam Chak 2022년 7월 19일
Some minor issues. If you use Euler's method and you want to simulate for a relatively long duration, then you need to make the time step smaller. Also, the time vector begins from , because the initial value from the exact solution (cosine) is .
Zp_dot = zeros(1, 100001); % prealocating vectors [m/sec]
Zp = zeros(1, 100001); % prealocating vectors [m]
Zp(1) = 18; % initial condition
t = linspace(0, 10, 100001); % creating time vector [sec]
h = 1e-4; % time step
for i = 1:100000
Zp_dotaim = @(Zp) - 25*Zp + 440.19 ;
Zp_dot(i+1) = Zp_dot(i) + h*Zp_dotaim(Zp(i)) ;
Zp(i+1) = Zp(i) + h*Zp_dot(i) ;
end
exact = (0.3924*cos(5*t)+17.6076);
plot(t, Zp, 'r', t, exact,'b--');
title('Zp(t)');
grid on;
xlabel('t [sec]');
ylabel('Zp(t) [m]') ;
  댓글 수: 6
Yuval Levy
Yuval Levy 2022년 7월 20일
@Torsten Thank's alot! it worked. still don't really know how to figure out the h values and vector sizes, but you answed my question.
Thank you again.
Torsten
Torsten 2022년 7월 20일
편집: Torsten 2022년 7월 20일
still don't really know how to figure out the h values and vector sizes, but you answed my question.
I answered your question about the h value :-).
And the sizes of the vectors must be 1 x [(tend-tstart)/h + 1] in order that the solution and derivatives for all requested times can be saved within them.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Tom Brenner
Tom Brenner 2022년 7월 19일
You can't solve a second order differential equation with a single initial condition. You must have two. In this case, you assume that the first value of Zp_dot is zero (the vector was initialized with zeros) and add to this zero the approximate value of h times the second derivative.
You should determine how the exact solution 0.3924*cos(5*t)+17.6076 was arrived at (i.e., what the two initial conditions should be), and then correct your code.
  댓글 수: 1
Yuval Levy
Yuval Levy 2022년 7월 19일
편집: Yuval Levy 2022년 7월 19일
Thanks for you answer Tom.
You are right, my bad. The initial value for Zp_dot is actually zero, I just forgot to write it in the explenation.
So unfortunately, that's not the origin of my problem... I have edited the post.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by