How to use fsolve an implicit equation?

clc, clear
a1=28*7*400; %%ft^2
po=40.6; %%psi
delpio=9; %%psi
qf=1525; %%gpm
qo=qf;
pp=16.4; %%psi
qp=1234; %%gpm
depr1=24.9; %%psi
delpr2=18.2; %%psi
PH=360; %%ft
lp=2.76*.264/(60)^2*14.5; %%gal/m*min^2/psi
k2=1.75*10^-4*60^1.67*14.5/264.172^1.67; %%psi/(gal/min)^1.67
k3=2.27*10^-2; %%(m^3/h)^.6/m^2
g=32.17; %%ft/s^2
rhoh20=62.4; %%lbs/ft^3
pf=rhoh20*g*PH*1.488*.000145; %%psi
delpo=pf-po; %%psi
icQ=qo; %%gpm
icdelp=delpo; %%psi
function dydx=odes(jw,a1,k2)
dydx=zeros(2,1);
dydx(1)=-jw*a1;
dydx(2)= -k2*y(1)^1.67;
end
function F=JW(lp,qo,delpio,k3,y)
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40))-z;
end
xo=0;
X=@JW
jw=fsolve(JW,xo);
xspan=[0 50];
[x,y]=ode45(@odes,xspan,[icQ icdelp]);
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in mhlro (line 24)
jw=fsolve(@(z)JW(lp,qo,delpio,k3,y),xo);
Caused by:
Failure in initial objective function evaluation.
FSOLVE cannot continue.
FIrst is my function of differntial equations
followed by my function that i want to fsolve.
I want to find the number z in the equation but that equation is dependent on the differential equations and the differentials are depenedent on z.
The error i get is below.

8 个评论

@(z)JW(lp,qo,delpio,k3,y)
That ignores z, so fsolve would not be able to propose different input vectors as solutions.
Your code does not match the error message, which makes it confusing.
jw=fsolve(JW,xo);
That would try to invoke JW with no inputs, expecting that it would return a function handle that could be solved against.
Im sorry. Here it is updated..
clc, clear
a1=28*7*400; %%ft^2
po=40.6; %%psi
delpio=9; %%psi
qf=1525; %%gpm
qo=qf;
pp=16.4; %%psi
qp=1234; %%gpm
depr1=24.9; %%psi
delpr2=18.2; %%psi
PH=360; %%ft
lp=2.76*.264/(60)^2*14.5; %%gal/m*min^2/psi
k2=1.75*10^-4*60^1.67*14.5/264.172^1.67; %%psi/(gal/min)^1.67
k3=2.27*10^-2; %%(m^3/h)^.6/m^2
g=32.17; %%ft/s^2
rhoh20=62.4; %%lbs/ft^3
pf=rhoh20*g*PH*1.488*.000145; %%psi
delpo=pf-po; %%psi
icQ=qo;
icdelp=delpo;
xo=0;
x=@JW
jw=fsolve(JW,xo);
xspan=[0 50];
[x,y]=ode45(@odes,xspan,[icQ icdelp]);
function dydx=odes(jw,a1,k2)
dydx=zeros(2,1);
dydx(1)=-jw*a1;
dydx(2)= -k2*y(1)^1.67;
end
function F=JW(lp,qo,delpio,k3,y)
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40))-z;
end
x =
function_handle with value:
@JW
Error: File: JW.m Line: 3 Column: 50
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Error in mhlro (line 24)
jw=fsolve(JW,xo);
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40))-z;
1 2 1 2 1 2 3 4 3 21
Should be 0 by the end of the expression.
clc, clear
a1=28*7*400; %%ft^2
po=40.6; %%psi
delpio=9; %%psi
qf=1525; %%gpm
qo=qf;
pp=16.4; %%psi
qp=1234; %%gpm
depr1=24.9; %%psi
delpr2=18.2; %%psi
PH=360; %%ft
lp=2.76*.264/(60)^2*14.5; %%gal/m*min^2/psi
k2=1.75*10^-4*60^1.67*14.5/264.172^1.67; %%psi/(gal/min)^1.67
k3=2.27*10^-2; %%(m^3/h)^.6/m^2
g=32.17; %%ft/s^2
rhoh20=62.4; %%lbs/ft^3
pf=rhoh20*g*PH*1.488*.000145; %%psi
delpo=pf-po; %%psi
icQ=qo;
icdelp=delpo;
xo=0;
x=@JW
jw=fsolve(JW,xo);
xspan=[0 50];
[x,y]=ode45(@odes,xspan,[icQ icdelp]);
function dydx=odes(jw,a1,k2)
dydx=zeros(2,1);
dydx(1)=-jw*a1;
dydx(2)= -k2*y(1)^1.67;
end
function F=JW(lp,qo,delpio,k3,y)
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40)))-z;
end
>> JW
Not enough input arguments.
Error in JW (line 3)
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40)))-z;
This is the error I got.
As I wrote before:
jw=fsolve(JW,xo);
That would try to invoke JW with no inputs, expecting that it would return a function handle that could be solved against.
Remember that MATLAB evaluates all functions and expressions before passing them in to a function. With JW being a function, you are asking to invoke JW and to use whatever JW returns as the first parameter of fsolve() . The first parameter that reaches fsolve() must be a function handle: the first parameter in your call to fsolve() must be something that evaluates to a function handle. Including possibly an expression that builds a handle to an anonymous function.
xo=0;
x=@JW
jw=fsolve(x,xo);
Error in mhlro (line 24)
jw=fsolve(x,xo);
Caused by:
Failure in initial objective function evaluation.
FSOLVE cannot continue.
Is this what you mean by function handle?
Yes, that would be a function handle, and is closer to what you need. However your JW function is defined to take 5 inputs, but fsolve() is only going to pass one input to the function handle, so your JW function is not receiving enough inputs.
Reminder of what I posted earlier:
@(z)JW(lp,qo,delpio,k3,y)
That ignores z, so fsolve would not be able to propose different input vectors as solutions
function F=JW(lp,qo,delpio,k3,y,xo,z)
F=fsolve(@findjw,xo);
function F=findjw(z)
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40)))-z;
end
end
Undefined function or variable 'y'.
Error in mhlro (line 23)
x=JW(lp,qo,delpio,k3,y,xo,z)
This is the function i created.
'Y' is the answer coming out of the other function after ode45 which im trying to use for this one (JW function).
Also, im sure itll say z is undefined, right?

请先登录,再进行评论。

 采纳的回答

function F=JW(lp,qo,delpio,k3,y,z) %<- z is an INPUT
F=lp*(y(2)-qo*delpio/y(1)*exp(z/(k3*y(1)^0.40)))-z;
end

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Surrogate Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by