Borrar filtros
Borrar filtros

How to make sure that fsolve gives only positive values as the solution

30 visualizaciones (últimos 30 días)
I have L equations to solve. I am using the following code to solve :
fh = matlabFunction([equn(1:L)],'vars',{[lam(1:L)]});
options = optimset('Display','off','TolFun',eps);
[solu_fsolve,value] = fsolve(fh, [1:L] ,options ) ;
But, i am getting -ve answer as a solution of fsolve. But, i am expecting + ve solutions only. Is there anyway to make sure that fsolve gives only +ve solutions?
Please answer. Thanks in advance.
With best regards, kalpana

Respuesta aceptada

Torsten
Torsten el 27 de Nov. de 2014
Square your unknowns in the functions you supply to fsolve.
E.g. if you want to solve
x(1)-5=0,
solve instead
y(1)^2-5=0.
After you get the solution y(1) from fsolve (in this case sqrt(5)), you only have to square it to get x(1) (in this case 5) - the solution of your original untransformed problem.
Best wishes
Torsten.

Más respuestas (1)

Ninad Mauskar
Ninad Mauskar el 18 de Jun. de 2019
Hi Kalpana,
Add a couple of conditional clauses before the return line.
For example, I am looking to solve for x such that array1[0,0] which is determined using x = certain fixed value.
I added a conditional statement before the return line which states:
if x < 0:
array1[0,0] = 0
else:
array1[0,0] is calculated using a specified formula which takes x as an input.
I have a return line after the above lines:
return array1[0,0] - certain value
When i use fsolve now, fsolve only gives be positive values of x.
Hope this helps!
Cheers,
Ninad

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by