function a2sol %insert the exact file name (as explained in the submission % instructions) after "function" %------------------------------------------------------------------------ %%% ENGINEERING MATHEMATICS III - MATH 2Z03 %%% ASSIGNMENT #2 %%% EULER's METHOD FOR INITIAL--VALUE PROBLEMS %------------------------------------------------------------------------ %------------------------------------------------------------------------ % Covers: % - "Numerical Mathematics" by M. Grasselli and D. Pelinovsky, % Sections 9.1. % - "Advanced Engineering Mathematics" by D.G. Zill and W.S. Wright, % (Jones and Bartlett, 4th edition) Sections 2.6, 2.7, 2.8. %------------------------------------------------------------------------ %------------------------------------------------------------------------ % 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; h = 0.05; % time step y0 = 1+b/10; % initial value of y(0) T = 1; % length of interval [a,a+1] n = T/h; % number of steps x(1) = 0; y(1) = y0; % values of t and y at first step k = 1 for k = 1:n % Loop to generate steps from k = 1 to k = n x(k+1) = x(k) + h; % increment for t (**) y(k+1) = y(k) + h*(2*x(k)*sin(x(k)^2))*(y(k)^2-(a+2)*y(k)); % update for the solution y (*) end Answer1=y(n+1); h = 0.01; % time step y0 = 1+b/10; % initial value of y(0) T = 1; % length of interval [a,a+1] n = T/h; % number of steps x(1) = 0; z(1) =y0; % values of t and y at first step k = 1 for k = 1:n % Loop to generate steps from k = 1 to k = n x(k+1) = x(k) + h; % increment for t (**) z(k+1) = z(k) + h*(2*x(k)*sin(x(k)^2))*(z(k)^2-(a+2)*z(k)); % update for the solution y (*) end Answer2=z(n+1); 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=a+2; s=1+b/10; y_ex=r.*s./((r-s).*exp(r.*(1-cos(x.^2)))+s); y_num=z; plot(x,y_ex,x,y_num,'*') legend('y_{ex}', 'y_{num}','location','Northeast'); xlabel('x'); ylabel('y'); grid on; disp(' ') disp(' ------------------------------------------------ ') disp(' ') disp(' ') disp(' ') disp(' ') disp(' ---------- Solution to Question #3 ---------- ') disp(' ') % Insert here your solution to Question #2 % (if you define your own functions, they may appear % at the end of the file) c=8; d=9; k=2+c/10; r=1.25+d/100; Answer3=k^(1/(r-1)); h = 0.1;% time step P0 = 1;% initial value of P(0) t(1) = 0; P(1)=P0; j=1; q=1; while abs(Answer3-P(j))>=0.5 t(j+1) = t(j) + h; % increment for t (**) P(j+1) = P(j)+h*(0.2)*(k*P(j)-P(j)^r); % update for the solution P(*) if (P(j)<=Answer3/2) & (P(j+1)>Answer3/2) q=t(j); end j=j+1; end Answer4=q figure plot(t,P) xlabel('t'); ylabel('P'); 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