Creating a Function That Finds Zeros

1 visualización (últimos 30 días)
Lavorizia Vaughn
Lavorizia Vaughn el 30 de Sept. de 2021
Comentada: Lavorizia Vaughn el 30 de Sept. de 2021
Hello,
I am trying to make a function 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 for 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
I believe the code i have (which is below) is terribly wrong, but its my best shot. I would appreciate some help. thanks.
function p = findmanyzeros(f, a, b, n)
x = linspace(a,b,n+1);
for k = 1:n
if f(x(k))*f(x(k+1))<0
p(k)=findzero(f);
break;
end
  11 comentarios
Walter Roberson
Walter Roberson el 30 de Sept. de 2021
You posted very similar code in another Question. In that other place, you noted that it warned about p = [p,z] and it gave you an error about findzero not being found. Is that the same issues as here?
MATLAB does not supply any findzero() function, so you would have to supply your own.
Hint: compare
N = 20;
x = 1:N;
z = [];
for K = 1 : N
if isprime(K); z = [z, K]; end
end
z
z = 1×8
2 3 5 7 11 13 17 19
to
N = 20;
x = 1:N;
P = false(1,N);
for K = 1 : N
if isprime(K); P(K) = true; end
end
x(P)
ans = 1×8
2 3 5 7 11 13 17 19
Notice that the second of these does not grow any arrays dynamically.
Lavorizia Vaughn
Lavorizia Vaughn el 30 de Sept. de 2021
yes i simply needed to rename a file to findzero thank you

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by