Eulers_Method_8.1.2.mw

8.1.2 Euler's Method and Backwards Euler 

 

Given diff(y, x) = `+`(`*`(5, `*`(t)), `-`(`*`(3, `*`(sqrt(y))))) with y(0) = 2 use Euler's Method to approximate t = .1, .2, .3, .4 

with h = 0.5e-1 and h = 0.25e-1 

 

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);
 

proc (t, y) options operator, arrow; `+`(`*`(5, `*`(t)), `-`(`*`(3, `*`(sqrt(y))))) end proc (1)
 

Let's approximate y(.1) using a step size of h = 0.5e-1. This means we need `and`(N = `+`(`*`(.1, `*`(`/`(0.5e-1)))), `+`(`*`(.1, `*`(`/`(0.5e-1)))) = 2) iterations. 

> N := 2; h := 0.05;
 

 

2
0.5e-1 (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 y(.1) is approximately... 

 

> yApprox := evalf(y[N]);
 

1.599801196 (3)
 

 

Now let's approximate and y(.4) using a step size of h = 0.5e-1. For t = .4 we 

need `and`(N = `+`(`*`(.4, `*`(`/`(0.5e-1)))), `+`(`*`(.4, `*`(`/`(0.5e-1)))) = 8) iterations (t = .2is the value after 4 iterations and t = .3 is the value after 6 iterations).
 

> N := 8; h := 0.05;
 

 

8
0.5e-1 (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 y(.2) is approximately... 

 

> yApprox := evalf(y[4]);
 

1.292884317 (5)
 

 

y(.3) is approximately... 

 

> yApprox := evalf(y[6]);
 

1.072415654 (6)
 

 

y(.4) is approximately... 

 

> yApprox := evalf(y[8]);
 

.9301746251 (7)
 

 

Next, let's approximate and y(.4) using a step size of h = 0.25e-1. For t = .4 we 

need `and`(N = `+`(`*`(.4, `*`(`/`(0.25e-1)))), `+`(`*`(.4, `*`(`/`(0.25e-1)))) = 16) iterations (t = .1is the value after 4 iterations, t = .2is the value after 8
iterations and t = .3 is the value after 12 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;
 

 

16
0.25e-1 (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 y(.1) is approximately... 

 

> yApprox := evalf(y[4]);
 

1.611240171 (9)
 

 

y(.2) is approximately... 

 

> yApprox := evalf(y[8]);
 

1.313607181 (10)
 

 

y(.3) is approximately... 

 

> yApprox := evalf(y[12]);
 

1.100116860 (11)
 

 

y(.4) is approximately... 

 

> yApprox := evalf(y[16]);
 

.9625518570 (12)
 

 

Now for Backwards Euler's Method. 

 

Let's approximate and y(.4) using Backwards Euler's and a step size of h = 0.5e-1.  

For t = .4 we need `and`(N = `+`(`*`(.4, `*`(`/`(0.5e-1)))), `+`(`*`(.4, `*`(`/`(0.5e-1)))) = 8) iterations (t = .1is the value after 2 iterations, t = .2is the value after 4
iterations, and t = .3 is the value after 6 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;
 

 

8
0.5e-1 (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 y(.1) is approximately... 

 

> yApprox := evalf(y[2]);
 

1.643368035 (14)
 

 

y(.2) is approximately... 

 

> yApprox := evalf(y[4]);
 

1.371644616 (15)
 

 

y(.3) is approximately... 

 

> yApprox := evalf(y[6]);
 

1.177630957 (16)
 

 

y(.4) is approximately... 

 

> yApprox := evalf(y[8]);
 

1.053340660 (17)
 

 

Finally, let's approximate and y(.4) using Backwards Euler's and a step size of
h = 0.25e-1. For t = .4 we need `and`(N = `+`(`*`(.4, `*`(`/`(0.25e-1)))), `+`(`*`(.4, `*`(`/`(0.25e-1)))) = 16) iterations (t = .1is the value after 4 iterations,
 

t = .2is the value after 8 iterations, and t = .3 is the value after 12 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;
 

 

16
0.25e-1 (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 y(.1) is approximately... 

 

> yApprox := evalf(y[4]);
 

1.633008139 (19)
 

 

y(.2) is approximately... 

 

> yApprox := evalf(y[8]);
 

1.352952030 (20)
 

 

y(.3) is approximately... 

 

> yApprox := evalf(y[12]);
 

1.152669248 (21)
 

 

y(.4) is approximately... 

 

> yApprox := evalf(y[16]);
 

1.024066907 (22)