Fzero function parameter issues

1 次查看(过去 30 天)
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 个评论
Ragini Ravichandren
Ah, OK, so does it not calculate roots?
Walter Roberson
Walter Roberson 2021-3-9
fzero is a numeric root finder that tries to find a single numeric root near the given starting point.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2021-3-9
编辑:Matt J 2021-3-9
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 个评论
Ragini Ravichandren
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 2021-3-9
编辑:Matt J 2021-3-9
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-3-9
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.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by