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

30 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2014-11-27
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.

更多回答(1 个)

Ninad Mauskar
Ninad Mauskar 2019-6-18
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

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by