Professional Documents
Culture Documents
Phm B Duy
Phm Xun Huy
Bi Vn Tnh
L Xun Vng
1
Truncation Error
|f/x|<=A
|y| <= K
B=(K/2)*h2
3
Euler-Cromer method
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
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;