% chaotic_sqrt.m : a variant of apple_quiet.m that % solves the ODE y' = |y|^(1/2), subject to y(-2) = -1 % Try running: y(-2) = -1, i.e., t_0=-2, y_0=-1. % MATLAB seems to prefer the solution: % % y(t) = (-1/4)t^2 for t <= 0, and % y(t) = (1/4)t^2 for t >= 0 % % This is one valid solution of many. The more general solution is to % take any b >= 0, and to have % % y(t) = (-1/4)t^2 for t <= 0, % y(t) = 0 for 0 <= t <= b, and % y(t) = (1/4)(t-b)^2 for t >= b % % So try: % chaotic_sqrt(-2,2,1000,-1); % chaotic_sqrt(-2,2,100000,-1); % % So far, so good. You should observe that MATLAB reports: % y(t_end)=y(2)=1, and this is the t=1 valule of (1/4)t^2. % Hence MATLAB seems to prefer the b=0 solution. % % Since (1/4)t^2 = 0 at t=0, MATLAB should produce the same answer as % the above under the initial condition y(0)=0 for the value of y(t_end) % at t_end = 2 (or maybe not???...) % % Try: % chaotic_sqrt(0,2,10000,0); % hence y_0 = 0 % chaotic_sqrt(0,2,10000,1e-20); % hence y_0 = 10^{-20}, which is near 0 % chaotic_sqrt(0,2,10000,1e-40); % hence y_0 = 10^{-40}, yet closer to 0 % chaotic_sqrt(0,2,10000,-1e-1); % Explain... % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function result = whatever(t_0,t_end,N,y_0) % the name "whatever" is irrelevant % MATLAB calls this function "chaotic_sqrt" % assuming you have named this file chaotic_sqrt.m 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; result(N+1) % this is one thing to observe end % Second function: this is where we specify the function f. % This function is purely local to this program. function sqrt_y = f(t,y) sqrt_y = sqrt( abs(y) ); end