Equation of motion of Non linear pendulum

15 visualizzazioni (ultimi 30 giorni)
tspan=[0,2*pi];
u0=[pi/4;0];
[t,u]=ode45(@pendulum,tspan,u0,[]);
plot(t,u(:,1),'b-', 'LineWidth', 2)
xlabel('time')
ylabel('angle')
function zdot = pend (t,z)
wsq = 1.56; % specify a value of w"2
t = time;
z = [z (l) ; z ( 2)] = [theta; thetadot]
zdot = [z (2) ; -wsq* sin (z(l))];
end
This doesn't seem to be working. I'm trying to plot the discplacement versus time and velocity versus time. Any idea as to what the problem mightbe?
I got this from Rudra pratap. I've attached a link to the book, it's on page 159 (173 by docs)

Risposta accettata

Bjorn Gustavsson
Bjorn Gustavsson il 4 Nov 2020
Note that the function you use for the input-argument to ode45 is pendulum while the function you implemented for the pendulum has the name pend. They should be the same. When that's fixed there seems to be no major problems with your code:
u0=[pi/4;0];
pend = @(t,z) [z(2);-1.56*sin(z(1))];
figure
tspan=[0,12*pi]; % just a slightly longer time-span
[t,u]=ode45(pend,tspan,u0,[]);
plot(t,u)
HTH
  3 Commenti
Bjorn Gustavsson
Bjorn Gustavsson il 4 Nov 2020
Ok, that is because your script is named pend.m and then matlab doesn't like the naming-confusion with a variable with that name in the script. You should be fine if you rename the variable to someting like pendODE for example.
HTH

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by