Euler Method in MATLAB: Solving a Double Pendulum

Click For Summary
SUMMARY

The discussion focuses on implementing the Euler method for solving a double pendulum system modeled by a set of four first-order differential equations in MATLAB. The user has successfully utilized MATLAB's ode45 and ode23s functions but encounters challenges with the Euler method due to the need for a custom function and proper loop implementation. Key insights include the necessity of defining a step size 'h' and the structure of the iterative loop for calculating the next values of the dependent variables. The user aims to graph the angles of the pendulum branches over time.

PREREQUISITES
  • Understanding of first-order differential equations
  • Familiarity with MATLAB programming and syntax
  • Knowledge of numerical methods, specifically the Euler method
  • Ability to create and use custom functions in MATLAB
NEXT STEPS
  • Learn how to implement MATLAB's ode45 and ode23s for solving differential equations
  • Research the specifics of the Euler method, including error analysis and step size selection
  • Explore MATLAB's plotting functions to visualize results effectively
  • Study examples of custom function creation in MATLAB for numerical methods
USEFUL FOR

Students and professionals in engineering, physics, or applied mathematics who are working on dynamic systems modeling, particularly those interested in numerical methods for solving differential equations in MATLAB.

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

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K