LAB 4 - Anh Quan Truong - MAT 275
Exercise 1
Part (a)
type 'LAB04ex1.m'
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [0;-1];
%ODE
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1); v = Y(: ,2);
[t,Y];
%Plot
figure; %E1
plot(t,y,'b',t,v,'r');
grid on;
ylabel ("y,v=y'");
xlabel ('t');
ylim([-2.2,2.2]);
legend('y(t)','v(t)');
figure; %E1
plot(y,v);
grid on;
xlabel('y');
ylabel("v=y'");
axis square; ylim([-2.2,2.2]); xlim([-2.2,2.2]);
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2);
dydt = [v; -4*sin(t)-3*v-4*y];
end
LAB04ex1
1
y(t)
wt)
10
20
30
40
50
60
Part (b)
Maxima 1
2
[t,y,v]≈[55.6250, 0.9340, 0.1300]
[t,y,v]≈[55.7366, 0.9426, 0.0252]
[t,y,v]≈[55.8481, 0.9394 , -0.0798]
Maxima 2
[t,y,v]≈[49.4338, 0.9419, 0.0436]
[t,y,v]≈[49.5224, 0.9421, -0.0400]
[t,y,v]≈[49.6111, 0.9349, -0.1233]
Maxima 3
[t,y,v]≈[43.0922, 0.9376, 0.0987]
[t,y,v]≈[43.2163, 0.9428, -0.0183]
[t,y,v]≈[43.3196, 0.9358, -0.1154]
Part (c)
The long-term behavior is oscillatory and sinusoidal
Part (d)
type LAB04ex1d
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [1.8;1.9];
%ODE
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1); v = Y(: ,2);
[t,Y];
%Plot
figure; %E1d
plot(t,y,'b',t,v,'r');
grid on;
ylabel ("y,v=y'");
xlabel ('t');
ylim([-2.2,2.2]);
legend('y(t)','v(t)');
figure; %E1d
plot(y,v);
grid on;
xlabel('y');
ylabel("v=y'");
axis square; ylim([-2.2,2.2]); xlim([-2.2,2.2]);
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2);
dydt = [v; -4*sin(t)-3*v-4*y];
end
LAB04ex1d
3
y(t)
wt)
10
20
30
40
50
60
4
The initial condition only affected the first few intervals but it will not affect the long term behavior because the
main function has not changed. This can be seen in the graphs shown above. The only differences between
part A and B are the beginning values.
Exercise 2
Part (a)
Create a new M-file with the differential equation changed
type LAB04ex2
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [0;-1];
%ODE
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1); v = Y(: ,2);
[t,Y];
%Plot
figure; %E2
plot(t,y,'b',t,v,'r');
grid on;
ylabel ("y,v=y'");
xlabel ('t');
ylim([-2.2,2.2]);
legend('y(t)','v(t)');
figure; %E2
plot(y,v);
grid on;
xlabel('y');
ylabel("v=y'");
axis square; ylim([-2.2,2.2]); xlim([-2.2,2.2]);
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2);
dydt = [v; -4*sin(t)-3*y^2*v-4*y];
end
LAB04ex2
5
2
y(t)
wd)
15
4
os.
fo
&
05
a
15
2
o
10
20
30
40
50
60
t
2
15
4
os.
ro
s
05
a
15
2
45-1
05
05
1
15
2
Part (b)
6
The amplitudes are about the same when compared with Exercise 1 and the intervals look less like a sine
function than in Exercise 1. The second graphs of each Exercise also differ. One is more circular and the other
is more oblong.
Part (c)
The long term behavior is still oscillatory, but now it is non-sinusoidal.
Part (d)
You will have to create another M-file, LAB04ex2d.
type LAB04ex2d
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [0;-1];
%ODE and Euler's
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1);
[te,Ye]= euler(@f ,[t0 ,tf],y0,600);%euler's
ye= Ye(:,1);
%Plot
figure; %E2d
plot(t,y, 'k',te,ye,'r');
grid on;
xlabel('t');
ylabel('y');
xlim([t0,tf]);
ylim([-2.2,2.2]);
legend('y(t)','v(t)');
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2);
dydt = [v; -4*sin(t)-3*y^2*v-4*y];
end
LAB04ex2d
7
The solution are similar but not identical because Euler is used in estimation of the original function. By
increasing the N (the steps), you can incrementally get closer to the actual function but never will it be identical.
Exercise 3
type LAB04ex3
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [0;-1];
%ODE
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1); v = Y(: ,2);
[t,Y];
%Plot
figure; %E3
plot(t,y,'b',t,v,'r');
grid on;
ylabel ("y,v=y'");
xlabel ('t');
ylim([-22,2.2])
legend('y(t)','v(t)');
figure; %E3
plot(y,v);
grid on;
xlabel('y');
8
ylabel("v=y'");
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2);
dydt = [v; -4*sin(t)-3*y*v-4*y];
end
LAB04ex3
Warning: Failure at t=1.360176e+00. Unable to meet integration tolerances without reducing the step size
below the smallest value allowed (3.552714e-15) at time t.
9
The behavior of this function is different from exercise 1 because it is divergent. As you can see from the graph
the functions do not converge to a number, rather it diverges to negative infinity. The only difference from
exercise 1 is the 2nd term has a y which adds one change in the dydt. this slight change causes the function to
be divergent.
A warning above is saying it is compromising the scale to fit the step size inputted.
Exercise 4
type LAB04ex4
function ex_with_2eqs
%Intial Conditions
t0 = 0; tf = 60; y0 = [0;-1;-0.5];
%ODE
[t,Y] = ode45(@f ,[t0 ,tf],y0);
y = Y(: ,1); v = Y(: ,2); w = Y(:,3);
[t,Y];
%Plot
figure; %E4
plot(t,y,'b',t,v,'r',t,w,'k');
grid on;
ylabel ("y,v=y'");
xlabel ('t');
ylim([-2.2,2.2]);
legend('y(t)','v(t)','w(t)');
figure; %E4
plot3 (y,v,w);
grid on;
hold on; view ([ -40 ,60]);
10
xlabel ('y') ; ylabel ('v=y''') ; zlabel ('w=y''''');
ylim([-2.2,2.2])
end
% --------------------------------------
function dydt= f(t,Y)
y=Y(1); v=Y(2); w=Y(3);
dydt = [v; w;-4*cos(t)-3*y^2*w-6*y*v^2-4*v];
end
LAB04ex4
11
12