Creating a Function That Finds Zeros

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 Comments

for k = 0:n
n=n+1;
x = x(k);
end
I am having difficulty figuring out what the intent of that section was.
i was trying to compute n+1 points as described in 1.
i have updated my code
for k = 0:n
okay, that is a range of n+1 values.
Note that n will be recorded at the time the for loop is started, and changes to n after that will not affect how many iterations the loop does.
n=n+1;
Each of the n+1 times through the loop, you make n larger by 1 (which does not affect how many loops are done.) At the end, n will have become one more than twice as large as it was originally.
x = x(k);
x has not been defined yet, so that is an error. x has certainly not been defined as a function of one parameter, or as an array to be indexed by k. If it were an array, then you would have a problem because k starts at 0, and indexing from 0 is not permitted. If it were an array, then because you are assigning to the complete variable x, after the first x(k) was extracted, the entire array x would be overwritten with the single value extracted. For example if x started as [3 1 4 2] and k were 1, then x(1) would be 3, and that 3 would be written over top of the entire variable x, making x be just 3 . And then the next iteration, x would no longer have multiple items, so x(k) on the right hand side would be an index-out-of-range.
The equidistant points are supposed to be in the range a to b, but nothing in that loop uses a or b.
for k = 1:n
if f(x(k))*f(x(k-1))<0
k starts at 1. x(k) is x(1) which is okay so f(x(k)) is okay.
k starts at 1. x(k-1) is x(0) which is not permitted so f(x(k-1)) is a problem.
Hint: x is n+1 elements long so the last iteration of your for loop should be using x(n+1) as one of the endpoints.
ive made an update
ok, suppose it finds a zero when k=1 and another when k=5. Then p(1) will be explicitly assigned to, p(2), p(3), p(4) will not be explicitly assigned to, p(5) will be explicitly assigned to. Any location not explicitly assigned to will be 0. So now how do you deal with the fact that you are only to return the zeros of the function, so in this case a vector of length 2?
No, you cannot just select the non-zero elements, since one of the solutions might have been at 0.
Next question: what if one of the solutions is exactly at a boundary?
i have new code but it still wont run. it 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
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.
yes i simply needed to rename a file to findzero thank you

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Products

Tags

Asked:

on 30 Sep 2021

Commented:

on 30 Sep 2021

Community Treasure Hunt

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

Start Hunting!