Fzero function parameter issues

5 Ansichten (letzte 30 Tage)
Ragini Ravichandren
Ragini Ravichandren am 9 Mär. 2021
Bearbeitet: Matt J am 9 Mär. 2021
So I'm trying to create a function that reads an expression, minimum, maximum, and figure(number). It includes the fzero function that finds the roots of the expression, between the minimum and maximum parameters. However, I keep geting an error message, even though I already made sure it works upon parsing the expression through, and chosing two random min and max values. Also, it says the function values at the interval endpoints must differ in sign.
function phaseline(f, ymin, ymax, fNum)
figure(fNum);
hold on;
t=[-1 1];
a=fzero(f, [ymin ymax]);
zeroSols = [];
zeroSols=unique(a);
for y=ymin:.1:ymax
if f(y) > 0
plot(y, f(y), 'b.', 'MarkerSize', 4);
elseif f(y) < 0
plot(y, f(y), 'r.', 'MarkerSize', 4);
end
for solT = 1:length(zeroSols)
plot(y, zeroSols(solT), 'k.', 'MarkerSize', 4);
end
end
hold off;
end
And here's the function:
f3=@(y) y.*(y-2)^2.*(y+4)^3;
phaseline(f3, -6, 4, 5);
Any help would be great!
  3 Kommentare
Ragini Ravichandren
Ragini Ravichandren am 9 Mär. 2021
Ah, OK, so does it not calculate roots?
Walter Roberson
Walter Roberson am 9 Mär. 2021
fzero is a numeric root finder that tries to find a single numeric root near the given starting point.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 9 Mär. 2021
Bearbeitet: Matt J am 9 Mär. 2021
If your function will always be a polynomial, you should just use roots().
To handle cases where your function is not a polynomial, you would need to first know if there will be a finite number of roots in the given interval, and the minimum separation between them.
  2 Kommentare
Ragini Ravichandren
Ragini Ravichandren am 9 Mär. 2021
Yeah I though so too. But then I wasn't sure how to calculate which roots were within a specific range.
Matt J
Matt J am 9 Mär. 2021
Bearbeitet: Matt J am 9 Mär. 2021
For a polynomial, it is straightforward to just calculate all the roots. Then discard the complex roots and the real roots that are outside the range.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Mär. 2021
In the general case, when given a "black box" function handle, it can be difficult to know if you got all the roots because of numeric round-off. Also when the function form is not constrained then it can be proven theoretically that there are functions for which calculus techniques do not help, that no amount of data about the function behavior at other points can help, that the only way to tell if something is a root is to test it individually.
The other extreme from that is that there are some function forms such as polynomials of degree 4 or less that there are exact solutions for, or ways to determine the solutions to arbitrary precision.
In the special case of continuous functions with it being known that the minimum separation between roots is delta, then you can divide up the range into segments that wide and test the value of the function at each segment endpoint, and search more carefully for exact zeros in any segment in which the endpoints are opposite signs.

Community Treasure Hunt

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

Start Hunting!

Translated by