Simple ODE 2nd order with ODE45 - Where's my problem?

Hello everyone,
Im trying to solve this simple problem since 2 days and I can't seem to figure it out.
I have a simple 2nd order ODE (See Image) Lets assume theta = phi.
In the first case, c = 0. So, the eqn is:
phi'' = -w^2 * sin(phi).
My code is:
function second_oder_ode_2a
l=0.5;
g=9.81;
w=sqrt(g/l)
t=0:0.001:500; % time scale
initial_phi = 45;
initial_dphidt = (1/pi)*180;
[t,phi]=ode45( @rhs, t, [initial_phi initial_dphidt]);
plot(t,phi(:,1));
xlabel('t'); ylabel('phi');
function dphidt=rhs(t,phi,w)
dphidt_1 = phi(2);
dphidt_2 = -w^.2 * sin(phi(1));
dphidt=[dphidt_1; dphidt_2];
I really have no idea where the problem is. Errors occur at:
• ODE45 Function • dphidt = -w^.2 * sin(phi(1));
Any help is appreciated.
Greetings

Answers (1)

[t,phi]=ode45( @(t,y)rhs(t,y,w), t, [initial_phi initial_dphidt]);
And convert degress in radians in your initial conditions.
Best wishes
Torsten.

3 Comments

Thanks for this quick answer, appreciate it! The plotting works and the solution seems reasonable, however, the values should be different I guess.
My Code now looks like this.
function second_oder_ode_2a
l=0.5;
g=9.81;
w=sqrt(g/l)
t=0:0.001:20; % time scale
initial_phi = pi/4;
initial_dphidt = 1;
[t,phi]=ode45( @(t,phi)rhs(t,phi,w), t, [initial_phi initial_dphidt]);
plot(t,phi(:,1));
xlabel('t'); ylabel('phi');
function dphidt=rhs(t,phi,w)
dphidt_1 = phi(2);
dphidt_2 = -w^.2 * sin(phi(1));
dphidt=[dphidt_1; dphidt_2];
end
end
The Plot now looks like this:
Whereas the solution shows it should look different:
You can see that the first minimum for my plot is @ ~4 and it should be at ~1. Can you help me figure out what went wrong?
Again, thanks. Really kind of you.
I don't know which parameters are taken in your book, but the reason for the discrepancy can only be the value of "w".
Best wishes
Torsten.
Thank you very much. The problem was: I had to remove the dot after the operator (w^.2)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 8 Jun 2017

Commented:

on 9 Jun 2017

Community Treasure Hunt

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

Start Hunting!