fsolve Doesn't Recognize Input Variables

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

 Risposta accettata

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.

3 Commenti

How would this code look if I had 2 equations and 2 unknowns? My code is below, but it doesn't seem to work.
Parent File:
Patm=101.3E3; %Atmosphere pressure
Po1=800E3; %Inlet stagnation pressure
P2=Patm; %Atmospheric pressure
gamma=1.4; %Specific heat ratio
guess=[1E-12; 0.5];
options = optimoptions('fsolve','Display','off'); %Turn off display
solution = fsolve(@(x1,x2)myfun_Fanno_Flow(x1,x2,gamma,P2,Po1),guess,options)
Child File:
function F = myfun_Fanno_Flow(M1,M2,gamma,P2,Po1)
F = [(-1/gamma/(M1^2))-(((gamma+1)/2/gamma)*log(M1^2/(1+(((gamma-1)/2)*M1^2))))-66.67;
((P2*((1+((gamma-1)/2*M2^2))^(gamma/(gamma-1))))*M2/M1*...
(((1+(((gamma-1)/2)*M1^2))/(1+(((gamma-1)/2)*M2^2)))^...
((gamma+1)/2/(gamma-1))))-Po1];
Thanks,
M Ridzon
solution = fsolve(@(x)myfun_Fanno_Flow(x(1),x(2),gamma,P2,Po1),guess,options)
Best wishes
Torsten.
Thank you again! Perfect! I think I am starting to see how fsolve is supposed to be set up.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 10 Mar 2017
That's correct. Functions have their own workspaces where the variables they have defined internally and the variables they received as input arguments when they are called "live". Because you neither defined the variable Ath in your myfun function's body nor passed it in as an input argument to myfun, it doesn't exist in that workspace and so you can't use it. [I'm deliberately omitting some of the more complicated details regarding workspaces and variable scoping; what I've listed is sufficient to answer the question.]
The approach Torsten used to solve this problem and make Ath visible inside the myfun function workspace is the first approach listed on this documentation page. Global variables is another solution, but they can lead to other complications so that documentation page recommends avoiding using them.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Richiesto:

il 10 Mar 2017

Commentato:

il 15 Mar 2017

Community Treasure Hunt

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

Start Hunting!

Translated by