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

3 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by