# Answer to Question #5583 in MatLAB | Mathematica | MathCAD | Maple for shayan

Question #5583

Write a Matlab script to determine the positive roots of the equation:

x^3-7x^2cos(3x)+3=0

x^3-7x^2cos(3x)+3=0

Expert's answer

Notice that a general function may have infinitely many roots even of a finite interval.

And MATLAB can find only one root at once starting from some initial value.

Therefore before writting the script we should localize the roots.

First look at the graph of the function

f(x) = x^3-7x^2cos(3x)+3.

Run the code:

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=30;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see in the plot that the positive roots of f are in the interval [0,7], and f(x)>0 for x>7.

Again rerun the script changing b=1.

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=1;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see that there are no roots on the interval [-0.1,1]

Rerun the script changing a=1, b=7.

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=7;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see 6 roots aproximately at points

1.8, 2.5, 3.8, 4.5, 6.1, 6.5

Now we can find all the by running the following command:

[fzero(f, 1.8), fzero(f, 2.5), fzero(f, 3.8), fzero(f, 4.5), fzero(f, 6.1), fzero(f, 6.5)]

This will give the list f roots

ans =

1.7047 2.4723 3.8722 4.4720 6.1225 6.4119

So to find all positive roots we need the following unique command:

[fzero(f, 1.8), fzero(f, 2.5), fzero(f, 3.8), fzero(f, 4.5), fzero(f, 6.1), fzero(f, 6.5)]

or the following 6 commands:

fzero(f, 1.8)

fzero(f, 2.5)

fzero(f, 3.8)

fzero(f, 4.5)

fzero(f, 6.1)

fzero(f, 6.5)

And MATLAB can find only one root at once starting from some initial value.

Therefore before writting the script we should localize the roots.

First look at the graph of the function

f(x) = x^3-7x^2cos(3x)+3.

Run the code:

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=30;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see in the plot that the positive roots of f are in the interval [0,7], and f(x)>0 for x>7.

Again rerun the script changing b=1.

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=1;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see that there are no roots on the interval [-0.1,1]

Rerun the script changing a=1, b=7.

f=@(x) x.^3-7*x.^2.*cos(3*x)+3;

a=-0.1;

b=7;

x=[a:0.01:b];

hold off

plot(x,f(x))

hold on

plot([a,b],[0,0],'r')

We will see 6 roots aproximately at points

1.8, 2.5, 3.8, 4.5, 6.1, 6.5

Now we can find all the by running the following command:

[fzero(f, 1.8), fzero(f, 2.5), fzero(f, 3.8), fzero(f, 4.5), fzero(f, 6.1), fzero(f, 6.5)]

This will give the list f roots

ans =

1.7047 2.4723 3.8722 4.4720 6.1225 6.4119

So to find all positive roots we need the following unique command:

[fzero(f, 1.8), fzero(f, 2.5), fzero(f, 3.8), fzero(f, 4.5), fzero(f, 6.1), fzero(f, 6.5)]

or the following 6 commands:

fzero(f, 1.8)

fzero(f, 2.5)

fzero(f, 3.8)

fzero(f, 4.5)

fzero(f, 6.1)

fzero(f, 6.5)

## Comments

## Leave a comment