How do I eliminate error when plotting 2D system of DEs with sinusoidal functions?

1 view (last 30 days)
I'm trying to plot the trajectories of an unforced and undamped pendulum in phase space at various initial conditions. I've got a system defined by:
function dY = undf(t,Y)
th = Y(1);
om = Y(2);
dY(1) = om;
dY(2) = -9.81*sin(th)/0.1;
dY = [dY(1);dY(2)];
end
I'm then using Matlab's ode45 function to calculate om(ega) and th(eta) on a set time interval at a given initial condition. I got good plots for initial conditions (0,0), (0.01,0), and (pi,0). For (pi,0), I used sym(pi) to get rid of round off error. My issue now is that when plotting from the I.C. (3,0), Matlab seems to be rounding off the values of th and om. This gives me a bad plot. I assume this is because sin(3) is an irrational number. However, when I try another I.C., such as (12,0), I get a good plot. Any ideas on how to fix this?
Thanks,
Axel

Accepted Answer

Mischa Kim
Mischa Kim on 22 Apr 2015
Edited: Mischa Kim on 22 Apr 2015
Axel, check out odeset. It allows you to set error tolerances. ode45 should work just fine for your problem.
If you show the entire code it is much easier to pinpoint the problem.
  1 Comment
Axel Larsson
Axel Larsson on 22 Apr 2015
Thanks for the reply, Mischa. I just tried using odeset to limit RelTol to 1e-10 and it worked. Much appreciated.
-Axel

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!