Help with a vector
3 次查看(过去 30 天)
显示 更早的评论
Hello. I have come up with the following code:
function p = findmanyzeros(f, a, b, n, tol)
x = a + (b-a)*(0:n)/n;
fx = f(x);
p = [];
for i = 1:n
if sign(fx(i)) ~= sign(fx(i+1))
p(end+1) = findzero(f, x(i), x(i+1), tol);
end
end
My instructions were to Implement a MATLAB function findmanyzeros of the form function p=findmanyzeros(f, a, b, n, tol)
which finds zeros in the interval [a, b] using the following strategy:
1. Compute n+1 equidistant points xk, k=0,...,n, between a and b
2. For k = 1,...,n, if f(xk) and f(xk−1) have different signs, compute a zero using findzero
3. The output vector p should contain all the computed zeros
5 个评论
采纳的回答
Cris LaPierre
2021-9-30
编辑:Cris LaPierre
2021-9-30
You have not defined a variable fx inside your findzeros function. Do you mean to use f?
You have created a recursive function but you have not defined an exit criteria. Since sign(f(i) always is the same as sign(f(i+1)), p is never assigned a value, and your result is an empty vector.
f= @(x) cos(x)-x;
f(1:10)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Construction 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!