fzero error ( complex function encountered)

Hi experts,
I am using fzero command to solve a nonlinear equation. The function part is as the following, where x is the variable to be solved, e is a variable value that passed to the function (e is a vector, I have a loop outside the nonlinear function for that), and others are parameters:
function F = solvex(x,e)
global alpha eta delta w r mu chi
F= x - w^(-alpha/(1-eta)).*(r+mu*(1-chi*exp(x))).^((alpha*eta-1)/(1-eta)).*exp(e);
end
I got the following error message under some parameter values and e values:
Exiting fzero: aborting search for an interval containing a sign change because complex function value encountered during search.
I checked some answers in mathworks and it seems that because the A^B operator in matlab.
I am looking forward to figuring out the solution. Thank you very much

 Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Feb. de 2019

1 voto

if chi is positive then 1-chi*exp(x) can become negative .Multiply by mu and the result can overwhelm r. You get a negative being raised to a power .
You need to pass fzero a range of values to search over.

6 comentarios

Xiaolu Zhu
Xiaolu Zhu el 4 de Feb. de 2019
Thank you!
Yes, parameters are all positive and the problem described by you could happen, and that is why there is an error.
I know x should be nonegative. I am wondering how can I set the range for x in fzero( I searched a lot but did not find a answer)?
Thanks again!
Walter Roberson
Walter Roberson el 4 de Feb. de 2019
x0 can be a vector with 2 elements that are lower bound and upper bound. Use eps(realmin) as the lower bound to say nonnegative .
Xiaolu Zhu
Xiaolu Zhu el 4 de Feb. de 2019
Thank you! I tried, and it showed "The function values at the interval endpoints must differ in sign". I think it suggests there is no root over my ideal range. Do you know is there anyway to deal with it? or I have to give up...Thanks again!
Walter Roberson
Walter Roberson el 4 de Feb. de 2019
Consider
442994.jpg
If you fzero() the function specifying [1 3] as the bounds, then the function is positive on both sides, and it happens there is no solution over that range.
If you fzero() the function specifying [1/4 2] as the bounds, then the function is negative on one side and positive on the other, and it is certain that fzero will be able to find at least one zero in that range. Likewise if you [2 3+3/4] as the bounds, then the function is positive on one side and negative on the other and it is certain that fzero will be able to find at least one zero in that range.
But if you specify [1/4 3+3/4] as the bounds, then the function is negative on both sides, and even though we can see that it has multiple solutions, the algorithm that fzero uses will not be able to find any solutions. fzero needs the value at the two endpoints you give to have different sign.
I recommend that you get rid of the global variables; see Here . And then I recommend that you move the calculations such as w^(-alpha/(1-eta)) out of the function: since they will not vary with x it makes no sense to compute them over and over again.
Do you have access to the symbolic toolbox? It can make it easier to find roots of an equation.
Xiaolu Zhu
Xiaolu Zhu el 4 de Feb. de 2019
Thank you for your suggestions! Your graph illustrates the rootfinding of fzero clearly. I have access to symbolic toolbox, and I will learn to use it immediately~
Walter Roberson
Walter Roberson el 4 de Feb. de 2019
The point about the symbolic toolbox is that you can take your existing function, and pass it in a symbolic x, and the expression you get out would already incorporate all of the various values, giving you something simpler to evaluate. You could matlabFunction() the resulting symbolic expression to get a function that fzero() could work with.
You could also potentially use the symbolic toolbox to find a zero, using vpasolve(). Or you could do some analytic work to find valid ranges for the solution that you could then later process numerically.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 4 de Feb. de 2019

Comentada:

el 4 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by