MAT275 LAB 4 Buyun Ma
Exercise 1
(a)
function LAB04ex1
t0 = 0; tf = 55; y0 = [-3;3];
[t,Y] = ode45(@f,[t0,tf],y0);
y = Y(:,1); v = Y(:,2);
figure(1);
plot(t,y,'b-','Linewidth',2);
ylabel('y(t)');
xlabel('t');
grid on;
hold on;
plot(t,v,'r-','Linewidth',2);
ylabel('y,v=y''');
legend ('y(t)','v(t)=y''(t)');
figure(2);
plot(y,v,'k','Linewidth',2);
axis square;
xlabel('y');
ylabel('v=y''');
ylim([-3,3]);
xlim([-2.5,2.5]);
grid on;
end
%----------------------------------------------------------------------
function dYdt = f(t,Y)
y = Y(1);
v = Y(2);
dYdt = [v;5*sin(t)-8*v-6*y];
end
type LAB04ex1;
LAB04ex1;
sy’
ya
FA
40
yt)
—wov')
(b)
Y reach a local maximum are:
7.1730 -0.0708 0.5313
7.2336 -0.0385 0.5338
7.2942 -0.0061 0.5344
7.3522 0.0249 0.5335
7.4103 0.0558 0.5308
(c)
The long term behaivor of y is periodic.
(d)
function LAB04ex1
t0 = 0; tf = 55; y0 = [1;1.8];
[t,Y] = ode45(@f,[t0,tf],y0);
y = Y(:,1); v = Y(:,2);
[t,Y(:,1),Y(:,2)]
figure(1);
plot(t,y,'b-','Linewidth',2);
ylabel('y(t)');
xlabel('t');
grid on;
hold on;
plot(t,v,'r-','Linewidth',2);
ylabel('y,v=y''');
legend ('y(t)','v(t)=y''(t)');
figure(2);
plot(y,v,'k','Linewidth',2);
axis square;
xlabel('y');
ylabel('v=y''');
ylim([-3,3]);
xlim([-2.5,2.5]);
grid on;
end
%----------------------------------------------------------------------
function dYdt = f(t,Y)
y = Y(1);
v = Y(2);
dYdt = [v;5*sin(t)-8*v-6*y];
end
No, the long term behavior is not changed, both are periodic. Figure 2 shows a closed
circles which indicates periodic behavior.
Exercise 2
function LAB04ex2
t0 = 0; tf = 55; y0 = [-1;1];
[t,Y] = ode45(@f,[t0,tf],y0);
y = Y(:,1); v = Y(:,2);
[t,Y(:,1),Y(:,2)]
figure(1);
plot(t,y,'b-','Linewidth',2);
ylabel('y(t)');
xlabel('t');
grid on;
hold on;
plot(t,v,'r-','Linewidth',2);
ylabel('y,v=y''');
legend ('y(t)','v(t)=y''(t)');
figure(2);
plot(y,v,'k','Linewidth',2);
axis square;
xlabel('y');
ylabel('v=y''');
ylim([-3,3]);
xlim([-2.5,2.5]);
grid on;
end
%----------------------------------------------------------------------
function dYdt = f(t,Y)
y = Y(1);
v = Y(2);
dYdt = [v;5*sin(t)-8*y.^2*v-6*y];
end
(b)
In figure7 the graphs are nice and smooth and in figure7,the graphs are all scattered
out.
(C)
In the first equation, it is linear, so the amplitude is constant. In the second equation,
it is nonlinear and its amplitude is constantly changing.
(d)
function LAB04ex2d
t0 = 0; tf = 55; y0 = [-1,1];
[t,Y] = ode45(@f,[t0,tf],y0);
[te,Ye]=euler(@f,[t0,tf],y0,700);
y = Y(:,1);
v = Y(:,2);
ye = Ye(:,1);
figure(1);
plot(t,y,'k',te,ye,'r','Linewidth',2);
grid on;
hold on;
legend('y(t)[ode45]','y(t)[Euler]');
ylim([-3,3]);
xlim([0,50]);
end
%----------------------------------------------------------------------
function dYdt = f(t,Y)
y = Y(1);
v = Y(2);
dYdt = [v;5*sin(t)-8*y.^2*v-6*y];
end
Yes, if you increase the step size, the solution is the same and the Euler method
becomes more accurate.
Exercise 3
function LAB04ex3
t0 = 0;tf = 55;y0 = [-0.5,0.5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
u1 = Y(:,1);
u2 = Y(:,2);
[te,Ye] = euler(@f,[0,55],y0,700);
u3 = Ye(:,1);u4= Ye(:,2);
figure(1);
plot(t,u1,'b-+',te,u3,'ro-');
grid on;
legend('y(t)','euler');
ylim([-3,3]);
end
function dYdt = f(t,Y)
y = Y(1);
v = Y(2);
dYdt = [v; 5*sin(t) - 8*y*v-6*y]
end
Exercise 4
function LAB04ex4
t0 = 0,tf =55; y0 =[-0.5;0.5;1.625];
[t,Y] = ode45(@f,[t0,tf],y0);
y = Y(:,1);
v = Y(:,2);
w = Y(:,3);
figure(9);
plot(t,y,t,v,t,w,'Linewidth',2);axis square; xlabel('t');
grid on;
legend('y(t)','v(t)','w(t)','Location','northeast');
ylim([-3,3]);
figure(10);
axis square;
grid on;
veiw([-40,60])
xlabel('y');ylebel('v=y''');zlabel('w = y''''');
ylim([-3,3]);
xlim([-2.5,2.5]);
zlim([-5,5]);
end
function dYdt = f(t,Y)
y = Y(1);v = Y(2); w =Y(3);
dYdt = [v;w; 5*cos(t)-8*y.^2*w-16*y*v.^2-6*v];
end