How can I solve a cubic equation in which the coefficients are vector arrays?

10 views (last 30 days)
Hi everyone,
My intention is to find the roots of a cubic function. My only problem is that each coefficient is a vector array (1x30). Also, I want to take only the positive roots (and bigger than 0.1) so that in the end also the resulting roots vector has a dimension of 1x30. How can I do that? I have tried by making a loop but there is still something wrong.
  2 Comments
Stephan
Stephan on 9 Jul 2018
Hi,
please share your code by inserting it, mark it and press the {}Code button, so that it looks like this:
A = Your code
Best regards
Stephan
letoppina
letoppina on 10 Jul 2018
Edited: letoppina on 10 Jul 2018
R = linspace(1000,100e4,30); %I create my array
for ii = 1:length(R)
%coefficients of my polyonmial equation (they are all in function of R(ii) so that they are all arrays)
a(ii) = CONSTANTS*R(ii);
b(ii) = CONSTANTS*R(ii);
c(ii) = CONSTANTS*R(ii);
d(ii) = CONSTANTS*R(ii);
end
%so my polynomial is defined by the coefficients:
p = [a(:) -b(:) -c(:) d(:)];
%i used roots in order to find the roots of my cubic equation
r = roots(p);
the problem is that since p is a matrix, i don't know how to delcare it when declaring the command "roots(p)". Of course, since it's a cubic equation, for each vector I will have 3 different roots. Then i will only select the one bigger than 0.1 by declaring:
V3 = r(r>0.1) %this should give a vecotr of the same length of R(ii) since i take only one root instead of three.

Sign in to comment.

Accepted Answer

letoppina
letoppina on 11 Jul 2018
So in the end I have figured it was just a way to declare my variables. Here below my working code:
R = linspace(1000,100e4,30); %I create my array
rts = zeros(3, length(R)); %inizialization of rts (matrix of the roots --> it,s a cubic equation, so three roots for each component of the arrays)
for ii = 1:length(R)
a(ii) = CONSTANTS*R(ii);
b(ii) = CONSTANTS*R(ii);
c(ii) = CONSTANTS*R(ii);
d(ii) = CONSTANTS*R(ii);
p = [a1(ii) -b1(ii) -c1(ii) d1(ii)];
rts(:,ii) = roots(p); %find the roots of the polynomial equation
end
coeff = rts.*(rts>0.1); %condition that I put in order to exctact only the parameters that I need

More Answers (1)

Shantanu Gontia
Shantanu Gontia on 10 Jul 2018
You can find the roots for the polynomial inside the loop itself. I'm assuming that only one root will satisfy the criteria of being positive and greater than 0.1. Here is a sample code implementing this
R = linspace(1000,100e4,30); %I create my array
% Declare roots array (initialize with zeros)
r = zeros(1, 30); % 30 roots
for ii = 1:length(R)
%coefficients of my polyonmial equation (they are all in function of R(ii) so that they are all arrays)
a(ii) = CONSTANT*R(ii);
b(ii) = CONSTANT*R(ii);
c(ii) = CONSTANT*R(ii);
d(ii) = CONSTANT*R(ii);
p = [a(ii) -b(ii) -c(ii) d(ii)];
rts = roots(p); % Get the roots
rts = rts(rts > 0.1); % Get the roots satisfying the criteria
r(ii) = rts(1); % Get one of the roots (if more than one)
end
  4 Comments
letoppina
letoppina on 10 Jul 2018
Edited: letoppina on 10 Jul 2018
ok, but in this way the dimension of p is not 30, I just get one row.
Torsten
Torsten on 11 Jul 2018
Edited: Torsten on 11 Jul 2018
I thought you are interested in the roots, not the polynomials that produce the roots. The roots are stored in "r" which is an 1x30 row vector.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!