hello,
i have to implement a MATLAB function findmanyzeros of the form function p = findmanyzeros(f, a, b, n, 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(xk1) have different signs, compute a zero using findzero
3. The output vector p should contain all the computed zeros
my code is as follows:
function p = findmanyzeros(f, a, b, n, tol)
n=n+1;
for k = 1:n
if f(xk)*f(xk-1)<0
findzero(f);
end
end
p
my code is wrong and i could really appriciate some help, thanks.

 Respuesta aceptada

Jan
Jan el 29 de Sept. de 2021
Editada: Jan el 30 de Sept. de 2021

0 votos

This homework question contains useful instructions already:
"Compute n+1 equidistant points xk , k=0,...,n, between a and b"
You've omitted this step. Use the command linspace() to solve it and assign the output to the variable x.
Then you can use f(x(k)) and f(x(k+1)).
I guess, that calling findzero needs the variable tol. Assign the output of this function to p(k).

4 comentarios

Walter Roberson
Walter Roberson el 29 de Sept. de 2021
However, the output is only to be the list of zeros, so if you assign to p(k) you need to collapse the list later to remove the places no zero was found.
Lavorizia Vaughn
Lavorizia Vaughn el 30 de Sept. de 2021
the new code i have is as follows:
function p = findz(f,a,b,n,tol)
x = linspace(a,b,n+1);
p = [];
for k = 2:length(x)
if f(x(k)) * f(x(k-1)) > 0
continue
else
z = findzero(f,x(k-1),x(k),tol);
p = [p,z];
end
end
disp(p)
end
i am getting the error Unrecognized function or variable 'findzero'.
Stephen23
Stephen23 el 30 de Sept. de 2021
"Unrecognized function or variable 'findzero'."
FINDZERO is not a MATLAB function. Did you mean FZERO?:
Lavorizia Vaughn
Lavorizia Vaughn el 30 de Sept. de 2021
Yes I had the file findzero named wrong. Thanks for the help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Eigenvalues en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Sept. de 2021

Comentada:

el 30 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by