MATLAB: Fsolve Doesn’t Recognize Input Variables

fsolveglobalvariable

I'm new to the fsolve command and learning how it works. Following examples from Help, I wrote the following in the child file:
function F = myfun(M)
F = ((1/M)*(((1+((gamma-1)/2)*M^2)/(1+((gamma-1)/2)))^((gamma+1)/2/(gamma-1))))-(Ae/Ath);
I then wrote the following in the parent file and executed the script:
Ath=0.289; %Throat area (m^2)
Ae=0.578; %Exit area (m^2)
gamma=1.37; %Specific heat ratio
guess=1E-12;
fsolve(@myfun,guess)
However, fsolve doesn't work until I replace the variable references (i.e., gamma, Ae, Ath) in the child file with actual numbers (e.g., replace gamma w/1.37, etc.). Why is this and how do I work around this? Someone said I had to elevate the variables (i.e., gamma, Ae, Ath) to "global variables." But I don't know what that means, how to do it, or why. Any insight would be greatly appreciated.
Thanks, M Ridzon

Best Answer

Ath=0.289; %Throat area (m^2)
Ae=0.578; %Exit area (m^2)
gamma=1.37; %Specific heat ratio
guess=1E-12;
sol=fsolve(@(x)myfun(x,Ath,Ae,gamma),guess)
function F = myfun(M,Ath,Ae,gamma)
F = ((1/M)*(((1+((gamma-1)/2)*M^2)/(1+((gamma-1)/2)))^((gamma+1)/2/(gamma-1))))-(Ae/Ath);
Best wishes
Torsten.