Euler Method in MATLAB: Solving a Double Pendulum

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 24K views
blue-steel
Messages
6
Reaction score
0
Hi all

Im working on a systems dynamics problem which involves modelling a double pendulum, a chaotic system

I have a non linear system of 4 1st order differential equations which I need to solve using numerical methods in MATLAB

The methods are ode45, ode23s and euler method

Ive been able to model using the first 2 but am having problems with the euler method as this involves the variable 'h' in the euler algorithm and creating a unique function (as opposed to built in ones in matlab) and I am not sure how to use loops and feval syntax and such

So for a system such as

x1' = f(x1,x2,x3,x4)
x2' = f(x1,x2,x3,x4)
x3' = f(x1,x2,x3,x4)
x4' = f(x1,x2,x3,x4)

How do you solve in MATLAB using the euler algorithm, y(i+1) = y(i) + hf(y,i)
my ultimate goal is to solve and graph points x1 and x3 with time (as these represent angles of the 2 pendulum branches)

Id appreciate any ideas on how to start with coding the euler algorithm in MATLAB and how apply user made functions, I don't know where to start...
 
Physics news on Phys.org
This question is best answered by MATLAB:

https://www.mathworks.com/matlabcentral/answers/278300-matlab-code-help-on-euler-s-method
[CODE lang="matlab" title="Example Euler Method implemented in MATLAB" highlight="4,5,12,13"]% Euler's Method

% Initial conditions and setup
h = (enter your step size here); % step size
x = (enter the starting value of x here):h:(enter the ending value of x here); % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = (enter the starting value of y here); % the initial y value
n = numel(y); % the number of y values

% The loop to solve the DE
for i=1:n-1
f = the expression for y' in your DE
y(i+1) = y(i) + h * f;
end

plot(x,y);
[/CODE]

First decide on the range of x values and then on how fine a step between them.

In this example, you could define x as being from 0:10 and a step size of 0.1:

x=[0:0.1:10]

this will give you a 100 points to compute. The finer step sizes will give you more points but taking correspondingly longer to compute and in this MATLAB example will take more memory for the x and y arrays.

Once run, you will see your results in the generated plot.

Euler is good for monotonically increasing or decreasing solutions. It doesn't work so well for periodic solutions where injected error makes the integration go astray. Basically global error is estimated as the square of the step size so here with h=0.1 then global erro at each iteration is 0.01 or 1%.

Here's the wikipedia article on it:

https://en.wikipedia.org/wiki/Euler_method
Here's some more resources to look at:

http://people.math.sfu.ca/~ralfw/math467w03/matlab/euler_matlab.pdf
http://mandal.faculty.ku.edu/math320/euler.html
and some MATLAB videos on the topic:

https://www.mathworks.com/videos/solving-odes-in-matlab-1-euler-ode1-117526.html
 
  • Like
Likes   Reactions: Wrichik Basu