Solution Exam 2

Contents

Problem 1

Define the function f:

format compact
f = @(t,y) [y(2) ; t-y(2)-y(1)^2];

Problem 1(a)

One step of Euler Method:

t0 = 0; y0 = [1;2];
h = 1/2;
s1 = f(t0,y0)
y1 = y0 + h*s1
answer = y1(2)          % y' is given by 2nd component
s1 =
     2
    -3
y1 =
    2.0000
    0.5000
answer =
    0.5000

Problem 1(b)

One step of Improved Euler Method:

t0 = 0; y0 = [1;2];
h = 1/2;
s1 = f(t0,y0)
y1Euler = y0 + h*s1
s2 = f(t0+h,y1Euler)
y1 = y0 + h*(s1+s2)/2
answer = y1(1)          % y is given by 1st component
s1 =
     2
    -3
y1Euler =
    2.0000
    0.5000
s2 =
    0.5000
   -4.0000
y1 =
    1.6250
    0.2500
answer =
    1.6250

Problem 1(c)

Solve initial value problem using ode45:

[ts,ys] = ode45(f,[0,10],[1;2]);
answer = ys(end,1)      % y is given by 1st component
plot(ts,ys(:,2))        % y' is given by 2nd component
xlabel('t'); title('y''(t)')
answer =
    3.1457

Problem 2

r1 = 2 , r2 = -1

dsolve('D2y-Dy-2*y=0,y(0)=1,Dy(0)=1','t')
ans =
exp(-t)/3 + (2*exp(2*t))/3

Problem 3(a)

dsolve('D2y+2*Dy+10*y=2*exp(-t)','t')
ans =
(2*exp(-t))/9 + C5*cos(3*t)*exp(-t) + C6*sin(3*t)*exp(-t)

Problem 3(b)

dsolve('D2y+2*Dy+y=2*exp(-t)','t')
ans =
t^2*exp(-t) + C8*exp(-t) + C9*t*exp(-t)

Problem 3(c)

dsolve('D2y+2*Dy=3','t')  % Instead of C1*1 Matlab writes (C1-3/4)*1  (which is also correct)
ans =
C11 + (3*t)/2 + C12*exp(-2*t) - 3/4

Problem 3(d)

simplify(dsolve('D2y+2*y=3+2*exp(-t)','t'))
ans =
(2*exp(-t))/3 + C14*cos(2^(1/2)*t) + C15*sin(2^(1/2)*t) + 3/2