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

44 ビュー (過去 30 日間)
kalpana
kalpana 2014 年 11 月 27 日
回答済み: Ninad Mauskar 2019 年 6 月 18 日
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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by