function lab2soln % where Name_XXXXXXX_hwN is the name of the file you submit without the extension .m %------------------------------------------------------------------------ %%% ENGINEERING MATHEMATICS IV - MATH 2ZZ3 %%% ASSIGNMENT #5: %%% TRIPLE INTEGRALS, DIVERGENCE THEOREM AND CHANGE OF VARIABLES %------------------------------------------------------------------------ %------------------------------------------------------------------------ % Covers: % - "Numerical Mathematics" by M. Grasselli and D. Pelinovsky, % Sections 7.7--7.8. % "Advanced Engineering Mathematics" by D.G. Zill and W.S. Wright, section 9.15, 9.16, 9.17 %------------------------------------------------------------------------ %------------------------------------------------------------------------ % Instructions: % - Submit your assignment electronically (via Email) to the % address specific to your last name 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/MATH2ZZ3/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 = 'Lab 2'; Surname = 'Solutions'; ID = 1234567; fprintf(' Student: %s %s (ID: %d) \n', Name, Surname, ID); disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Solution to Question #1 ---------- ') disp(' ') % (a) t1 = 0:0.1:3; t2 = 0:0.01:3; % initialize output arrays for better efficiency y1 = zeros(size(t1)); y2 = zeros(size(t2)); for n = 2:31 y1(n) = y1(n-1) + 0.1*(-y1(n-1)+t1(n-1)); end Answer1 = y1(end); for n = 2:301 y2(n) = y2(n-1) + 0.01*(-y2(n-1)+t2(n-1)); end Answer2 = y2(end); % (b) t3 = 0:0.01:3; y3 = t3-1+exp(-t3); figure('Name','Euler''s method and exact solution for y'' = -y + t'); title('Euler''s method and exact solution for y'' = -y + t'); hold on; plot(t1,y1,t2,y2,t3,y3); legend('Euler: h = 0.1','Euler: h = 0.01','exact solution'); legend('Location','NorthWest'); disp(' ') disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Solution to Question #2 ---------- ') disp(' ') % (a) and (b) t = 0:0.01:100; % everything will happen within 100 years ... probably P = zeros(size(t)); P(1) = 50; n = 1; while P(n) < 300 n = n + 1; P(n) = P(n-1) + 0.01*( exp(-t(n-1))*P(n-1) - P(n-1)*(P(n-1)-300)/200 ); P(n) end Answer3 = t(n); while P(n) >= P(n-1) n = n + 1; P(n) = P(n-1) + 0.01*( exp(-t(n-1))*P(n-1) - P(n-1)*(P(n-1)-300)/200 ); end Answer4 = t(n-1); Answer5 = P(n-1); disp(' ') disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Answers ---------- ') disp(' ') % DO NOT TOUCH THIS PART (RESERVED FOR THE INSTRUCTOR'S USE) Name Surname ID Answer1 Answer2 Answer3 Answer4 Answer5 % User's Functions Space is below --------------------------