% apple_quiet.m : a variant of apple.m that has semicolons (;) so that % the program runs quietly % A program to solve y' = 2y, and y(t_0)=y_0. % What is is really doing is computing (1+2h/N)^N y_0, i.e., % i.e., (1+2(t_end-t_0)/N)^N y_0 % Try running: % % % sol_three = apple_quiet(1,2,1000,3); % sol_three(1) % this should be t_0 % % sol_three(1001) % this should equal the approximation to y(t_end), which % due to the simple nature of f, turns out to be exactly %(1+2(t_end-t_0)/N)^N y_0 % sol_three(1001), (1+2*(2-1)/1000)^1000 * 3 % sol_four = apple_quiet(1,2,10000,3); % sol_four(10001) % sol_five = apple_quiet(1,2,100000,3); % sol_five(100001) % (1+2*(2-1)/100000)^100000 * 3 % This should equal the last line % exp( 2 * (2-1) ) * 3 % The exact solution to the ODE function result = orange(t_0,t_end,N,y_0) % the name "orange" is irrelevant % MATLAB calls this function "apple_quiet" 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