Help with finding zero spots (fzero)

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.

回答(2 个)

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
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)

类别

帮助中心File Exchange 中查找有关 Optimization Toolbox 的更多信息

提问:

2012-9-21

Community Treasure Hunt

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

Start Hunting!

Translated by