You are on page 1of 16

Pendulum simulation

Fisika
Semester Ganjil TA 2016/2017
Kania Nur Sawitri, S.Si, M.Si

Outline
Pendulum solution using the euler method

Pendulum solution using the euler cromer method


Simple harmonic motion using a variety of numerical
approaches

Pendulum solution using the euler


method
What is euler method?
Hows the pendulum function being related by
euler method?

Eulers Method
y

yi 1 yi f xi , yi h

True Value

h xi 1 xi

yi+1, Predicted value

yi
h
Step size
xi

xi+1

General graphical interpretation of Eulers method

Pendulum solution

+
+
Initial condition:
=1m
= 10
=0.04 s
= 1 to 250

code
clear;
clc;
length= 1;
g=9.8; %inisiasi
npoints = 250;
dt = 0.04;
omega = zeros(npoints,1);
theta = zeros(npoints,1);
time = zeros(npoints,1);
theta(1)=0.2;
for step = 1:npoints-1
omega(step+1) = omega(step) - (g/length)*theta(step)*dt;
theta(step+1) = theta(step)+omega(step)*dt ;
time(step+1) = time(step) + dt;

end
plot(time,theta,'r' );
xlabel('time (seconds) ');
ylabel('theta (radians)');

Graph

Euler cromer method


We only need to make a small modification of
Eulers Method
in the second equation of the method we use the
updated value of the dependent variable as
computed in the first line.

+
+

code
clear;
clc;
length= 1;
g=9.8;
npoints = 250;
dt = 0.04;
omega = zeros(npoints,1);
theta = zeros(npoints,1);
time = zeros(npoints,1);
theta(1)=0.2;
for step = 1:npoints-1
omega(step+1) = omega(step) - (g/length)*theta(step)*dt;
theta(step+1) = theta(step)+omega(step+1)*dt;
time(step+1) = time(step) + dt;

end
plot(time,theta,'r' );
xlabel('time (seconds) ');
ylabel('theta (radians)');

Graph

2nd order Runge kutta


method
Whats 2nd order runge kutta?

Runge-Kutta 2
Method

Order

dy
f ( x, y ), y (0) y0
dx
http://numericalmethods.

For

nd

Runge Kutta 2nd order method is given by

yi 1 yi a1k1 a 2 k 2 h
where

k1 f xi , yi
k2 f xi p1h, yi q11k1h
12

Heuns Method

Heuns method

Slope f xi h, yi k1h

Here a2=1/2 is
1
chosen
a

yi+1, predicted

Slope f xi , yi

2
p1 1

q11 1

Average Slope
yi

1
f x i h , y i k1 h f x i , y i
2

resulting in

1
1
yi 1 yi k1 k2 h
2
2
where

xi

xi+1

k 1 f x i , y i k 2 f x i h, y i k 1 h

code
clear;
length= 1;
g=9.8;
npoints = 250;
dt = 0.04;
omega = zeros(npoints,1);
omega_dash=zeros(npoints,1);
theta = zeros(npoints,1);
theta _dash=zeros(npoints,1);
time = zeros(npoints,1);
theta(1)=0.2;
for step = 1:npoints-1
omega_dash= omega(step) 0.5*(g/length)*theta(step)*dt;
theta_dash= theta(step)+0.5*omega(step)*dt
omega(step+1) = omega(step) - (g/length)*theta_dash*dt;
theta(step+1) = theta(step)+omega_dash*dt
time(step+1) = time(step) + dt;

end
plot(time,theta,'r' );
xlabel('time (seconds) ');
ylabel('theta (radians)');

graph

Combine three methods


Use function methods!

You might also like