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

39 views (last 30 days)
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

Accepted Answer

Torsten
Torsten on 27 Nov 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.

More Answers (1)

Ninad Mauskar
Ninad Mauskar on 18 Jun 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

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!