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

7 ビュー (過去 30 日間)
letoppina
letoppina 2018 年 7 月 9 日
回答済み: letoppina 2018 年 7 月 11 日
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 件のコメント
Stephan
Stephan 2018 年 7 月 9 日
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 2018 年 7 月 10 日
編集済み: letoppina 2018 年 7 月 10 日
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.

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

採用された回答

letoppina
letoppina 2018 年 7 月 11 日
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

その他の回答 (1 件)

Shantanu Gontia
Shantanu Gontia 2018 年 7 月 10 日
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 件のコメント
letoppina
letoppina 2018 年 7 月 10 日
編集済み: letoppina 2018 年 7 月 10 日
ok, but in this way the dimension of p is not 30, I just get one row.
Torsten
Torsten 2018 年 7 月 11 日
編集済み: Torsten 2018 年 7 月 11 日
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.

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

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by