function Name_XXXXXXX_hwN %insert the exact file name (as explained in the submission % instructions) after "function" %------------------------------------------------------------------------ %%% ENGINEERING MATHEMATICS III - MATH 2Z03 %%% ASSIGNMENT #4 %%% HOMEWORK \#4: HIGHER ORDER ODEs: SPRING -- MASS SYSTEM %------------------------------------------------------------------------ %------------------------------------------------------------------------ % Covers: % - "Numerical Mathematics" by M. Grasselli and D. Pelinovsky, % Sections 9.2. % - "Advanced Engineering Mathematics" by D.G. Zill and W.S. Wright, % (Jones and Bartlett, 4th edition) Section 3.8 %------------------------------------------------------------------------ %------------------------------------------------------------------------ % Instructions: % - Submit your assignment electronically (via Email) to the % address specific to your lab section as indicated on the course % website; hardcopy submissions will not be accepted. % - It is obligatory to use the current MATLAB template file available at % http://www.math.mcmaster.ca/gabardo/M2Z03/frames/template.m; % submissions non compliant with this template will not be accepted. % - Make sure to enter your name and student I.D. number in the % appropriate section of the template. % - Late submissions and submissions which do not comply with % these guidelines will not be accepted. % - All graphs should contain suitable titles and legends. %------------------------------------------------------------------------ % Written by Vladislav Bukshtynov, 2009 clc; close all; clear all; % Student information disp(' ------------ Student Information ------------ ') % Please enter your information here Name = 'Johny'; Surname = 'Good'; ID = 123456789; fprintf(' Student: %s %s (ID: %d) \n', Name, Surname, ID); disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Solution to Question #1 ---------- ') disp(' ') a=6;b=7;c=8;d=9; % Insert here your solution to Question #1 % (if you define your own functions, they may appear % at the end of the file) h = 0.05; % time step for x value x0 = 1; % initial value of y(0) u0 = 1; % initial value of u(0) T = 10;% maximum value of x n = T/h; % number of steps t(1) = 0; x(1) = x0; u(1) = u0; for k = 1:n t(k+1) = t(k) + h; % increment for x x(k+1) = x(k) + h * u(k); % update for the solution y (5*) u(k+1) = u(k) + h *(-(0.5+0.1*a)*u(k)-(4+0.1*b)*x(k)); % update for the solution u (5**) end figure; plot(t,x); legend('x=x(t)'); xlabel('t'); ylabel('x'); grid on; i=0; for k = 1:n if u(k)*u(k+1)<= 0 i=i+1; z(i)=t(k); r(i)=x(k); end end Answer1=[z;r] disp(' ') disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Solution to Question #2 ---------- ') disp(' ') % Insert here your solution to Question #2 % (if you define your own functions, they may appear % at the end of the file) h = 0.001; % time step for x value x0 = (1+c)/10; % initial value of y(0) u0 = -d/10; % initial value of u(0) T = 20;% maximum value of x n = T/h; % number of steps t(1) = 0; for j=1:6 x(j,1) = x0; u(j,1) = u0; m(1,j)=x0;m(2,j)=x0 end for k = 1:n t(k+1) = t(k) + h; % increment for x for j=1:6 x(j,k+1) = x(j,k) + h * u(j,k); % update for the solution y (5*) u(j,k+1) = u(j,k) + h *(-16*x(j,k)+cos((3+(j-1)*0.2)*t(k))); % update for the solution u (5**) if x(j,k+1) > m(1,j) m(1,j)=x(j,k+1); end if x(j,k+1)