Help with finding zero spots (fzero)
Show older comments
Hi, I'm all new to Matlab and I'm supposed to use this function to find all 3 zero spots.
.m (my file where the function can be found)
function fval = f(x)
% FVAL = F(X), compute the value of a test function in x
fval = exp(-x) - exp(-2*x) + 0.05*x - 0.25;
So obviously I write "type f" to read my function but then I try to do like fzero ('f', 0) and I get the ans 0.4347 and I assume that's 1 of my 3 zero spots but how to find the other 2?
Thanks in advance.
Answers (2)
Andrei Bobrov
on 21 Sep 2012
x = 0:.001:5;
ff = @(x)exp(-x) - exp(-2*x) + 0.05*x - 0.25;
p = ff(x);
[b,b] = findpeaks(p);
[c,c] = findpeaks(-p);
x0 = x([1,sort([b(:)',c(:)']),end]);
xroots = zeros(numel(x0)-1,1);
for jj = 1:numel(x0)-1
xroots(jj) = fzero(ff,x0([jj,jj+1]));
end
Matt Fig
on 21 Sep 2012
Here is a solution that uses your m-file function.
Roots(1) = fzero(@f,0);
Roots(2) = fzero(@f,2);
Roots(4) = fzero(@f,4)
Categories
Find more on Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!