Using fsolve function, I want to get only real positive roots.

100 次查看(过去 30 天)
Hello folks,
I am using "fsolve" function to obtain roots in non-linear equations.
The roots have to be constrained with positive and real number.
How can I do that in fsolve"
Thanks
  3 个评论
Walter Roberson
Walter Roberson 2020-3-30
Where can i put this line?
After the point where you have used fsolve to obtain all of the roots and stored the roots in x
This particular syntax is for the case where the roots are scalars -- for equations in one variable. If you have multiple variables and you store the solutions as rows, then
x(all(imag(x)==0 & x>0,2), :)
will select only the rows in which all of the variables are positive reals.
Note that fsolve by itself will only ever return one root so you would need some strategy for finding multiple roots.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-3-28
You cannot constrain range to positive in fsolve().
You can add an option that will cause fsolve() to stop processing if it encounters imaginary or infinite or nan values, but this is not the same as a constraint, as fsolve() will stop at that point instead of trying again.
If you have a single equation in a single variable, you can use fzero(), which can constrain to positive -- pass in [eps(realmin) realmax] as the second parameter.
If you have a single equation in multiple variables, then one technique is to take the square of the equation and put it through fmincon() with fmincon told to use a lower bound of eps(realmin); minimizing the square of a real number is the same as trying to make the number zero.
  2 个评论
Jaemin Kim
Jaemin Kim 2020-3-30
Thank you for the answer Walter.
I have a multiple varialbes and multiple equations, like, variable vector x=(x(1),x(2),x(3),x(4)), and 4 equation. x(1)~x(4) should be positive and real number by physics theory, but fsolve give me complex number always.
Then, what way can I use for that?
Walter Roberson
Walter Roberson 2020-3-30
You have a few choices:
  1. Change your code that calculates the equations so that the code detects when there is a negative input or a complex result, and the code returns a large (non-zero!) value, and hope that steers fsolve() away from that area. However, this is risky: if you get the sign() of the non-zero value wrong, then fsolve might think that it has encountered a sign change that means a zero crossing, and might try to zoom in on where that happens. If this happens, Things Will Not Go Well. This method is not recommended, but it has the advantage of sticking with fsolve(). fsolve() is not able to handle bounds, period.
  2. If you have the symbolic toolbox, use vpasolve(). vpasolve() accepts an N x 2 array of lower and upper bound constraints. However, vpasolve() uses a modified netwon's method, and like all newton's method, it over-projects. If it over-projects into an area that is out of bounds, then vpasolve() will typically give up instead of adjusting the values to stay in bounds.
  3. Create a new expression, that is the sum of the squares of the individual equations, and use that sum-of-squares expression as the objective function for fmincon() with lower and upper bounds. If there are combinations of values that cannot be expressed as linear bounds but which would lead to complex results, then use nonlinear constraints to avoid those areas.
  4. If there are no combinations of values that cannot be expressed as linear bounds but which would lead to complex results, then you can build a sum-of-squares expression and use ga() or patternsearch() or other functions from the global optimization toolbox with it.
  5. If there are no combinations of values that cannot be expressed as upper/lower bounds but which would lead to complex results, then you can build a sum-of-squares expression and use John D'Errico's File Exchange contribution for doing fminsearch with bounds.
  6. If there are no combinations of values that cannot be expressed as upper/lower bounds but which would lead to complex results, then you can use the squares of your original functions (without summing) and gamultiobj()

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by