필터 지우기
필터 지우기

Getting 3 zeros from a function using fzero

조회 수: 4 (최근 30일)
D
D 2011년 10월 15일
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]);

채택된 답변

Ashikur
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
D
D 2011년 10월 16일
Thanks, I was hoping to find a way without for loops, but if it works, it works. I went with the following to get the 3 zeros stored in a single array.
for i = -5:.02:5
a = fzero(my_function,i);
if (b(end) == 0) && (b(end) ~= a)
b(length(b)) = a;
elseif (b(end) ~= a) && (abs((b(end) - a)) > .1)
b(length(b) + 1) = a;
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
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
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
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 CenterFile Exchange에서 Function Handles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by