% apple.m : a program to solve y' = 2y, and y(t_0)=y_0, % from time t_0 to time t_end. % We use N steps of Euler's method. % Here are some points to consider: % (1) If you don't type a semicolon (;) at the end of a line, you will % print the contents of the MATLAB computation to your Command Window % (or your terminal if you are running from your Mac OS terminal window: % "/Applications/MATLAB_R2023b.app/bin/matlab -nodesktop -nosplash" % % (2) Note the function name "orange" -- when you type: % apple(1,2,10,3), % MATLAB will look in the file apple.m, and assume that the first % function defined is the function you wish to run. Any other % functions defined elsewhere are only local functions, and won't % run globally... %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % HOMEWORK: type the following commands into the command window (without the %'s). % % sol_one = apple(1,2,10,3) % size(sol_one) % sol_one(11) % % sol_two = apple(1,2,100,3) % size(sol_two) % sol_two(101) % % sol_three = apple(1,2,1000,3) % sol_three(1001) % % exp( 2 * (2-1) ) * 3 % % sol_one(11),sol_two(101),sol_three(1001),exp( 2 * (2-1) ) * 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Here is the code that will be run: % First function: solves y'=f(t,y) subject to y(t_0)=y_0 % using Euler's method, from t = t_0 to t = t_end, using % step size h = (t_end-t_0)/N % function result = orange(t_0,t_end,N,y_0) % the name "orange" is irrelevant % MATLAB calls this function "apple" h = (t_end - t_0)/N t = [t_0 : h: t_end] y(1) = y_0 for i=1:N y(i+1) = y(i) + h * f(t(i),y(i)) end result = y end % Second function: this is where we specify the function f. % This function is purely local to this program. function two_y = f(t,y) two_y = 2 * y end