Effacer les filtres
Effacer les filtres

How can I write a code that works like MS Excel "Goal seak"?

1 vue (au cours des 30 derniers jours)
Luca Vergani
Luca Vergani le 3 Nov 2020
I'm working on a thermodynamic problem, and i have to find a pressure value that solves:
Value of η and are known, are function of that is the value i'm looking for.
in particular:
s9_1s = s8 ; %known
x9_1 = 0.98;
h9_1s = XSteam('h_ps',p9_1,s9_1s); % as you can see h values that are functions in wich p9_1 (that is the objective) is a parameter.
x9_1s = XSteam('x_ps',p9_1,s9_1s);
h9_1 = XSteam('h_px',p9_1,x9_1);
The problem is that i can't write in a explicit form, because of the function XSteam, so i'm not able to use Fsolve or similar.
Can anyone give me some help?

Réponse acceptée

Alan Stevens
Alan Stevens le 3 Nov 2020
Modifié(e) : Alan Stevens le 3 Nov 2020
What about fzero? Your code could be structured like
p90 = ... % put initial guess here
p9_1 = fzero(@(p) fn(p, XSteam), p90);
function Z = fn(p9_1, XSteam)
s9_1s = s8 ; %known
x9_1 = 0.98;
eta = ...
h8 = ...
h9_1s = XSteam('h_ps',p9_1,s9_1s);
x9_1s = XSteam('x_ps',p9_1,s9_1s);
h9_1 = XSteam('h_px',p9_1,x9_1);
Z = (h8 - h9_1)/(h8 - h9_1s) - eta;
end
View doc fzero for details on exact syntax etc of fzero.
  3 commentaires
Alan Stevens
Alan Stevens le 4 Nov 2020
The correct syntax for calling my fn function from fzero is
p9_1 = fzero(@(p) fn(p,@XSteam,s8), p90);
and the function line should be
function Z = fn(p9_1, XSteam,s8)
Luca Vergani
Luca Vergani le 8 Nov 2020
Modifié(e) : Luca Vergani le 8 Nov 2020
It finally works!!
Thanks thanks thanks both for the patience and for the answer.
Best regards.
LV

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by