next up previous
Next: A variable time-step, high Up: tutorial6 Previous: MATLAB code for the

Solving the pendulum equation with RK2

The linear pendulum

The input statement for y will automatically give the right number of rows for y so long as the input is in the form of a column vector. Therefore, e.g. to enter \bgroup\color{black}$y_1(0)=0.5, y_2(0)=0$\egroup the column vector [ 0.5; 0] should be input at the prompt.

Use your modified Runge-Kutta routine to solve equation (3), for \bgroup\color{black}$t$\egroup in the range from 0 to 20. with initial conditions \bgroup\color{black}$y_1(0) \equiv y(0) = 0.5$\egroup, \bgroup\color{black}$y_2(0) \equiv y'(0)=0$\egroup .

Use \bgroup\color{black}$h=0.01$\egroup as the step-size here and throughout.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupCheck that your code is working correctly by calculating the linearized solution ylinear for this special case (see  (4)) and plotting on the same axes as the numerical solution, using red dashes (see plotting ).

\bgroup\color{blue}\framebox{\em CHECKPOINT: submit solution}\egroup \bgroup\color{black}$\phantom{0}$\egroup \bgroup\color{red}\framebox{ 2.}\egroup Give the maximum value of y(1,:)-ylinear for the case where the maximum time-value is \bgroup\color{red}$t=20$\egroup and \bgroup\color{red}$h=0.01$\egroup.

\bgroup\color{red}\framebox{\em MAKE A NEW FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupCopy the MATLAB function pendplot.m to your filespace.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupAdd a command to call pendplot.m just after the plotting in rk2_many.m.

Re-run the case \bgroup\color{black}$y_1(0) \equiv y(0) = 0.5$\egroup, \bgroup\color{black}$y_2(0) \equiv y'(0)=0$\egroup and you will now be able to see the pendulum motion.

The nonlinear pendulum

We can now move on to solution of the full nonlinear pendulum problem (2). You will have to modify rhs2.m to include the appropriate right hand sides.

Re-run rk2_many.m for the nonlinear case, but with the same initial conditions used above i.e. \bgroup\color{black}$y_1(0)=0.5$\egroup and \bgroup\color{black}$y_2(0)=0$\egroup.

What differences do you see?

Modify your analytical solution to the linearized problem, i.e. \bgroup\color{black}$y=0.5 \cos t$\egroup so that any initial amplitude can be used. Then re-run the program with \bgroup\color{black}$y(0)=1$\egroup and then \bgroup\color{black}$y(0)= \pi/2, 3\pi/4, 7\pi/8$\egroup, but keeping \bgroup\color{black}$y'(0)=0$\egroup and describe how the motion changes.

Now run the code with \bgroup\color{black}$y(0)=\pi$\egroup. What's happening?

\bgroup\color{red}\framebox{\em HAND CALCULATION}\egroup \bgroup\color{black}$\phantom{0}$\egroupExplain the results for the \bgroup\color{black}$y(0)=\pi, y'(0)=0$\egroup case.

In all the previous examples the pendulum was started from an initial condition corresponding to zero velocity ( \bgroup\color{black}$dy/dt = 0$\egroup). Now again consider the problem where the initial condition is \bgroup\color{black}$y=\pi$\egroup, corresponding to the case where the (rigid) pendulum is inverted on its end, but start with a non-zero velocity \bgroup\color{black}$dy/dt=0.5$\egroup and take the calculation out to time \bgroup\color{black}$t=10$\egroup. From the MATLAB plot of the resulting solution, describe the motion.


next up previous
Next: A variable time-step, high Up: tutorial6 Previous: MATLAB code for the
Charlie Macaskill 2004-07-26