Getting 3 zeros from a function using fzero
4 次查看(过去 30 天)
显示 更早的评论
I'm trying to collect the 3 zeros over the interval [-5,5] for the function in my code below. I can't seem to figure out how to used the fzero function so I can collect all three zero values and store them.
my_function = @(x) (-x^2 - 5*x - 3 + e^x);
a = fzero(my_function,[-5 5]);
0 个评论
采纳的回答
Ashikur
2011-10-16
Try this
my_function = @(x) (-x^2 - 5*x - 3 + exp(x));
for i = -5:.01:5
a = fzero(my_function,i)
ind = int8((5.01+i)*100)
b(ind) = a;
end
result = unique(b)
Note: fzero outputs only one value, and the limit must evaluate the function +Ve and -Ve in the limiting edges. So you have to try a loop.
更多回答(1 个)
Fangjun Jiang
2011-10-16
Are there three zeros? I only get one. Use exp(x), instead of e^x
>> my_function = @(x) (-x^2 - 5*x - 3 + exp(x));
a = fzero(my_function,[-5 5])
a =
-4.306510588580705
3 个评论
Walter Roberson
2011-10-16
There are 3 zeros,
-.5426539024,
3.482467600,
-4.306510589
Remember, fzero stops when it finds a single zero.
Fangjun Jiang
2011-10-16
That is good to know. I didn't spend time on it. I thought the OP could get there after passing the e^x error.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Handles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!