Creating a Function That Finds Zeros

2 ビュー (過去 30 日間)
Lavorizia Vaughn
Lavorizia Vaughn 2021 年 9 月 30 日
コメント済み: Lavorizia Vaughn 2021 年 9 月 30 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 9 月 30 日
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 2021 年 9 月 30 日
yes i simply needed to rename a file to findzero thank you

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by