Using fsolve to determine roots of a simultaneous nonlinear equation.

4 ビュー (過去 30 日間)
lucas erickson
lucas erickson 2015 年 10 月 1 日
編集済み: John D'Errico 2015 年 10 月 1 日
  3 件のコメント
Star Strider
Star Strider 2015 年 10 月 1 日
Does it work?
Run your code to find out.
lucas erickson
lucas erickson 2015 年 10 月 1 日
It says unexpected matlab operator, not to sure what that means

サインインしてコメントする。

回答 (2 件)

John D'Errico
John D'Errico 2015 年 10 月 1 日
編集済み: John D'Errico 2015 年 10 月 1 日
Close. But no cigar. A good start though.
Effectively, the way you have posed it, fsolve will attempt to solve the equations
x^2 + 1 == 0
3*cos(x) == 0
Fsolve tries to set the expressions it sees equal to ZERO. But your problem has y in it, as an unknown. These are SIMULTANEOUS equations, two of them, in two unknowns. Here, the unknowns are x and y in those original equations.
The equations are better written in the form that fsolve wants to see them, as:
x^2 + 1 - y = 0
3*cos(x) - y = 0
fsolve will need a vector of starting values, with the two unknowns in a vector. So your function will look like this:
myfun = @(xy) [xy(1)^2 + 1 - xy(2); 3*cos(xy(1)) - xy(2)];
xy0 = [.5 .5]; % guess
[xy,fval,exitflag] = fsolve(myfun,xy0)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
xy =
0.91313 1.8338
fval =
1.2947e-08
-1.1871e-08
exitflag =
1
Fsolve thinks this was a success. And if you want to use an m-file function, it might look like this:
function obj = myfun(xy)
obj = [xy(1)^2 + 1 - xy(2); 3*cos(xy(1)) - xy(2)];
end
Personally, the function handle is far simpler to write. No file needed to be saved. Shorter.
Finally, we can solve this particular problem more simply yet, if we recognize that y can be eliminated. The pair of equations are equivalent to:
x^2 + 1 = 3*cos(x)
Solve this, using fzero most simply, since it is one equation in one unknown.
xfun = @(x) x.^2 + 1 - 3*cos(x);
x = fzero(xfun,1)
x =
0.91313
Now, recover y. See that we get the same value for each expression, as expected.
y = x^2 + 1
y =
1.8338
3*cos(x)
ans =
1.8338

Star Strider
Star Strider 2015 年 10 月 1 日
I’m not sure where you’re getting the ‘unexpected MATLAB operator’ error.
Running this code in a script file works:
myfun = @(x) [x.^2+1; 3*cos(x);];
x0=[5]
fsolve(myfun,x0)
although you don’t need the second semicolon (after the second expression) here: [x.^2+1; 3*cos(x);], that won’t throw the error.
  2 件のコメント
John D'Errico
John D'Errico 2015 年 10 月 1 日
But that is not actually the system of equations. Don't forget that y is an unknown.
Star Strider
Star Strider 2015 年 10 月 1 日
I interpret ‘y’ as the result rather than a parameter.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by