Unique function problem with fzero

조회 수: 3 (최근 30일)
Nitzan
Nitzan 2013년 8월 16일
Hello, I'm trying to know all the roots of my function in a specific range, I know that "roots" function will work for my polynomial but I'm trying to do it with "fzero" also because I want to build an algorithm that will work for any function that the user will enter. The fzero function will give me only one root every time for my guess, so I'm trying to find all the roots in a loop (is it a correct way to do it?). The code is:
y=@(x)(x.^5-15*x.^4+85*x.^3-225*x.^2+274*x-120);
x_min=0;
x_max=5;
t=x_min;
dx=0.3;
r(1)=fzero(y,t);
while t<=x_max
t=t+dx;
a=fzero(y,t); %my guess number will rise every 0.3
r(end+1)=a;
end
disp(unique(r));
the correct roots need to be= 1 2 3 4 5, but in the command line the unique function didn't work and I see multiple values: 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
2.0000 2.0000 2.0000 3.0000 3.0000 3.0000
4.0000 4.0000 4.0000 4.0000 5.0000 5.0000
I want to see the roots without duplicates.
Thank you for your help :)

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2013년 8월 16일
편집: Andrei Bobrov 2013년 8월 16일
y=@(x)(x.^5-15*x.^4+85*x.^3-225*x.^2+274*x-120);
x00 = [0 5]; % your choice
n = 300; % your choice
xx = linspace(x00(1),x00(2),n);
ys = sign(y(xx));
x0 = xx(unique([strfind(ys(:).',[-1, 1]),...
strfind(ys(:).',[1, -1]),...
find(abs(ys) < eps(1000))]));
root1 = zeros(numel(x0),1);
for jj = 1:numel(x0)
root1(jj) = fzero(y,x0(jj));
end

Matt J
Matt J 2013년 8월 16일
편집: Matt J 2013년 8월 16일
I want to build an algorithm that will work for any function that the user will enter
There's no numerical algorithm that will work literally for "any function". What if the user enters the function sin(1/x) and a search range starting at or close to x=0? If the search interval begins at x=0, there will be infinite roots and you'll never find them all. Even if the lower end of the interval is not precisely at zero, the number and density of the roots can be arbitrarily large. You need to know how in advance how densely spaced the roots can be in an interval, in order to reliably find them all.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by