How can I solve a cubic equation in which the coefficients are vector arrays?
9 views (last 30 days)
Show older comments
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
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
Accepted Answer
More Answers (1)
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
See Also
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!