Using output from a nested function in parent function?

4 visualizzazioni (ultimi 30 giorni)
Graditz
Graditz il 29 Mar 2016
Modificato: dpb il 29 Mar 2016
function yFinal = calcHeightAtSpecifiedDistance(xmax,dt,y0,v0,angle0,D)
% yFinal = calcHeightAtSpecifiedDistance(xmax,dt,y0,v0,angle0,D)
% Inputs: (all scalars)
% xmax: maximum range for trajectory, m
% dt: Euler method step size, sec
% y0: launch height (assume starting range is x0=0)
% v0: launch speed
% angle0: launch angle, degrees
% D: drag (D = rho*Cd*A/2)
% Output:
% yFinal: y-value corresponding to xmax
computeTrajectoryEuler
function [x,y,vx,vy] = computeTrajectoryEuler(xmax,dt,y0,v0,angle0,D)
% function [x,y,vx,vy] = computeTrajectoryEuler(xmax,dt,y0,v0,angle0,D)
% Inputs: (all scalars)
% xmax: maximum range for trajectory, m
% dt: Euler method step size, sec
% y0: launch height (assume starting range is x0=0)
% v0: launch speed
% angle0: launch angle, degrees
% D: drag (D = rho*Cd*A/2)
% Outputs (all vectors of same length)
% x: ranges computed from 0 to xmax, at each time step along trajectory
% y: heights computed at corresponding time steps
% vx: x-velocity at corresponding time steps
% vy: y-velocity at corresponding time steps
% define g in m/s^2
g = 9.81;
% Assumption: Working wth golf ball
% to calculate trajectory correctly, include mass of golf ball in kg
m = 0.045;
% ball starts at x=0 and y=y0, and initial velocities calculated from intial
% velocity
x(1) = 0;
y(1) = y0;
vx(1) = v0 * cosd(angle0);
vy(1) = v0 * sind(angle0);
% the x vector starts at the second element since we know the first
% position is 0
k=2;
while x(k-1) < xmax
vx(k) = vx(k-1) + dt * (-D * sqrt((vx(k-1)^2) + (vy(k-1)^2)) * vx(k-1) / m);
vy(k) = vy(k-1) + dt * (-g - D * sqrt((vx(k-1)^2) + (vy(k-1)^2)) * vy(k-1) / m);
x(k) = x(k-1) + dt * vx(k-1);
y(k) = y(k-1) + dt * vy(k-1);
k=k+1;
end
end
yFinal=y(end);
end
I have this function where I would like to call a nested function and use one of its outputs to set the value for the output of the parent function. It is my understanding that nested function can access variables from the parent workspace, but does it work the other way around? If so, shouldn't I be able to say yFinal=y(end)? Thanks for the help.

Risposte (1)

dpb
dpb il 29 Mar 2016
Modificato: dpb il 29 Mar 2016
"Functions that return output arguments have variables for the outputs in their workspace. However, parent functions only have variables for the output of nested functions if they explicitly request them."
Hence as you've written the call w/o a return value y from the nested function isn't available in the caller.
...
y=computeTrajectoryEuler;
yFinal=y(end);
would work (I think, untested) but seems right at first blush...
ADDENDUM
Oh, to call computeTrajectoryEuler without an argument list you'll have to change the definition to remove them as well; otherwise it'll error on the call with "Not enough input arguments."
That uses the variables as if they were global in same context as calcHeightAtSpecifiedDistance. It also doesn't appear there's any point in having the other return variables besides y altho you could add them to the assignment for debugging or other purposes if desired later.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by