27 lines
556 B
Plaintext
27 lines
556 B
Plaintext
f := y -> (-0.07) * (y - 20):
|
|
|
|
EulerMethod := proc(f, start_time, end_time, y0, h) # y0: initial value #h: step size
|
|
local cur, y, rate:
|
|
cur := start_time;
|
|
y := y0;
|
|
while cur <= end_time do
|
|
printf("%g %g\n", cur, y);
|
|
cur := cur + h;
|
|
rate := f(y);
|
|
y := y + h * rate;
|
|
end do;
|
|
return y;
|
|
end proc:
|
|
|
|
# step size = 2
|
|
printf("Step Size = %a\n", 2);
|
|
EulerMethod(f, 0, 100, 100, 2);
|
|
|
|
# step size = 5
|
|
printf("\nStep Size = %a\n", 5);
|
|
EulerMethod(f, 0, 100, 100, 5);
|
|
|
|
# step size = 10
|
|
printf("\nStep Size = %a\n", 10);
|
|
EulerMethod(f, 0, 100, 100, 10);
|