solve vs. fsolve

조회 수: 37 (최근 30일)
byungkeuk cho
byungkeuk cho 2020년 5월 27일
답변: Walter Roberson 2020년 5월 27일
I am trying to solve non linear equations with solve or fsolve.
But I found difficulties in using them.
for example,
i would like to solve the equation below.
a^2 = 4
then, i can get the answer with the script below.
syms a;
eqn1 = a^2 == 4;
[a1] = solve(eqn1);
then, it gives me a = -2, 2
but when I used fsolve, it gives me only one value according to initial point.
this is the script.
fun = @test;
x0 = [1];
[a2] = fsolve(fun,x0)
function F = test(x)
F(1) = -x(1)^2 + 4;
end
if I set x0 as [-1], then it gives me another value but still only one value.
How can I get the all values with fsolve?

채택된 답변

Star Strider
Star Strider 2020년 5월 27일
Give fsolve different starting points, one positive and one negative:
eqn1 = @(x) x.^2 - 4;
for k = 1:2
a2(k) = fsolve(eqn1, 5*(-1)^k);
end
a2
producing:
a2 =
-2 2
  댓글 수: 2
byungkeuk cho
byungkeuk cho 2020년 5월 27일
Thank you for your quick response.
But if I have no idea on "how many staring points needed" or "what points they should be" due to complicated equations, then how can I solve it?
Star Strider
Star Strider 2020년 5월 27일
My pleasure.
The easiest way would be to plot it, at least to find the real roots. If some or all of the roots are complex, this becomes more difficult, however fsolve will take complex initial estimates and will use them to return complex roots. In that situation, it will be necessary to experiment.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 27일
Sometimes plotting and finding intervals by eye is not productive.
In the general case, theory says that it is impossible to construct a function that will find all the zeros of every arbitrary function.
In practice, sometimes you can do good enough (your function might be well-enough behaved)

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by