You are on page 1of 8

Simulate some Physics

Phenonmenon Apply ODE Method.

Phm B Duy
Phm Xun Huy
Bi Vn Tnh
L Xun Vng
1

First degree ODE: dy/dx=f(x,y)

Euler method is a numerical method to


solve ordinary differential equation (ODE).
Inlitial value problem
dy/dt=v(t) (3.1)
t1=to, y1=yo.
t2=t1+t
y2= ??? y2 = y1 + t*v(t)

Repeat this procedure, in general


yn+1=yn+v(tn). t. Quantity t is called
stepsize and can be chosen.
That is Euler method.
2

Truncation Error

Local truncation error in each step is


proportional to h.
Error propagates through steps of
computing, it is much bigger.
|ei|<=M(|eo|+B/A*h )
M=exp(A*(xi-xo))

|f/x|<=A
|y| <= K
B=(K/2)*h2
3

Euler-Cromer method

V(t+ t )=v(t) + t *a(t)

X(t+ t ) = x(t) + t *v(t+ t )

Approximate better for oscillating system.

Euler-Richarson method (1)

Euler-Richarson Method (2)

ODE Coding
Basic steps:
Step
Step
Step
Step

1.
2.
3.
4.

define f(x,y).
input initial value xo and yo.
input step size t and number of step n.
calculate x, y and y:
for i=1:n
x=x+h;
y=y+h*f(x,y);
y=y+h*y;
end
Step 5. output x and y.
Step 6. end
7

Euler Method Matlab Code

%Modified Euler Methods - Problem 3.1


function f=Eul(x,v,t)
K = 1; m = 1; x0=10; v0=0;
n = 100; dt = 0.1;
x = zeros(n,1);
v = zeros(n,1);
t = zeros(n,1);

x(1) = x0;
v(1) = 0;
t(1) = 0.0;

for step=1:n-1
v(step+1) = v(step) - dt*(x(step));
x(step+1) = x(step) + dt*v(step);
t(step+1) = t(step) + dt;
end
clf; plot(t,x,r');
legend('x-(cm)','v-(cm/s)'); xlabel ('Time (s)'); ylabel ('Amplitude');
title('Displacement vs time','FontSize',18); grid;
hold on;

You might also like