8.1.2 Euler's Method and Backwards Euler
Given with use Euler's Method to approximate
with and
Then do the same using Backwards Euler's
It's always a good idea to begin your page with a "restart" command -- so when re-running
commands, you can start with a blank slate.
> | restart; |
> | f := (t,y) -> 5*t - 3*sqrt(y); |
(1) |
Let's approximate using a step size of . This means we need iterations.
> | N := 2; h := 0.05; |
(2) |
> | # Euler's Method
t[0] := 0: y[0] := 2: for n from 1 to N do t[n] := t[n-1] + h: y[n] := y[n-1] + f(t[n-1],y[n-1])*h: end do: |
So is approximately...
> | yApprox := evalf(y[N]); |
(3) |
Now let's approximate and using a step size of . For we
need iterations (is the value after 4 iterations and is the value after 6 iterations).
> | N := 8; h := 0.05; |
(4) |
> | # Euler's Method
t[0] := 0: y[0] := 2: for n from 1 to N do t[n] := t[n-1] + h: y[n] := y[n-1] + f(t[n-1],y[n-1])*h: end do: |
So is approximately...
> | yApprox := evalf(y[4]); |
(5) |
is approximately...
> | yApprox := evalf(y[6]); |
(6) |
is approximately...
> | yApprox := evalf(y[8]); |
(7) |
Next, let's approximate and using a step size of . For we
need iterations (is the value after 4 iterations, is the value after 8
iterations and is the value after iterations).
NOTE: Because trying to do all of the intermediate calculations exactly is too hard for my computer
I will introduce an "evalf" command in the next to last line of Euler's Method. This will approximate
the numbers used in the intermediate steps and greatly speed up computation.
> | N := 16; h := 0.025; |
(8) |
> | # Euler's Method
t[0] := 0: y[0] := 2: for n from 1 to N do t[n] := t[n-1] + h: y[n] := evalf(y[n-1] + f(t[n-1],y[n-1])*h): end do: |
So is approximately...
> | yApprox := evalf(y[4]); |
(9) |
is approximately...
> | yApprox := evalf(y[8]); |
(10) |
is approximately...
> | yApprox := evalf(y[12]); |
(11) |
is approximately...
> | yApprox := evalf(y[16]); |
(12) |
Now for Backwards Euler's Method.
Let's approximate and using Backwards Euler's and a step size of .
For we need iterations (is the value after 2 iterations, is the value after 4
iterations, and is the value after iterations).
NOTE: Because trying to do all of the intermediate calculations exactly is too hard for my computer
I will introduce an "evalf" command in the next to last line of Backwards Euler's. This will
approximate the numbers used in the intermediate steps and greatly speed up computation.
> | N := 8; h := 0.05; |
(13) |
> | # Backwards Euler's Method
t[0] := 0: y[0] := 2: for n from 1 to N do t[n] := t[n-1] + h: y[n] := evalf(solve(X = y[n-1] + f(t[n],X)*h, X)): end do: |
So is approximately...
> | yApprox := evalf(y[2]); |
(14) |
is approximately...
> | yApprox := evalf(y[4]); |
(15) |
is approximately...
> | yApprox := evalf(y[6]); |
(16) |
is approximately...
> | yApprox := evalf(y[8]); |
(17) |
Finally, let's approximate and using Backwards Euler's and a step size of
. For we need iterations (is the value after 4 iterations,
is the value after 8 iterations, and is the value after iterations).
NOTE: Because trying to do all of the intermediate calculations exactly is too hard for my computer
I will introduce an "evalf" command in the next to last line of Backwards Euler's. This will
approximate the numbers used in the intermediate steps and greatly speed up computation.
> | N := 16; h := 0.025; |
(18) |
> | # Backwards Euler's Method
t[0] := 0: y[0] := 2: for n from 1 to N do t[n] := t[n-1] + h: y[n] := evalf(solve(X = y[n-1] + f(t[n],X)*h, X)): end do: |
So is approximately...
> | yApprox := evalf(y[4]); |
(19) |
is approximately...
> | yApprox := evalf(y[8]); |
(20) |
is approximately...
> | yApprox := evalf(y[12]); |
(21) |
is approximately...
> | yApprox := evalf(y[16]); |
(22) |