function [t, v] = skydiver_params(f,dt,tol,varargin) % 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 show % how varargin can be used to pass input parameters to % the function passed into this function. % % Inputs: Function handle to first derivative function % time increment (s) % convergence tolerance tol (m/s) % varargin % which should be % mass m (kg) % gravity g (m/s^2) % drag coefficient c (kg/m) % if we pass in derivparams as the function to be called. % % 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 = f(vold,varargin{:}); 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)); % Note that we can no longer do the line above since m, g, and c % are no longer known to the program. 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.