function [t, v] = skydiver_while(m,g,c,dt,tol) % Function to calculate skydiver falling speed % for specified parameter values and time increment % using an iterative, numerical solution with % a while loop. For this problem, the goal is to % calculate the correct terminal speed. % % Inputs: mass m (kg) % gravity g (m/s^2) % drag coefficient c (kg/m) % time increment (s) % convergence tolerance tol (m/s) % All inputs are passed into the function. % % Outputs: falling speed vector v (m/s) % Iterate the solution until the final velocity stops changing tolnew = 1; % Why do we need this statement? tnew = 0; t(1,1) = 0; vnew = 0; v(1,1) = vnew; % Can/should I pre-allocate memory for v? i = 1; % Why is this line needed? while tolnew > tol % Increment counter i = i+1; % Update v vold = vnew; dvdt = g-(c/m)*vold^2; vnew = vold + dvdt*dt; % Update t told = tnew; tnew = told + dt; % Update tol tolnew = abs(vold-vnew); % Why not abs((vold-vnew)/vnew)? % Store new solution t(i,1) = tnew; v(i,1) = vnew; end % Compare with analytic solution to see how good our final value % of v is. Note that we cannot normally do this, but we will do it % here to develop an understanding of what factors influence the % accuracy of the numerical solution. vtrue = sqrt(m*g/c)*tanh(sqrt(g*c/m)*t(i,1)); fprintf('The numerical final velocity is %7.4f m/s\n', v(i,1)) fprintf('and the analytical final velocity is %7.4f m/s.\n', vtrue) fprintf('The final time is %7.4f sec.\n', t(i,1)) % Question: How accurately can we find the time required to reach % terminal velocity? Only to within dt of the true value.