필터 지우기
필터 지우기

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
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 CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by