1 / 8100%
FENIL PATEL 1215196606
%% Lab 4 - KEY - MAT 275 Lab
% MATLAB solvers for First-Order IVP
function Lab04ex1
t0 = 0; tf = 55; y0 = [-0.5;0.5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y(:,1), Y(:,2)]
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([-3.5,3.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-3.5,3.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 5*sin(t)-4*v-3*y];
end
%% EX 1
%% A
type LAB04ex1
LAB04ex1
%% B
%%
% do not print the entire vectors t and Y, but include a few values which
% show where the last three maxima occur
40.2753 1.1123 0.1135
40.3522 1.1177 0.0278
40.4292 1.1165 -0.0583
46.5585 1.1123 0.1135
46.6354 1.1177 0.0278
46.7124 1.1165 -0.0583
52.8417 1.1123 0.1135
52.9186 1.1177 0.0278
52.9955 1.1165 -0.0583
%% C
% The long term of Y seems to be periodic and is repeated. Y is confined by a
%loop of values ENTER COMMENTS.
%% D
function Lab04ex1d
t0 = 0; tf = 55; y0 = [-1.8;-1.3];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y(:,1), Y(:,2)]
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([-3.5,3.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-3.5,3.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 5*sin(t)-4*v-3*y];
end
% NOTE: create a file "LAB04ex1d" which is a duplicate of LAB04ex1, but with the
% initial conditions for y and v changed.
% figure(1) and figure(2) also need to be changed to figure(3) and
% figure(4) (in order to plot part a and part d on separate figures so they
% can be compared)
type LAB04ex1d
LAB04ex1d
% The long term behavior of solution doesnt change as it still confines into a
%loop.
%%
% [ ENTER COMMENTS. Each line must have % before it ]
%% EX2
function Lab04ex2
t0 = 0; tf = 55; y0 = [-0.5;0.5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
[t, Y(:,1), Y(:,2)]
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([-3.5,3.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-3.5,3.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 5*sin(t)-4*v.*(y.^2)-3*y];
end
%% A
% create a new M-file with the differential equation changed
type LAB04ex2
LAB04ex2
%% B
% The amplitude of v(t) in fig 8 is greater than fig 7 whereas the amplitude of
% y(t) is almost equal in both figures.
%% C
% In long term in problem 4, the amplitude of y(t) gets equal to the amplitude of
v(t) whereas in problem 7, the amplitude of y(t) increases in compare to the
amplitude of v(t).
%% D
function Lab04ex2d
t0 = 0; tf = 55; y0 = [-0.5;0.5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
y = Y(:,1); % y in output has 2 columns corresponding to u1 and u2
figure(1);
plot(t,y,'b-+');
legend('y(t)');
ylim([-3.5,3.5])
hold on
[te,Ye] = euler(@f,[0,55],[-0.5,0.5],500);
plot(te,Ye,'r-')
grid on
hold off
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 5*sin(t)-4*v.*(y.^2)-3*y];
end
% You will have to create another M-file, LAB04ex2d, in order for he
% publishing to work in this case.
% NOTE: there should only be ONE output plot for the code you write in
% LAB04ex2d. The plot should superimposes solutions for y(t) from euler.m
% and ode45. Include a legend to label each solution.
type LAB04ex2d
LAB04ex2d
%%
% The solution of euler method and ODE is not identical as the local maxima and
local minima of euler method is greater than the values we got from ODE.
%% EX 3
function LAB04ex3
t0 = 0; tf = 55; y0 = [-0.5;0.5];
[t,Y] = ode45(@f,[t0,tf],y0,[]);
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([-3.5,3.5])
grid on
figure(2)
plot(y,v); axis square; xlabel('y'); ylabel('v'); % plot the phase plot
ylim([-3.5,3.5])
grid on
end
%----------------------------------------------------------------------
function dYdt= f(t,Y)
y=Y(1); v=Y(2);
dYdt = [v; 5*sin(t)-4*v*(y)-3*y];
end
% NOTE: the code for this part should be very similar to what you wrote for
% EX1-A. You just need to modify the system of differential equations. It's
% OK if you get an error message.
type LAB04ex3
LAB04ex3
%%
% The behavior of the solution is significantly different as the solution is
%periodic in fig 7 which is opposite of the behavior of solution of this part.
% The matlab command window shows the following warning message as the solution
%at that particular instance is not defined.
LAB04ex3
Warning: Failure at t=4.800127e+00. Unable to meet integration tolerances
without reducing the step size below the smallest value allowed (1.421085e-14) at
time t.
> In ode45 (line 360)
In LAB04ex3 (line 3)
%% EX4
function LAB04ex4
t0=0;
tf=50;
Y=[-0.5;0.5;0];
[t,Y]=ode45(@f,[t0,tf],Y);
y=Y(:,1);
v=Y(:,2);
w=Y(:,3);
figure(1);
plot(t,y,'b',t,v,'r',t,w,'m')
xlabel('t');
ylabel('y,v=y''');
legend('y(t)','v(t)=y''(t)','w(t)=y''''(t)')
grid on;
ylim([-3.5,3.5]);
figure(2);
plot3(y,v,w,'k.-'); % plot the phase plot
axis square;
xlabel('y');
ylabel('v=y''');
zlabel('w=y''''')
grid on
view([-40,60])
end
%......................................................
function dYdt=f(t,Y)
y=Y(1);
v=Y(2);
w=Y(3);
dYdt=[v;w;5*cos(t)-4*y^2*w-8*y*v^2-3*v];
end
% NOTE: this code should also be very similar to what you wrote for
% EX1-A and EX3
% When defining the function f at the bottom of your LAB04ex4.m file, dYdt
% now needs to be a column with THREE elements.
% LAB04ex4 should include commands which reproduce the plots shown in the
% lab4 document
type LAB04ex4
LAB04ex4
Powered by TCPDF (www.tcpdf.org)
Students also viewed