Home > OS >  Matlab how to define coefficients in a matrix to compute the roots of a polynomial
Matlab how to define coefficients in a matrix to compute the roots of a polynomial

Time:12-16

I want to solve the coefficients of this equation in matlab in a matrix. E 1/2x^3 - 1/4X^5 -1/4 = 0, where E = 1 and 1/16 So far have I got this. But it doesn't work.

if E < 1/8
        coeffs = [E   1/2 - 1/4 1 0 -1/4];
        sols = roots(coeffs);
        sols = sort(sols,'descend');
        Y = sols(1);
        Z = sols(2);
    else E > 1/8;
        coeffs = [E   1/2 - 1/4 2 1 0 -1/4];
        sols = roots(coeffs);
        sols = sort(sols,'descend');
        Y = sols(3);
        Z = sols(4);
    end

(Where Y and Z are limits of an integral I'm computing later on in the code.)

Where am I going wrong with this? I don't know how to get the coeffs=[] line right.

CodePudding user response:

1.- coeffs is a MATLAB command.

You are not using it correctly :

  • operator * omission;

  • mixing x and X in the polynomial expression not collecting the result of coeffs in a variable

  • It's wise not using reserved words or command words as variables despite MATLAB may likely solve ambiguity correctly.

  • I also like keeping small characters for scalars and 1xN vectors and capital characters for arrays N*M (* ..) N>1 M>1

  • without collecting the output of coeffs the only way to use the resulting coefficients would be to use the system variable ans that always collects the most recent result.

So

2.- Instead of

coeffs = [E   1/2 - 1/4 1 0 -1/4];

try

e=1/16
syms x
c1=coeffs(e 1/2*x^3-1/4*x^5-1/4,'All')

without the 'All' option coeffs chains all non-zero resulting coefficients, and the resulting polynomial may not the the actual input.

3.- Your code is now as follows.

N=6;  % precision : amount digits   amount decimals

e=1/16;
syms x
c1=coeffs(e 1/2*x^3-1/4*x^5-1/4,'All')
s1 = round(roots(c1),N);
s1 = sort(s1,'descend')
        
if e < 1/8
        Y = s1(1)
        Z = s1(2)
        
    else e > 1/8
        Y = s1(3)
        Z = s1(4)
        
end

The result for e=1/16 is :

c1 =
 
[-1/4, 0, 1/2, 0, 0, -3/16]
 
 
s1 =
 
- 0.31028   0.613148i
- 0.31028 - 0.613148i
               1.2823
             0.830187
            -1.491928

And the result for e=1 is :

c1 =
 
[-1/4, 0, 1/2, 0, 0, 3/4]
 
 
s1 =
 
  0.424835   0.930891i
  0.424835 - 0.930891i
- 1.243788   0.449783i
- 1.243788 - 0.449783i
              1.637907
 
 
Y =
 
- 1.243788   0.449783i
 
 
Z =
 
- 1.243788 - 0.449783i

Comment : N=6 sets precision total amount of digits including left dot digits and right side dot decimals.

Otherwise no matter how format is set roots forces long precision, and there may be no need to drag along 20 decimals all the way.

CodePudding user response:

I assume you are trying to compute the roots of the following polynomial: (-1/4)x^5 (0)x^4 (E 1/2)x^3 (0)x^2 (0)x (-1/4) = 0 This is important to clarify. If my assumption is correct, coeffs = [(-1/4) 0 (E 1/2) 0 0 (-1/4)]

  • Related