필터 지우기
필터 지우기

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

조회 수: 31 (최근 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에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by