You are on page 1of 9

MAT 275 MATLAB Assignment #4

Question 1
Part a

function LAB04ex1
clc
t0 = 0; tf = 40; y0 = [-1;0]; %sets time initial and final and initial
conditions
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t,Y]
u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and
u2
figure(1); %sets first figure
plot(t,u1,'b.-',t,u2,'r.-'); %plots figures
grid on
ylim([-1.5,1.5]);
legend('y(t)','v(t)=y''(t)'); %puts up legend for first plot
figure(2)
plot(u1,u2,'black.-'); axis square; xlabel('y'); ylabel('v=y''');
axis([-1,1,-1.5,1.5]); %sets the axis limits
set(gca,'XTick',-1:.2:1)
%sets the ticks on the plot on the x axis
grid on
%puts the grid on
end
%---------------------------------------------------------------------function dYdt = f(t,Y) %defines the ODE
y=Y(1); v=Y(2);
%sets the vectors for each y and v
dYdt = [v; cos(t)-4*v-3*y];
%the IVP equation
end
1.5
y(t)
v(t)=y'(t)
1

0.5

-0.5

-1

-1.5

10

15

20

25

30

35

40

1.5

v=y'

0.5

-0.5

-1

-1.5
-1

-0.8 -0.6 -0.4 -0.2

0
y

0.2

0.4

0.6

0.8

%Part b
Y is a maximum when t=2.1340, 7.3562, 13.6632, 19.9324, 26.2151, 32.4983, 38.7814
2.0365 -0.0931 0.0448
2.1340 -0.0903 0.0125
2.2316 -0.0905 -0.0162

7.2771
7.3562
7.4353
7.5057

0.2210 0.0265
0.2224 0.0087
0.2224 -0.0091
0.2212 -0.0248

13.5866 0.2228 0.0194


13.6632 0.2236 0.0023
13.7398 0.2231 -0.0148

19.8555 0.2225 0.0226

19.9324 0.2235
20.0093 0.2233

26.1381 0.2225
26.2151 0.2235
26.2920 0.2233

32.4213 0.2225
32.4983 0.2235
32.5752 0.2233

38.7045 0.2225
38.7814 0.2235
38.8584 0.2233

0.0054
-0.0118
0.0227
0.0056
-0.0117
0.0227
0.0056
-0.0117
0.0227
0.0056
-0.0117

Part c
The long term behavior of y seems sinusoidal fluctuating from -.2 to .2235

Part d
1.5
y(t)
v(t)=y'(t)
1

0.5

-0.5

-1

-1.5

10

15

20

25

30

35

40

1.5

v=y'

0.5

-0.5

-1

-1.5
-1

-0.8 -0.6 -0.4 -0.2

0
y

0.2

0.4

0.6

0.8

Based on the graphs, the equation stabilizes at the same frequency and amplitude as it does with
the initial conditions of y(0)=0,v(0)=-1. Based on the picture of y vs y, we can see that y starts
positively while y starts negatively compared with the old initial conditions.

Question 2
Part a
function LAB04ex2
clc
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t,Y]
u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and
u2
figure(1);
plot(t,u1,'b.-',t,u2,'r.-');
grid on
ylim([-1.5,1.5]);
legend('y(t)','v(t)=y''(t)');
figure(2)
plot(u1,u2,'black.-'); axis square; xlabel('y'); ylabel('v=y''');
axis([-1,1,-1.5,1.5]);
set(gca,'XTick',-1:.2:1)
grid on
end
%---------------------------------------------------------------------function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*v*y^2-3*y];
end

1.5
y(t)
v(t)=y'(t)
1

0.5

-0.5

-1

-1.5

10

15

20

25

30

35

40

1.5

v=y'

0.5

-0.5

-1

-1.5
-1

-0.8

-0.6

-0.4

-0.2

0
y

0.2

0.4

0.6

0.8

Part b
In the short term, the solution of figs L4g become stable much more quickly, reaching the cyclic
solution. On the other hand, the solution of Figs L4h reach stability much more slowly. The
behavior of the equation is much more erratic and takes longer to reach steady state.
Part c
The long term behavior shows that the solution of L4.4 has an amplitude of .47 while the
solution of L4.5 has an amplitude of double that, .95 or so. Both, however, have the same period
for their oscillations.
Part d

function LAB04ex2d

clc
clear all
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t,Y]
u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and
u2
[te,Ye]=euler(@f,[0,40],y0,400);
%calls eulers method using the function
f, initial conditions and number of steps
u3=Ye(:,1); u4=Ye(:,2);
figure(1);
plot(t,u1,'black.-',te,u3,'r.-');
grid on
ylim([-1.5,1.5]);
legend('y(t)','euler');
grid on
end
%---------------------------------------------------------------------function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*v*y^2-3*y];
end
1.5
y(t)
euler
1

0.5

-0.5

-1

-1.5

10

15

20

25

30

35

40

The solutions are nearly identical. The ode45 solution is a little more accurate and approaches
the cyclic behavior a little faster than the euler solution.

Question 3
function LAB04ex3
clc
clear all
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and
u2
[te,Ye]=euler(@f,[0,40],y0,400);
u3=Ye(:,1); u4=Ye(:,2);

figure(1);
plot(t,u1,'black.-',te,u3,'r.-');
grid on
ylim([-1.5,1.5]);
legend('y(t)','euler');
grid on
end
%---------------------------------------------------------------------function dYdt = f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*v*y-3*y];
end

When solving numerically for the solution of the IVP, the solution looks somewhat different to
that of L4.7. For one, the solution looks as if it deviates much more at the beginning than the
solution of L4.7. When trying to run the numerically solving of this IVP, MATLAB gave the
error message of Warning: Failure at t=3.774765e+000. Unable to meet integration tolerances
without reducing the step size below the smallest value allowed (7.105427e-015) at time t. As a
result, beyond the when t=3.77, the numerical solution of this ode could not be solved. Initial
behavior is known, but final behavior is not because of this.

Question 4
Part a
function LAB04ex4
clc
t0 = 0; tf = 40; y0 = [-1;0;4];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
u1 = Y(:,1); u2 = Y(:,2); u3=Y(:,3); % y in output has 2 columns
corresponding to u1 and u2
figure(1);
plot(t,u1,'b.-',t,u2,'r.-',t,u3,'.-k');
grid on
ylim([-1.5,1.5]);
legend('y(t)','v(t)=y''(t)','w=y''''');
figure(2)
plot3(u1,u2,u3,'k.-'); axis square;
xlabel('y'); ylabel('v=y'''); zlabel('w=y''''');
axis([-1,1,-1.5,2,-4,4]);
grid on
view(-40,60);
end
%---------------------------------------------------------------------function dYdt = f(t,Y)
y=Y(1); v=Y(2); w=Y(3);
dYdt = [v;w; -sin(t)-4*y^2*w-8*y*v^2-3*v];
end

1.5
y(t)
v(t)=y'(t)
w=y''

0.5

-0.5

-1

-1.5

10

15

20

25

30

35

40

4
w=y''

2
0
-2
-4
2
1

0.5
0

v=y'

0
-0.5

-1
-1

Part b
At the beginning, it looks as if L4i is more erratic than L4h, but both quickly go to the same
oscillation in roughly the same time. The amplitude of the solution of each of them is nearly
identical as is the period. In addition, looking at the phase plots of each, it seems as if they are
nearly identical with the one from Fig L4i
Part c
2
d d2 y 4 y 2 dy
d3 y
dy 2 dy
2 d y
+
+3
y=cos
(
t
)
=
+
4
y
+
8
y
+3 =sint
dt d t 2
dt
dt
dt
dt3
dt2

( )

When differentiated, the ODE of L4.7 is the same as the ODE in L4.8. They are identical
equations.

Part d
The equation in L4.7 satisfies the initial condition of L4.8 because when you set t=0
d2 y
dy
+ 4 y +3 y=1 . When we take the initial conditions of L4.8
2
dt
dt
2
( 0 )=1, dy ( 0 )=0, d y2 ( 0 )=4
,
dt
dt

initial conditions of L4.8.

d2 y
d2 y
3=1=
=4 , which is exactly the same as the
dt2
dt2