% Lab 4_Patrick Hayes
% MAT 275 MATLAB Assignment #4
% 10-21-16
%% Exercise 1
% Part A)
function LAB04ex1
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y]
y = Y(:,1); v = Y(:,2); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+',t,v,'ro-');
legend('y(t)','v(t)=y''(t)')
ylim([-1.5,1.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-1.5,1.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*v-3*y];
end
% Part B)
% Local maximums between t=2.1340 and t=2.316 where y=.0905
% Local maximums between t=7.3562 and t=7.4353 where y=.2224
% Local maximums between t=13.6632 and t=13.7398 where y=.2231
% Local maximums between t=19.9324 and t=20.0093 where y=0.2233
% Local maximums between t=26.2151 and t=26.920 where y=.2233
% Local maximums between t=32.4983 and t=32.5752 where y=.2233
% Local maximums between t=38.7814 and t=38.8584 where y=.2233
% Part C)
% The long term behavior of y stabilizes into a close curve as shown in
% figure 2 of the function file LAB04ex1. Figure 1 shows that y appears to
% be sinusoidal when it stabilizes.
% Part D)
% The graph appears to still have the same behavior despite the initial
% conditions. The second figure has a tight behavior and the first figure
% has a similar sinusoidal pattern.
function LAB04ex1
t0 = 0; tf = 40; y0 = [1.5;5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y]
y = Y(:,1); v = Y(:,2); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+',t,v,'ro-');
legend('y(t)','v(t)=y''(t)')
ylim([-1.5,1.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-1.5,1.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*v-3*y];
End
%% Exercise 2
% Part A)
function LAB04ex2
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y]
y = Y(:,1); v = Y(:,2); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+',t,v,'ro-');
legend('y(t)','v(t)=y''(t)')
ylim([-1.5,1.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-1.5,1.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*(y^2)*v-3*y];
end
% Part B)
% The short term for L4.4 approaches stability faster compared to L4.7
% which still has wide curves.
% Part C)
% The long term for both functions leads to tight loops which indicates
% stability of the functions
% Part D)
function LAB04ex2
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y]
y = Y(:,1); v = Y(:,2); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+',t,v,'ro-');
legend('y(t)','v(t)=y''(t)')
ylim([-1.5,1.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-1.5,1.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*(y^2)*v-3*y];
end
function dYdt= f(te,Ye)
y=Y(1); v=Y(2);
dYdt = [v; cos(te)-4*(Ye^2)*v-3*Ye];
[te,ye]=euler(dYdt, [0,40], -1, 400);
figure(3)
plot(te,Ye,'r', t,y,'k')
-10
-12
x
4
oi’
0.5
1.5
2.0
end
% The solutions are similar and show the same general solution.
%% Exercise 3
% The solutions are radically different than the solutions for L4.7, the
% graphs have no consistency and do not seem to stabilize. There are no
% error messages for the code
function LAB04ex3
t0 = 0; tf = 40; y0 = [-1;0];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y]
y = Y(:,1); v = Y(:,2); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+',t,v,'ro-');
legend('y(t)','v(t)=y''(t)')
ylim([-1.5,1.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-1.5,1.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; cos(t)-4*(y)*v-3*y];
end
%% Exercise 4
% Part A)
function LAB04ex4
t0 = 0; tf = 40; y0 = [-1;0;4];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
y= Y(:,1); v = Y(:,2); w= Y(:,3); % y in output has 3 columns corresponding to y and v and w
figure(1);
plot(t,y,'b',t,v,'r',t,w,'g');
legend('y(t)','v(t)=y''(t)', 'w(t)=y''''(t)')
grid on;
lim(-1.5,1.5)
figure(2)
plot3(y,v,w,'k.-'); % plot the phase plot
grid on; view([-40,60])
xlabel('y'); ylabel('v=y'''); zlabel('w=y''''');
end
%----------------------------------------------------------------------
function dYdt = f(t,Y)
y = Y(1); v = Y(2); w=Y(3);
dYdt = [w; -sin(t)-4*(y^2)*w-8*y*(v^2)-3*v];
End
% Part B)
% For figure L4i, w or the highest derivative is the most radical wave on the graph for the
longest period of t and does not fall into the normal sinusoidal pattern as the other function. L4h
settles down and has dense curves after a period. The obscurity of the w portion of the function
seems to create the pattern of a sinusoidal wave in the z axis as it spirals toward stability on all
three planes.
% Part C)
%The derivative of L4.7 is L4.8 so they are identical when L4.7 is differentiated and compared
to L4.8
% Part D)
% When t=0, the value for w(t)=w(0) can be found which provides the initial condition for w(t).
As for the other initial conditions, the two functions are identical when the derivative is taken of
L4.7 so the initial conditions can be carried over to L4.8