MATLAB Answers

ode45 fail with pendulum

1 view (last 30 days)
Bobby Fischer
Bobby Fischer on 13 Jan 2021
Edited: Mischa Kim on 13 Jan 2021
Hello. I have proof that in my machine the pendulum goes all the way around, contradicting reality. Any thoughts?
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.8.0.1323502 (R2020a)
MATLAB License Number: 40875211
Operating System: Microsoft Windows 8.1 Version 6.3 (Build 9600)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.8 (R2020a)
Simulink Version 10.1 (R2020a)
Optimization Toolbox Version 8.5 (R2020a)
Statistics and Machine Learning Toolbox Version 11.7 (R2020a)
Symbolic Math Toolbox
function pendule2
[~,y]=ode45(@fun,0:0.05:40,[pi-0.1,0]);
figure(1)
close(1)
figure(1)
[n,~]=size(y);
t1=0:0.05:2*pi;
x1=cos(t1);
y1=1+sin(t1);
for k=1:n
hold on
axis equal
axis([-1 1 0 2])
plot(x1,y1,'k--')
plot(sin(y(k,1)),1-cos(y(k,1)),...
'bo','MarkerSize',5,'MarkerFaceColor','b')
plot([0 sin(y(k,1))], [1 1-cos(y(k,1))],'b')
pause(0.01)
clf
end
hold on
axis equal
axis([-1 1 0 2])
plot(x1,y1,'k--')
plot(sin(y(n,1)),1-cos(y(n,1)),...
'bo','MarkerSize',5,'MarkerFaceColor','b')
plot([0 sin(y(n,1))], [1 1-cos(y(n,1))],'b')
text(0.85,0.1,'end')
function [dydt]=fun(~,y)
dydt=[y(2); -sin(y(1))];
end
end

Accepted Answer

Mischa Kim
Mischa Kim on 13 Jan 2021
Edited: Mischa Kim on 13 Jan 2021
Hi Bobby, this is the wonderful world of numerical (vs symbolic) computation. ode45 is a numerical integrator that approximates the actual solution. In essence this mean that with every integration step there will be a small error that adds up over time. You can fine-tune how well you would like to approximate the solution by setting tolerance levels. Try, e.g.
options = odeset('RelTol',1e-10);
[~,y] = ode45(@fun,0:0.05:40,[pi-0.1,0],options);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by