I can't find where the mistake is. Could anyone help me with my code?

1 vue (au cours des 30 derniers jours)
DS
DS le 19 Fév 2019
Modifié(e) : Stephan le 19 Fév 2019
Differential equation of simple pendulum
d^2θ /dt^2 + (g/L)*sin(θ )=0
Initial conditions: θ(0) = θ0 , θ'(0) = 0
The angular displacement θ0 = 30 degrees, length of the pendulum L = 0.6 m and gravity g = 9.82 m/s^2.
This is the script I use to solve the differential equation with ode45 and to plot the solution in degrees but I got a wrong plotting.
theta0=30;
L=0.6;
g=9.82;
t=0:100;
[t,theta30]=ode45(@(t,THETA) fun(t,THETA,g,L),t,[theta0*pi/180 0]);
plot(t,theta30(:,1)*180/pi,'--b')
grid on
axis ([0 t(end) -30 30])
xlabel('Time')
ylabel('Angle')
function dthetadt=fun(t,THETA,g,L)
dthetadt=zeros(2,1);
dthetadt(1)=THETA(2);
dthetadt(2)=(-g/L)*sin(THETA(1));
end

Réponse acceptée

Stephan
Stephan le 19 Fév 2019
Modifié(e) : Stephan le 19 Fév 2019
Hi,
the way you define t is not useful. Use:
t = [0 100]
What you do is corresponding to a sample rate of 1 second. This lets the solution appear wrong, due to the ratio of frequency of your pendulum and the 1 second sample rate for plotting.
Best regards
Stephan

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by