Math 2280-2
Problem 4.3.10Invoke some packages that are neededwith(DEtools):
Define the slope functionf := (t,x) -> Matrix([[1., 2.],[1., 0.]]).x + Vector([0.,exp(-t)]);xtrue:= t-> (1/9)*Vector([2*exp(2*t) - 2*exp(-t) - 6*t*exp(-t),exp(2*t) - exp(-t) + 6*t*exp(-t)]);Initial condition x0:=Vector([0,0]);Calculation with n=10Setting up some data structures for the iterationst0:=0.; tn:=1.; n :=10; h := (tn-t0)/n;
xvals := Vector(n+1): tvals := Vector(n+1):
xvals[1]:=x0: tvals[1]:=t0: RK4 using vectorsfor i from 1 to n do
x := xvals[i]: t := tvals[i]:
k1:= f(t,x): # left hand slope
k2:= f(t+h/2,x+h*k1/2): # midpoint slope: first approx.
k3:= f(t+h/2,x+h*k2/2): # midpoint slope: second approx.
k4:= f(t+h,x+h*k3): # right hand slope approx.
k:=(k1+2*k2+2*k3+k4)/6: # Simpson's integration rule
xvals[i+1]:= x + h*k: # improved Euler update
tvals[i+1]:= t+h: # increase x
end do:This is the approximation to x(1)print(tvals[n+1],xvals[n+1],xtrue(tvals[n+1]));Calculation with n=20Setting up some data structures for the iterationst0:=0.; tn:=1.; n :=20; h := (tn-t0)/n;
xvals := Vector(n+1): tvals := Vector(n+1):
xvals[1]:=x0: tvals[1]:=t0: RK4 using vectorsfor i from 1 to n do
x := xvals[i]: t := tvals[i]:
k1:= f(t,x): # left hand slope
k2:= f(t+h/2,x+h*k1/2): # midpoint slope: first approx.
k3:= f(t+h/2,x+h*k2/2): # midpoint slope: second approx.
k4:= f(t+h,x+h*k3): # right hand slope approx.
k:=(k1+2*k2+2*k3+k4)/6: # Simpson's integration rule
xvals[i+1]:= x + h*k: # improved Euler update
tvals[i+1]:= t+h: # increase x
end do:This is the approximation to x(1)print(tvals[n+1],xvals[n+1],xtrue(tvals[n+1]));Calculation of the true solution using MapleThis part of the code confirms there is a typo in the books xtrue.unassign('x'); unassign('t');
sys:={diff(x(t),t)=x(t)+2*y(t), diff(y(t),t)=x(t)+exp(-t)};dsolve(sys union {x(0)=0,y(0)=0});xtrue(t)[1]- (-(2/9)*exp(-t)+(2/9)*exp(2*t)-(2/3)*t*exp(-t));xtrue(t)[2] - (-(1/9)*exp(-t)+(1/9)*exp(2*t)+(2/3)*t*exp(-t));TTdSMApJNVJUQUJMRV9TQVZFLzE0NjM0NjE2WColKWFueXRoaW5nRzYiNiJbZ2whIyUhISEiIyIjIiIhRidGJg==TTdSMApJNVJUQUJMRV9TQVZFLzE2MDUyMjE2WColKWFueXRoaW5nRzYiNiJbZ2wnIyUhISEiIyIjM0ZGNTBBMjRGMUVERURFMDNGRjA2N0U3ODhCMTEwRjRGJg==TTdSMApJM1JUQUJMRV9TQVZFLzc3MzIyNFgqJSlhbnl0aGluZ0c2IjYiW2dsJyMlISEhIiMiIzNGRjUwQTQ2NUY2OTZENDMzRkYwNjdGOEJEM0JDRDE0RiY=TTdSMApJNVJUQUJMRV9TQVZFLzEyOTc0MzQ4WColKWFueXRoaW5nRzYiNiJbZ2wnIyUhISEiIyIjM0ZGNTBBNDQxNTdFMDIwQzNGRjA2N0Y3OTBENTY4RjdGJg==TTdSMApJNFJUQUJMRV9TQVZFLzMzMDEyNDhYKiUpYW55dGhpbmdHNiI2IltnbCcjJSEhISIjIiMzRkY1MEE0NjVGNjk2RDQzM0ZGMDY3RjhCRDNCQ0QxNEYm