ODE Homework
MAT360
Spring, 2017

Thanks, Lauren Hensley, for the start: p. 343, #1-5

#1

(y^I)(t) = t*y(t)^2
(y^II)(t) = t(2y(t) * (y^I)(t)) + y(t)^2
(y^III)(t) = 2t*y(t)*(y^II)(t) + y^I(t)[2t*(y^I)(t) + 2y(t)] + 2*(y^I)(t)y(t)
y(2) = 1. (y^I)(2) = 2(1)^2 = 2. (y^II)(2) = 2*2*1*2 + 1^2 = 9.
(y^III)(2) = 2*2*9 = 2[2*2*2 + 2] + 2 *2*1 = 60.

Clear["Global `*"]
f[t_]:=t*y[t]^2 (* y'(t) *)
f[t] (* y'(t) *)
f'[t] (* y''(t) *)
f''[t] (* y'''(t) *)
f[2]
f'[2]
f''[2]
y[2]=1
f[2]
y'[2]=f[2]
f'[2]
y''[2]=f'[2]
f''[2]

#2

(y^II)(t) = (ty^I)(t) + y(t)^2
(y^III)(t) = (ty^II)(t) + (y^I)(t) + 2y(t)(y^I)(t)
y(1) = 1. 
(y^I)(1) = 2. 
(y^II)(2) = 1*2 + 1^2 = 3. 
(y^III)(1) = 1*3 + 2 +2*1*2 = 9.

Clear["Global `*"]
f[t_]:=t*y'[t]+y[t]^2 (* y''(t) *)
f[t]
f'[t] (* y'''(t) *)
y[1]=1
y'[1]=2
f[1]
y''[1]=f[1]
f'[1]

#3

(y^I)(t) = t*y(t)
(y^II)(t) = t*(y^I)(t)*y(t) + y(t)

(Subscript[y, n+1]) = (Subscript[y, n]) + (Subscript[hy, n]^I) + h^2/2 * (Subscript[y, n]^II)
= (Subscript[y, n]) + h*(Subscript[t, n])(Subscript[y, n]) + h^2/2 * [(Subscript[t, n])* (Subscript[y, n]^I)*(Subscript[y, n]) + (Subscript[y, n])]

Whoops, I did a fourth! I’ll follow with a third.

Clear["Global `*"]
f[t_]:=t*y[t] (* y'(t) *)
f[t] (* y'(t) *)
f'[t] (* y''(t) *)
f''[t] (* y'''(t) *)
f'''[t] (* y''''(t) *)
y'[t]=f[t]
y''[t]=f'[t]
y'''[t]=f''[t]
next=y[t]+h*(f[t]+h/2*(f'[t]+h/3*(f''[t]+h/4*f'''[t]))) 
Simplify[next]

The step function, defined in this way, gives the Taylor-4 next guess from an initial starting point. Obviously one can then define y[.1] as the next guess, etc.

step[t_,h_]=Simplify[next]
y[0]=1
y[.1]=step[0,.1]
y[.2]=step[.1,.1]
y[.3]=step[.2,.1] (* etc.! *)

Clear["Global `*"]
f[t_]:=t*y[t] (* y'(t) *)
f[t] (* y'(t) *)
f'[t] (* y''(t) *)
f''[t] (* y'''(t) *)
y'[t]=f[t]
y''[t]=f'[t]
y'''[t]=f''[t]
next=y[t]+h*(f[t]+h/2*(f'[t]+h/3*f''[t])) 
HornerForm[Collect[Simplify[next],h]]

#4

(u^I)(t) = t^2 + u^2
(u^II)(t) = 2t + 2(u^I)*u
(u^III)(t) = 2 + 2((u^I))^2 + 2(u^II)* u
(u^IV)(t) = 2(u^II)* (u^I) + 2 (u^II)*(u^I) + 2(u^III)* u

Fourth order:
(Subscript[u, n+1]) = (Subscript[u, n]) + (Subscript[hu, n]^I) + h^2/2(Subscript[u, n]^II) + h^3/6*(Subscript[u, n]^III) + h^4/24*(Subscript[u, n]^IV)
with local error: 
(Subscript[d, n+1]) = -h^5/5! * u^V((Subscript[t, n])+theta*h) where theta is between 0 and 1.

fourth:

Clear["Global `*"]
f[t_]:=t^2+y[t]^2 (* y'(t) *)
f[t] (* y'(t) *)
f'[t] (* y''(t) *)
f''[t] (* y'''(t) *)
f'''[t] (* y''''(t) *)
y'[t]=f[t]
y''[t]=f'[t]
y'''[t]=f''[t]
next=y[t]+h*(f[t]+h/2*(f'[t]+h/3*(f''[t]+h/4*f'''[t]))) 
Collect[Simplify[next],h]
HornerForm[Collect[Simplify[next],h]]

#5

Lauren’s method passes yprime and y2 as a parameter:

Clear["Global `*"]
yprime[t_, y_] := t*y - y^2;
y2[t_, y_] := y+t * yprime[t, y] - 2*y * yprime[t, y];
taylor2[t0_, y0_,h_, xn_, yprime_, y2prime_] := 
Module[{t=t0, nexty, prevy = y0},
Print["t=",t,"; y=",prevy];
For[t= t0 , t < xn, t = t + h,
nexty = prevy + h*(yprime[t, prevy] + h/2*y2prime[t, prevy]);
prevy=nexty;
Print["t=",t+h,"; y=",nexty];
];
]
taylor2[0, 1, 0.02, 1, yprime, y2]

I just built in the given ODE. I also pass n, rather than h. This avoids a possible issue of n*h not equal to (end-start), because of round-off errors:

mytaylor2[t0_,y0_,n_,end_] := 
Module[{t=t0, y1, y2,y = y0,h=(end-t0)/n*1.0},
Print["t=",t,"; y=",y];
Do[
y1=t*y-y^2;
y2=y+t*y1-2 *y* y1;
y = y + h*(y1 + h/2*y2);
t=t+h;
Print["t=",t,"; y=",y],
{n}
];
]
mytaylor2[0, 1, 50, 1]