Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

function LAB04ex1 % NOTE: you CAN run this type of function m file as a script b

ID: 3860676 • Letter: F

Question

function LAB04ex1 % NOTE: you CAN run this type of function m file as a script because it requires no inputs. Click the Green arrow above the "Run" tab
t0 = 0; tf = 40; % initial and final time
y0 = [-1; 0];%[y0, v0]
[t,Y] = ode45(@f,[t0,tf],y0);% f=system of diff eqs; [t0,tf]

y = Y(:,1); % the output Y has 2 columns corresponding to y and v
v = Y(:,2); % the output Y has 2 columns corresponding to y and v

figure(1);
plot(t,y,'b-+'); % plots y(t)
hold on

plot(t,v,'ro-'); % plots v(t)
legend('y (t)','v (t)')
grid on;

figure(2)
plot(y,v); % plot solution in the space of y and v (instead of plotting y and v over time)
axis square; % makes the figure window square
xlabel('y'); ylabel('v');
ylim([-1.5,1.5]); xlim([-1,1]);
grid on
end
%-----------------------------------
function dydt = f(t,Y)
y = Y(1); v = Y(2);% for EX4 you will need a third element w=Y(3)
dYdt = [v; cos(t)-4*v-3*y];%=[dy/dt; dv/dt], the two derivatives as a function of y and v
end

3. Solve numerically the IVP +7y_ +5y =sint, h y(0)=-0.5,-(0)=0.5 wit dt dt in the interval 0Sts 50. Include the M-file in your report. Is the behavior of the solution significantly different from that of the solution of (L4.7)? Is MATLAB giving any warning message? Comment

Explanation / Answer

Solution for function ex_with_2eqs :

Although the ODE problem is now defined with two equations, the MATLAB implementation is

very similar to the case of a single ODE, except that vectors must now be used to describe the unknown

functions.

1 function ex_with_2eqs

2 t0 = 0; tf = 20; y0 = [10;60];

3 a = .8; b = .01; c = .6; d = .1;

4 [t,y] = ode45(@f,[t0,tf],y0,[],a,b,c,d);

5 u1 = y(:,1); u2 = y(:,2);    % y in output has 2 columns corresponding to u1 and u2

6 figure(1);

7 subplot(2,1,1); plot(t,u1,'b-+'); ylabel('u1');

8 subplot(2,1,2); plot(t,u2,'ro-'); ylabel('u2');

9 figure(2)

10 plot(u1,u2); axis square; xlabel('u_1'); ylabel('u_2'); % plot the phase plot

11 %----------------------------------------------------------------------

12 function dydt = f(t,y,a,b,c,d)

13 u1 = y(1); u2 = y(2);

14 dydt = [ a*u1-b*u1*u2 ; -c*u2+d*u1*u2 ];

solution for function ex_with_param:

1 function ex_with_param

2 t0 = 0; tf = 3; y0 = 1;

3 a = 1;

4 [t,y] = ode45(@f,[t0,tf],y0,[],a);

5 disp(['y(' num2str(t(end)) ') = ' num2str(y(end))])

6 disp(['length of y = ' num2str(length(y))])

7 %-------------------------------------------------

8 function dydt = f(t,y,a)

9 dydt = -a*(y-exp(-t))-exp(-t);