Euler's Method and Backwards Euler 

 

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; 1; with(DEtools); -1
 

I will use our example equation from class. 

> f := proc (t, y) options operator, arrow; 1/2-t+2*y end proc; 1
 

(Typesetting:-mprintslash)([f := proc (t, y) options operator, arrow; 1/2-t+2*y end proc], [proc (t, y) options operator, arrow; 1/2-t+2*y end proc]) 

> eq := diff(y(t), t) = f(t, y(t)); 1
 

(Typesetting:-mprintslash)([eq := diff(y(t), t) = 1/2-t+2*y(t)], [diff(y(t), t) = 1/2-t+2*y(t)]) 

Maple easily solves our initial value problem. 

> soln := dsolve({eq, y(0) = 1}); 1
 

(Typesetting:-mprintslash)([soln := y(t) = 1/2*t+exp(2*t)], [y(t) = 1/2*t+exp(2*t)]) 

Let's approximate y(1) using a step size of h = 1/30. This means we need N = 30 iterations. 

> N := 30; 1; h := 1/30; 1
 

(Typesetting:-mprintslash)([N := 30], [30]) 

(Typesetting:-mprintslash)([h := 1/30], [1/30]) 

Here's Euler's Method. 

 

t[0] = 0 and y[0] = 1 (from our initial condition) 

> t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := y[n-1]+f(t[n-1], y[n-1])*h end do; 1
 

(Typesetting:-mprintslash)([t[1] := 1/30], [1/30]) 

(Typesetting:-mprintslash)([y[1] := 13/12], [13/12]) 

(Typesetting:-mprintslash)([t[2] := 1/15], [1/15]) 

(Typesetting:-mprintslash)([y[2] := 527/450], [527/450]) 

(Typesetting:-mprintslash)([t[3] := 1/10], [1/10]) 

(Typesetting:-mprintslash)([y[3] := 17059/13500], [17059/13500]) 

(Typesetting:-mprintslash)([t[4] := 2/15], [2/15]) 

(Typesetting:-mprintslash)([y[4] := 68911/50625], [68911/50625]) 

(Typesetting:-mprintslash)([t[5] := 1/6], [1/6]) 

(Typesetting:-mprintslash)([y[5] := 4447429/3037500], [4447429/3037500]) 

(Typesetting:-mprintslash)([t[6] := 1/5], [1/5]) 

(Typesetting:-mprintslash)([y[6] := 35832557/22781250], [35832557/22781250]) 

(Typesetting:-mprintslash)([t[7] := 7/30], [7/30]) 

(Typesetting:-mprintslash)([y[7] := 1153476199/683437500], [1153476199/683437500]) 

(Typesetting:-mprintslash)([t[8] := 4/15], [4/15]) 

(Typesetting:-mprintslash)([y[8] := 4636686046/2562890625], [4636686046/2562890625]) 

(Typesetting:-mprintslash)([t[9] := 3/10], [3/10]) 

(Typesetting:-mprintslash)([y[9] := 297943922569/153773437500], [297943922569/153773437500]) 

(Typesetting:-mprintslash)([t[10] := 1/3], [1/3]) 

(Typesetting:-mprintslash)([y[10] := 2391240052427/1153300781250], [2391240052427/1153300781250]) 

(Typesetting:-mprintslash)([t[11] := 11/30], [11/30]) 

(Typesetting:-mprintslash)([y[11] := 76711898474539/34599023437500], [76711898474539/34599023437500]) 

(Typesetting:-mprintslash)([t[12] := 2/5], [2/5]) 

(Typesetting:-mprintslash)([y[12] := 307424244288781/129746337890625], [307424244288781/129746337890625]) 

(Typesetting:-mprintslash)([t[13] := 13/30], [13/30]) 

(Typesetting:-mprintslash)([y[13] := 19701100902060109/7784780273437500], [19701100902060109/7784780273437500]) 

(Typesetting:-mprintslash)([t[14] := 7/15], [7/15]) 

(Typesetting:-mprintslash)([y[14] := 157738553554371497/58385852050781250], [157738553554371497/58385852050781250]) 

(Typesetting:-mprintslash)([t[15] := 1/2], [1/2]) 

(Typesetting:-mprintslash)([y[15] := 5049579908808247279/1751575561523437500], [5049579908808247279/1751575561523437500]) 

(Typesetting:-mprintslash)([t[16] := 8/15], [8/15]) 

(Typesetting:-mprintslash)([y[16] := 20198319635232989116/6568408355712890625], [20198319635232989116/6568408355712890625]) 

(Typesetting:-mprintslash)([t[17] := 17/30], [17/30]) 

(Typesetting:-mprintslash)([y[17] := 1292254562764530444049/394104501342773437500], [1292254562764530444049/394104501342773437500]) 

(Typesetting:-mprintslash)([t[18] := 3/5], [3/5]) 

(Typesetting:-mprintslash)([y[18] := 10331468093760530661767/2955783760070800781250], [10331468093760530661767/2955783760070800781250]) 

(Typesetting:-mprintslash)([t[19] := 19/30], [19/30]) 

(Typesetting:-mprintslash)([y[19] := 330311400624329901098419/88673512802124023437500], [330311400624329901098419/88673512802124023437500]) 

(Typesetting:-mprintslash)([t[20] := 2/3], [2/3]) 

(Typesetting:-mprintslash)([y[20] := 1319767710617284204003051/332525673007965087890625], [1319767710617284204003051/332525673007965087890625]) 

(Typesetting:-mprintslash)([t[21] := 7/10], [7/10]) 

(Typesetting:-mprintslash)([y[21] := 84354291588503534026898389/19951540380477905273437500], [84354291588503534026898389/19951540380477905273437500]) 

(Typesetting:-mprintslash)([t[22] := 11/15], [11/15]) 

(Typesetting:-mprintslash)([y[22] := 673836755689004376951515237/149636552853584289550781250], [673836755689004376951515237/149636552853584289550781250]) 

(Typesetting:-mprintslash)([t[23] := 23/30], [23/30]) 

(Typesetting:-mprintslash)([y[23] := 21527860986382303728219971959/4489096585607528686523437500], [21527860986382303728219971959/4489096585607528686523437500]) 

(Typesetting:-mprintslash)([t[24] := 4/5], [4/5]) 

(Typesetting:-mprintslash)([y[24] := 85961807392675630623329106586/16834112196028232574462890625], [85961807392675630623329106586/16834112196028232574462890625]) 

(Typesetting:-mprintslash)([t[25] := 5/6], [5/6]) 

(Typesetting:-mprintslash)([y[25] := 5491455205813623420348385087129/1010046731761693954467773437500], [5491455205813623420348385087129/1010046731761693954467773437500]) 

(Typesetting:-mprintslash)([t[26] := 13/15], [13/15]) 

(Typesetting:-mprintslash)([y[26] := 43847471085528846199914766243907/7575350488212704658508300781250], [43847471085528846199914766243907/7575350488212704658508300781250]) 

(Typesetting:-mprintslash)([t[27] := 9/10], [9/10]) 

(Typesetting:-mprintslash)([y[27] := 1400341446224578420022486142851899/227260514646381139755249023437500], [1400341446224578420022486142851899/227260514646381139755249023437500]) 

(Typesetting:-mprintslash)([t[28] := 14/15], [14/15]) 

(Typesetting:-mprintslash)([y[28] := 5590002759165994623102182120235721/852226929923929274082183837890625], [5590002759165994623102182120235721/852226929923929274082183837890625]) 

(Typesetting:-mprintslash)([t[29] := 29/30], [29/30]) 

(Typesetting:-mprintslash)([y[29] := 357021579914022917174335096368914269/51133615795435756444931030273437500], [357021579914022917174335096368914269/51133615795435756444931030273437500]) 

(Typesetting:-mprintslash)([t[30] := 1], [1]) 

(Typesetting:-mprintslash)([y[30] := 2850207050802715832476105484086079777/383502118465768173336982727050781250], [2850207050802715832476105484086079777/383502118465768173336982727050781250]) 

Our approximation of y(1)is y[N]. I let's print out a decimal approximation of y[N]. 

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

(Typesetting:-mprintslash)([yApprox := 7.432050342], [7.432050342]) 

The exact value of y(1) is given below: 

> yExact := subs(t = N*h, rhs(soln)); 1
 

(Typesetting:-mprintslash)([yExact := 1/2+exp(2)], [1/2+exp(2)]) 

The difference between the exact value and the approximated value gives us the "global truncation error". 

> evalf(yExact-yApprox); 1
 

.457005757 

Here's Backwards Euler's Method. 

Recall the Backwards Euler's is an implicit method. So we need to solve 

for y[n] each time through the loop. 

 

t[0] = 0 and y[0] = 1 (from our initial condition) 

> t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
t[0] := 0; -1; y[0] := 1; -1; for n to N do t[n] := t[n-1]+h; y[n] := solve(X = y[n-1]+f(t[n], X)*h, X) end do; 1
 

(Typesetting:-mprintslash)([t[1] := 1/30], [1/30]) 

(Typesetting:-mprintslash)([y[1] := 457/420], [457/420]) 

(Typesetting:-mprintslash)([t[2] := 1/15], [1/15]) 

(Typesetting:-mprintslash)([y[2] := 3473/2940], [3473/2940]) 

(Typesetting:-mprintslash)([t[3] := 1/10], [1/10]) 

(Typesetting:-mprintslash)([y[3] := 17561/13720], [17561/13720]) 

(Typesetting:-mprintslash)([t[4] := 2/15], [2/15]) 

(Typesetting:-mprintslash)([y[4] := 797791/576240], [797791/576240]) 

(Typesetting:-mprintslash)([t[5] := 1/6], [1/6]) 

(Typesetting:-mprintslash)([y[5] := 2412581/1613472], [2412581/1613472]) 

(Typesetting:-mprintslash)([t[6] := 1/5], [1/5]) 

(Typesetting:-mprintslash)([y[6] := 60717893/37647680], [60717893/37647680]) 

(Typesetting:-mprintslash)([t[7] := 7/30], [7/30]) 

(Typesetting:-mprintslash)([y[7] := 2747364257/1581202560], [2747364257/1581202560]) 

(Typesetting:-mprintslash)([t[8] := 4/15], [4/15]) 

(Typesetting:-mprintslash)([y[8] := 41394937487/22136835840], [41394937487/22136835840]) 

(Typesetting:-mprintslash)([t[9] := 3/10], [3/10]) 

(Typesetting:-mprintslash)([y[9] := 207712581963/103305233920], [207712581963/103305233920]) 

(Typesetting:-mprintslash)([t[10] := 1/3], [1/3]) 

(Typesetting:-mprintslash)([y[10] := 1874578499363/867763964928], [1874578499363/867763964928]) 

(Typesetting:-mprintslash)([t[11] := 11/30], [11/30]) 

(Typesetting:-mprintslash)([y[11] := 140882642107201/60743477544960], [140882642107201/60743477544960]) 

(Typesetting:-mprintslash)([t[12] := 2/5], [2/5]) 

(Typesetting:-mprintslash)([y[12] := 705425601828421/283469561876480], [705425601828421/283469561876480]) 

(Typesetting:-mprintslash)([t[13] := 13/30], [13/30]) 

(Typesetting:-mprintslash)([y[13] := 31772499038466593/11905721598812160], [31772499038466593/11905721598812160]) 

(Typesetting:-mprintslash)([t[14] := 7/15], [7/15]) 

(Typesetting:-mprintslash)([y[14] := 476785914270312431/166680102383370240], [476785914270312431/166680102383370240]) 

(Typesetting:-mprintslash)([t[15] := 1/2], [1/2]) 

(Typesetting:-mprintslash)([y[15] := 476785914270312431/155568095557812224], [476785914270312431/155568095557812224]) 

(Typesetting:-mprintslash)([t[16] := 8/15], [8/15]) 

(Typesetting:-mprintslash)([y[16] := 107237938686930843919/32669300067140567040], [107237938686930843919/32669300067140567040]) 

(Typesetting:-mprintslash)([t[17] := 17/30], [17/30]) 

(Typesetting:-mprintslash)([y[17] := 1607480103635057973217/457370200939967938560], [1607480103635057973217/457370200939967938560]) 

(Typesetting:-mprintslash)([t[18] := 3/5], [3/5]) 

(Typesetting:-mprintslash)([y[18] := 8029777681492957067109/2134394271053183713280], [8029777681492957067109/2134394271053183713280]) 

(Typesetting:-mprintslash)([t[19] := 19/30], [19/30]) 

(Typesetting:-mprintslash)([y[19] := 360913116812972431277249/89644559384233715957760], [360913116812972431277249/89644559384233715957760]) 

(Typesetting:-mprintslash)([t[20] := 2/3], [2/3]) 

(Typesetting:-mprintslash)([y[20] := 1081245274449180065232451/251004766275854404681728], [1081245274449180065232451/251004766275854404681728]) 

(Typesetting:-mprintslash)([t[21] := 7/10], [7/10]) 

(Typesetting:-mprintslash)([y[21] := 26989297733516859230030987/5856777879769936109240320], [26989297733516859230030987/5856777879769936109240320]) 

(Typesetting:-mprintslash)([t[22] := 11/15], [11/15]) 

(Typesetting:-mprintslash)([y[22] := 1212468525750339187713160303/245984670950337316588093440], [1212468525750339187713160303/245984670950337316588093440]) 

(Typesetting:-mprintslash)([t[23] := 23/30], [23/30]) 

(Typesetting:-mprintslash)([y[23] := 18154229930128376173485658753/3443785393304722432233308160], [18154229930128376173485658753/3443785393304722432233308160]) 

(Typesetting:-mprintslash)([t[24] := 4/5], [4/5]) 

(Typesetting:-mprintslash)([y[24] := 90598960380976644745816628357/16070998502088704683755438080], [90598960380976644745816628357/16070998502088704683755438080]) 

(Typesetting:-mprintslash)([t[25] := 5/6], [5/6]) 

(Typesetting:-mprintslash)([y[25] := 813783543578580932243974111405/134996387417545119343545679872], [813783543578580932243974111405/134996387417545119343545679872]) 

(Typesetting:-mprintslash)([t[26] := 13/15], [13/15]) 

(Typesetting:-mprintslash)([y[26] := 60910019079927486892233141482159/9449747119228158354048197591040], [60910019079927486892233141482159/9449747119228158354048197591040]) 

(Typesetting:-mprintslash)([t[27] := 9/10], [9/10]) 

(Typesetting:-mprintslash)([y[27] := 303920112258355557237562494238059/44098819889731405652224922091520], [303920112258355557237562494238059/44098819889731405652224922091520]) 

(Typesetting:-mprintslash)([t[28] := 14/15], [14/15]) 

(Typesetting:-mprintslash)([y[28] := 13647740818697674662016366041353167/1852150435368719037393446727843840], [13647740818697674662016366041353167/1852150435368719037393446727843840]) 

(Typesetting:-mprintslash)([t[29] := 29/30], [29/30]) 

(Typesetting:-mprintslash)([y[29] := 204283943845545752154853686383800609/25930106095162066523508254189813760], [204283943845545752154853686383800609/25930106095162066523508254189813760]) 

(Typesetting:-mprintslash)([t[30] := 1], [1]) 

(Typesetting:-mprintslash)([y[30] := 203851775410626384379461882147303713/24201432355484595421941037243826176], [203851775410626384379461882147303713/24201432355484595421941037243826176]) 

Our approximation of y(1)is y[N]. I let's print out a decimal approximation of y[N]. 

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

(Typesetting:-mprintslash)([yApprox := 8.423128533], [8.423128533]) 

The exact value of y(1) is given below: 

> yExact := subs(t = N*h, rhs(soln)); 1
 

(Typesetting:-mprintslash)([yExact := 1/2+exp(2)], [1/2+exp(2)]) 

The difference between the exact value and the approximated value gives us the "global truncation error". 

> evalf(yExact-yApprox); 1
 

-.534072434 

>