Trying to solve and plot the non-linear pendulum equation
Show older comments
Hi! I have the d^2/dt^2 + g/L(sinθ)=0 2nd order D.E. (Simple Pendulum) which I have seperated to two 1st order D.E.
y(1) = dθ/dt = ω = u_θ , y(2) = dω/dt = -b^2*sinθ (where b = sqrt(g/L))
I want to use ode45 to solve these equations.
I tried following the link: https://www.mathworks.com/help/matlab/ref/ode45.html but I got errors on my code.
m-file
function dydt = spendn(t,y)
dydt = [u; -sinu];
In command window:
[t,y] = ode45(@spendn,[0 7],[0; 1.57]);
I am doing something wrong here, pls help.
1 Comment
Please post the complete error message.
The conversion from
y(1) = dθ/dt = ω = u_θ
y(2) = dω/dt = -b^2*sinθ (where b = sqrt(g/L))
to
dydt = [u; -sinu];
seems to be too rough. Neither "u" not "sinu" are defined.
Answers (1)
Star Strider
on 16 Nov 2017
Try this:
dydt = [u; -sin(u)];
4 Comments
Star Strider
on 16 Nov 2017
Alexandros’s “Answer” moved here:
This is the error:
Undefined function or variable 'u'.
Error in spendn (line 9)
dydt = [u; -sin(u)];
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Star Strider
on 16 Nov 2017
The complete correction is:
function dudt = spendn(t,u)
dydt = [u; -sin(u)];
end
Alexandros
on 17 Nov 2017
Star Strider
on 17 Nov 2017
Use this:
function dudt = spendn(t,u)
dydt = [u(2); -sin(u(1))];
end
or this:
spendn = @(t,u) [u(2); -sin(u(1))];
Either of these will work.
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!