Finding zeros of an equation

How do you find all zeros of an equation?
I have this problem and I'm suppose to find all the zeros of 3x^3 - 12x^2 - 33x + 80 over the range -10< x <10. I graphed it on my calculator and it clearly crosses the x-axis 3 times over -10 < x < 10.
When I do the problem in matlab it only gives me one of the zeros?
Any suggestions on how to find all the zeros?
Thank you.

1 件のコメント

Steven
Steven 2012 年 3 月 31 日
Here is what I have so far:
function yzero = findzeros(range)
fun=@testfun;
[yzero,value]=fzero(fun,range);
%
function fx = testfun(x)
fx=(3.*x.^3)-(12.*x.^2)-(33.*x)+80;
end
end

サインインしてコメントする。

回答 (4 件)

bym
bym 2012 年 3 月 31 日

0 投票

doc roots

1 件のコメント

Steven
Steven 2012 年 3 月 31 日
I don't want to roots. I want to solve for all the zeros

サインインしてコメントする。

Wayne King
Wayne King 2012 年 3 月 31 日

0 投票

C = [3 -12 -33 80];
Pzeros = roots(C);
Pzeros

2 件のコメント

Steven
Steven 2012 年 3 月 31 日
I'm asking to find all the zeros. which are when the points cross the x-axis at x=0. You're giving me the roots.
Jared Jones
Jared Jones 2016 年 3 月 14 日
the roots of the function are all the points where your function equals zero. fzero is mainly used for nonlinear functions.

サインインしてコメントする。

Wayne King
Wayne King 2012 年 3 月 31 日

0 投票

Steven, In this case the roots are exactly the points you are looking for.
Please convince yourself:
C = [3 -12 -33 80];
Pzeros = roots(C);
x = -10:0.01:10;
y = 3*x.^3-12*x.^2-33*x+80;
plot(x,y)
grid on;
hold on
plot(Pzeros,zeros(length(Pzeros)),'r*','markersize',10);

3 件のコメント

Steven
Steven 2012 年 3 月 31 日
Ok, thats how it looks on my graph. But how would I do this in terms of using a primary function and subfunction to compute the zeros and display the answers?
Wayne King
Wayne King 2012 年 3 月 31 日
Write the primary function to accept the coefficients of the polynomial like the C vector above. Use roots() as a subfunction to find the zeros and then check the sign of the polynomial to the left and right of the zero, then you'll know whether it crosses the x-axis.
Steven
Steven 2012 年 3 月 31 日
Here is what I have so far:
function yzero = findzeros(range)
fun=@testfun;
[yzero,value]=fzero(fun,range);
%
function fx = testfun(x)
fx=(3.*x.^3)-(12.*x.^2)-(33.*x)+80;
end
end
Then I typed this into the command window:
yzero = findzeros([-10, 10])
yzero =
5.1309
All I get is 1 zero. How do I get it to display all of them?

サインインしてコメントする。

bym
bym 2012 年 4 月 1 日

0 投票

if you insist on using a subfunction , consider the following:
function x = myroots(y)
% y is an integer range to find the roots in
n = y(1):y(2);
for k = 1:numel(n)
fx=@(x)(3.*x.^3)-(12.*x.^2)-(33.*x)+80;
r(k) = fzero(fx,k);
end
separate(r)
function a = separate(r)
a = sort(r(abs(diff(r))>10*eps));
end
end
however it is sensitive to range given. Works for range[-10,10], other ranges may give different results

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 4 月 1 日
That's an example of a function that is a nested function but not a subfunction. To be a subfunction there would have to be a shared variable.

サインインしてコメントする。

質問済み:

2012 年 3 月 31 日

コメント済み:

2016 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by