function Name_XXXXXXX_hwN %insert the exact file name (as explained in the submission % instructions) after "function" %------------------------------------------------------------------------ %%% ENGINEERING MATHEMATICS III - MATH 2Z03 %%% ASSIGNMENT #3 %%% EULER's AND HEUN's METHODS %------------------------------------------------------------------------ %------------------------------------------------------------------------ % 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) Sections 1.2, 2.3, 6.1. %------------------------------------------------------------------------ %------------------------------------------------------------------------ % 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/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(' ') % Insert here your solution to Question #1 % (if you define your own functions, they may appear % at the end of the file) a=6;b=7;c=8;d=9; A=a+1;B=1+b/10; h = 0.1; % time step y0 = 1+c/10; % initial value of y(0) n = 1/h; % number of steps t1(1)=1; yHeun1(1)=y0; for k = 1 : n t1(k+1) = t1(k) + h; p=yHeun1(k)+h*(A*t1(k)+B*yHeun1(k)-5); yHeun1(k+1)=yHeun1(k)+h/2*[(A*t1(k)+B*yHeun1(k)-5)+(A*t1(k+1)+B*p-5)]; end Answer1=yHeun1(6) h = 0.05; % time step y0 = 1+c/10; % initial value of y(0) n = 1/h; % number of steps t2(1)=1; yHeun2(1)=y0; ytrue(1)=y0; for k = 1 : n t2(k+1) = t2(k) + h; p=yHeun2(k)+h*(A*t2(k)+B*yHeun2(k)-5); yHeun2(k+1)=yHeun2(k)+h/2*[(A*t2(k)+B*yHeun2(k)-5)+(A*t2(k+1)+B*p-5)]; ytrue(k+1)=(5-A*t2(k+1))/B-(A/B^2)+exp(B*(t2(k+1)-1))*(y0-(5-A)/B+A/B^2); end Answer2=yHeun2(11) figure; plot(t1, yHeun1, '*',t2, yHeun2, '*',t2, ytrue) legend('yHeun1', 'yHeun2','ytrue'); xlabel('x'); ylabel('y'); grid on; 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) r=c+1;s=d+1; h = 0.05; % time step y0 = 1; % initial value of y(0) n = 2/h; % number of steps t3(1)=0; yMod1(1)=y0; for k = 1 : n t3(k+1) = t3(k) + h; p = yMod1(k) + 0.5*h*(4*sin(8*t3(k)))/((t3(k))^r+(yMod1(k))^s); yMod1(k+1)= yMod1(k)+h*(4*sin(8*(t3(k)+h/2)))/((t3(k)+h/2)^r+(p)^s); end Answer3=yMod1(41) h = 0.01; % time step y0 = 1; % initial value of y(0) n = 2/h; % number of steps t4(1)=0; yMod2(1)=y0; for k = 1 : n t4(k+1) = t4(k) + h; p = yMod2(k) + 0.5*h*(4*sin(8*t4(k)))/((t4(k))^r+(yMod2(k))^s); yMod2(k+1)=yMod2(k)+h*(4*sin(8*(t4(k)+h/2)))/((t4(k)+h/2)^r+(p)^s); end Answer4=yMod2(201) figure; plot(t3, yMod1,'*', t4, yMod2,'*') legend('yMod1', 'yMod2'); xlabel('x'); ylabel('y'); grid on; 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