Fzero function parameter issues
5 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 9 Mar 2021
fzero is a numeric root finder that tries to find a single numeric root near the given starting point.
Accepted Answer
Matt J
on 9 Mar 2021
Edited: Matt J
on 9 Mar 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 Comments
More Answers (1)
Walter Roberson
on 9 Mar 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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!