how to use fzero function?

조회 수: 212 (최근 30일)
Duckyoon Go
Duckyoon Go 2021년 4월 7일
댓글: Walter Roberson 2021년 4월 8일
Hello~
I am trying to use fzero function and I am confused about the usage of the function
If I write code like following, it works
syms x
fzero('x.^2-2*x-5',2)
However, if I write code like below, it does not work
syms x
eqn=x.^2-2*x-5;
fzero('eqn', 2) or fzero(eqn,2)
Can anyone tell me the difference between two cases?
thank you

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 7일
syms x
fzero('x.^2-2*x-5',2)
The syms x is not having any effect there. What is happening is that you are encountering an undocumented syntax that creates an inline() object for some kinds of character vectors.
syms x
eqn=x.^2-2*x-5;
The syms x is having an effect there, so eqn is created as a symbolic expression.
fzero('eqn', 2)
That undocumented syntax... when you happen to pass in a character vector that looks like an identifier, the internal code turns it into a function handle; for example fzero('cos', 2) would turn into fzero(@cos,2) effectively. But there is no visible function named eqn so that will fail.
fzero(eqn,2)
fzero is not defined for symbolic expressions.
You need to choose between proceeding numerically or symbolically. Numerically you would use
eqn = @(x) x.^2-2*x-5
fzero(eqn,2)
and symbolically you would use
syms x
eqn=x.^2-2*x-5;
vpasolve(eqn,2)
or
syms x
eqn=x.^2-2*x-5;
solve(eqn)
Both of those symbolic processes will return two roots for a polynomial of degree 2.
There is no exact equivalent to fzero() for symbolic work.
  댓글 수: 1
Duckyoon Go
Duckyoon Go 2021년 4월 7일
Thank you so much Sir!
That is so informative and easy to understand!!!

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

추가 답변 (1개)

David Hill
David Hill 2021년 4월 7일
First, fzero finds roots of nonlinear functions and should not be symbolic. If you have a polynomial you should use roots() function. If you have a symbolic use solve() or vpasolve() functions. The documentation for fzero expects a function input that can handle an array of values. The easiest way to satisfy is with an anonymous function as input.
eqn=@(x)x.^2-2*x-5;
fzero(eqn,2);
But for a simple polynomial, better to use roots()
roots([1 -2 -5]);
  댓글 수: 2
Duckyoon Go
Duckyoon Go 2021년 4월 7일
Thank you Sir! I was not aware that fzero does not take symbolic function.
Walter Roberson
Walter Roberson 2021년 4월 8일
fzero() can be useful for polynomials if you want to find a root in a particular range.
... However, it might well be the case that it would be faster to use roots() and filter the results.

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

카테고리

Help CenterFile Exchange에서 Function Handles에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by