Solving 2nd order ODE with various methods

조회 수: 5 (최근 30일)
Steven
Steven 2015년 10월 2일
댓글: Steven 2015년 10월 2일
Okay the problem is of a free swinging pendulum with dampening which is modelled using the following equation:
Damping coefficient: c=1 s−1 Mass: m=1 kg Gravity: g=9.81 ms−1 Link length: l=0.5 m
We know θ(0)=90° and θ′(0)=0, solve this equation from t = 0 to t = 10 with a time interval of 0.01s
The equation is:
d2θ/dt2+(c/m)*(dθ/dt)+(g/l)*sin (θ)=0
So we need to use Euler,Heun and 4th order Runge-Kutta method
2. Relevant equations
3. The attempt at a solution
Okay so my idea was to create a function as so:
function xdot=pendemo(t,x)
% PENDEMO Pendulum ODE derivative evaluation
xdot(1,1) = x(2,1);
xdot(2,1) = -1/(1*1)*x(2,1) - 9.81/1*sin(x(1,1));
% End of pendemo.m
and than an m.file giving the above information:
xphi = [pi/2;0];
tphi = 0; 5 %start time
tfin = 10; %end time
[t,x] = ode45('pendemo',[tphi tfin],xphi);
plot(t,x(:,1))
The only thing is how do I implement a euler/heun method? What is a 4th order Runga Kata??
thanks

답변 (1개)

@Johannes
@Johannes 2015년 10월 2일
Hello,
in your example you are using ODE45 to solve your ode. ODE45 is a solver which is using the method of Runge Kutta (RKF45) to solve ode's. This means you are using already a Runge Kutta solver of the order 4. Explicit Euler and Heun are to be implemented by you as functions. For example use the equations form wikipedia.
https://en.wikipedia.org/wiki/Euler_method
https://en.wikipedia.org/wiki/Heun%27s_method
Regards, John
  댓글 수: 1
Steven
Steven 2015년 10월 2일
Okay this is my code for euler:
function [t,x] = euler(dydt,tspan,y0,h)
t = (tspan(1):h:tspan(2));
n=length(t);
y = y0*ones(n,1);
for i = 1:n-1
y(i+1)= y(i) + dydt(t(i), y(i))*(t(i+1)-t(i));
end
How can i apply it to my ODE?

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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by