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

1 次查看(过去 30 天)
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

采纳的回答

Stephan
Stephan 2019-2-19
编辑:Stephan 2019-2-19
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by