function IterationStepsize % Function to evaluate how iteration stepsize affects the accuracy % of the Euler iteration method for two different functions. % Inputs are read in interactively % Problem 1 is y = a + b*t assuming we only know dy/dt = b % and y(0) = a. Pick a = 4 and b = 3. % Problem 2 is y = a + b*t + c*t^2 assuming we only know dy/dt = b + 2*c*t % and y(0) = a. Pick a = 4, b = 3, and c = 10. % Problem 3 is y = a + b*sin(2*pi*t) assuming we only know % dy/dt = 2*b*pi*cos(2*pi*t)and y(0) = a. Pick a = 4 and b = 3. % Read inputs prob = input('Enter number (1, 2 or 3) of problem to be solved: '); step = input('Enter stepsize in seconds: '); % Create time vector t = [0:step:1]'; npts = length(t); y = zeros(npts,1); count = 1; % Perform Euler iteration based on selected problem and stepsize switch prob case 1 % y = a + b*t % Define problem constants a = 4; b = 3; % Define initial conditions y(1,1) = a; % Perform Euler iteration to produce numerical solution for i = 1:npts-1 dydt = b; y(i+1,1) = y(i,1) + dydt*step; end % Plot true and calculated solutions for comparison ytrue = a + b*t; plot(t,ytrue,'k-',t,y,'ro') legend('True','Computed') case 2 % y = a + b*t+ c*t^2 % Define problem constants a = 4; b = 3; c = 10; % Define initial conditions y(1,1) = a; % Perform Euler iteration to produce numerical solution for i = 1:npts-1 dydt = b + 2*c*t(i,1); y(i+1,1) = y(i,1) + dydt*step; end % Plot true and calculated solutions for comparison ytrue = a + b*t + c*t.*t; plot(t,ytrue,'k-',t,y,'ro') legend('True','Computed') case 3 % y = a + b*sin(2*pi*t) % Define problem constants a = 4; b = 3; % Define initial conditions y(1,1) = a; % Perform Euler iteration to produce numerical solution for i = 1:npts-1 dydt = b*2*pi*cos(2*pi*t(i)); y(i+1,1) = y(i,1) + dydt*step; end % Plot true and calculated solutions for comparison ytrue = a + b*sin(2*pi*t); plot(t,ytrue,'k-',t,y,'ro') legend('True','Computed') otherwise fprintf('The problem number entered is incorrect.\n') fprintf('Please run the program again.\n') return end