Unique function problem with fzero
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 :)
0 Kommentare
Antworten (2)
Andrei Bobrov
am 16 Aug. 2013
Bearbeitet: Andrei Bobrov
am 16 Aug. 2013
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
0 Kommentare
Matt J
am 16 Aug. 2013
Bearbeitet: Matt J
am 16 Aug. 2013
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!