필터 지우기
필터 지우기

How to find zeros of a function?

조회 수: 995 (최근 30일)
Wenjie
Wenjie 2018년 12월 17일
댓글: DAVID 2024년 9월 20일 2:55
For instance,
x = -3.55:0.1:3.55;
y = x.^2 - 4;
Obviously, when x=2 or -2, y=0.
But I want to know how to use matlab to find zeros of a function y = f(x) when x is a matrix defined by the user like the above case.
  댓글 수: 3
Mark Sherstan
Mark Sherstan 2018년 12월 17일
As per documentaiton note:
x = fzero(fun,x0) tries to find a point x where fun(x) = 0. This solution is where fun(x) changes sign—fzero cannot find a root of a function such as x^2.
Akira Agata
Akira Agata 2018년 12월 17일
If your function is always polynomial, you can use roots function to do this task. Please look at the following help page.

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

답변 (3개)

Wenjie
Wenjie 2018년 12월 17일
I've found the solution. First, define the function in a separate file as
function y = fun(x)
y = x.^2-4;
end
Then use fzero to find x value that will give y=0.
x0 = fzero(@(x) fun(x), 3)
  댓글 수: 3
Nico
Nico 2024년 2월 25일
What does the @(x) and the 3 mean? Sorry I'm university student with no experience...
Dyuman Joshi
Dyuman Joshi 2024년 2월 25일
편집: Dyuman Joshi 2024년 2월 25일
@(x) is the syntax used to define a Function Handle / Anonymous Functions
The 3 is provided as an initial guess for fzero() to work with - see fzero for more information.

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


Alexander
Alexander 2024년 2월 25일
If it's not a function but meassured data I would go like this in a first try:
x = -3.55:0.0001:3.55; % assuming 10 kHz sample frequency
y = x.^2 - 4;
yS = sign(y);
dyS = diff(yS);
Z=find(dyS ~= 0);
x(Z)
  댓글 수: 1
DAVID
DAVID 2024년 9월 20일 2:55
This was really elegant, thanks!

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


Walter Roberson
Walter Roberson 2018년 12월 17일
x(y==0)
Note that this can miss an indefinite number of zeroes of a function if the x do not happen to sample at the right places . It also will not detect zero crossings between x values . You could make use of the results to get hints about zero crossings .

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by