How to retain only the positive root of a quadratic equation?
33 ビュー (過去 30 日間)
古いコメントを表示
I was wondering if there is any Matlab function that would allow me to retain only the positive root of a quadratic equation. I want to use that function along with the 'roots' function to solve n number of quadratic equations to get n number of positive roots.
x=zeros(1,n)
for i=1:n
x(i)=function(roots([a(i) b(i) c(i)]))
end
0 件のコメント
採用された回答
Guillaume
2014 年 8 月 15 日
To only get the positive elements of array a:
>>a = [-2.5 0 2.5 -3.5 1];
>>apos = a(a>=0)
apos =
0 2.5000 1.0000
4 件のコメント
Walter Roberson
2016 年 9 月 19 日
Joel Matthew commented on the Answer:
did you read the question ? he is asking for finding the positive root not an element in an array
Walter Roberson
2016 年 9 月 19 日
Joel,
roots() returns a vector of values. roots() is an already known step. The question is therefore to write a function which examines a vector of values and returns the positive (real) values from the vector. So where the original poster had written
x=zeros(1,n)
for i=1:n
x(i)=function(roots([a(i) b(i) c(i)]))
end
using "function" as a stand-in for the name of the function to do the desired selection, that could be written as
x=zeros(1,n)
for i=1:n
x(i)=Extract_positive(roots([a(i) b(i) c(i)]))
end
function apos = Extract_positive(a)
apos = a(a > 0);
which is code that only has to worry about elements of an array.
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!