Error using fzero " FUN must be a function, a valid string expression, or an inline function object"

조회 수: 14 (최근 30일)
I am getting errors while using fsolve or fzero in the following code:
U = 1;
while U < 20
eq = @(q) 2.*(1-cos(2.*pi.*q));
hq = @(q,n0) ((eq(q)).^2+2.*U.*n0.*(eq(q))).^0.5;
y = @(q,n0) (((eq(q))+(U.*n0))./hq(q,n0))-1;
a = -0.5;
c = -0.01;
v = @(n0) (integral(@(q) y(q,n0),a,c));
cv =@(n0) n0+(0.5*v(n0))-1;
n0 = fzero(cv(n0),0.1);
plot(U,n0)
hold on
U = U + 1;
end
Can anyone please help me out?

채택된 답변

Matt J
Matt J 2015년 2월 25일
n0 = fzero(cv,0.1);
  댓글 수: 4
Matt J
Matt J 2015년 2월 25일
편집: Matt J 2015년 2월 25일
Ah well, why not do instead
n=fminbnd(@(n0) abs(cv(n0)), 0,1 )

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2015년 2월 25일
편집: John D'Errico 2015년 2월 25일
cv is a function handle, i.e., perfectly valid for fzero to work on. It is a function that takes one argument, and returns an output that can be minimized.
cv(n0) is a variable. A number. Well, it would be, if n0 was defined. It may be so in your workspace, as otherwise, you would have gotten a different error before fzero was even entered.
Regardless, cv(n0) is NOT one of the valid things fzero can minimize.
There IS a difference. While you may THINK of cv as being a function of n0, when MATLAB sees the expression cv(n0), it looks around and says to itself, yep, I found n0. I found cv. It will evaluate that expression, then pass it in as an argument to a function called fzero.
Only then does fzero take control. It sees what was passed in, in this case, the variable that contains the result of cv(n0), where again, n0 MUST have been defined in your workspace, else you would have gotten a different error. That value is indeed NOT a function. Just a number. So fzero gets upset and tells you:
" FUN must be a function, a valid string expression, or an inline function object"
So the solution is simple. Remove the (n0) in your calls to fzero or to fsolve, like this:
n0 = fzero(cv,0.1);
  댓글 수: 19
INFOZOU
INFOZOU 2017년 8월 20일
편집: INFOZOU 2017년 8월 20일
Hello
  • I have changed the objfun=-sum(objvec) because I should to maximize this function
  • I have added the lower bounds zeros(size(obj)) to obtain only positive results of X
  • I have changed the Non linear constraints by the following expression: Qr(nn,1,ii,jj)=min(sum(QA(:,nn,ii,jj)),DE(nn,1,ii,jj)); Qr(nn,2,ii,jj)=min(sum(QA(:,nn,ii,jj))-Qr(nn,1,ii,jj),DE(nn,2,ii,jj));
  • I want to verify if there are a difference in the constraints between: a*x<b and x*a<b and how I change the program for the second case.
I have obtained the following result: > In backsolveSys In solveKKTsystem In computeTrialStep In barrier In fmincon (line 798) In masterFINALMODEL2 (line 249) Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.590792e-16.
Solver stopped prematurely.
fmincon stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 1000000 (the selected value).
exitflag =
0
fval =
-1.1377e+23
I have tried using the optimisation tool also but there are the same problem and I have obtained the attached results.
<<
>>
Thank you very much for your cooperation

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by