how many number of values when added becomes equal to B(Known value)

I have to find the X value for which \sum_(i=1)^X (i * a_i ) >= B
or ( 1*a_1 + 2*a_2 + --- + X*a_x >= B ) where a_i & B are known.
For finding the X value; is there any explicit formula? If anyone knows, please answer.
Thank you very much, kalpana

 採用された回答

Roger Stafford
Roger Stafford 2014 年 7 月 28 日

1 投票

Assume 'a' is a row vector.
x = 1:size(a,2);
X = x(cumsum(x.*a)>=B);

5 件のコメント

kalpana
kalpana 2014 年 7 月 28 日
Thank you for your answer. But, i am looking for any explicit formula for finding X. If you know, please answer.
Roger Stafford
Roger Stafford 2014 年 7 月 28 日
The values in that final X are the ones for which your inequality holds. It is as "explicit" as it is possible to be, unless perhaps you are looking for, say, the first X for which that is true in which case you would choose the first element in X.
kalpana
kalpana 2014 年 7 月 28 日
In equation ( 1*a_1 + 2*a_2 + --- + X*a_x >= B ) , i have to find X ( of course, the first X value that satisfies the equation ). Is there any formula for finding X ? ( why i want this is, i am trying to reduce the number of flops for finding X. So, if there is expression for finding X, then i could reduce no. of flops ).
Roger Stafford
Roger Stafford 2014 年 7 月 28 日
I've already told you. The first X that satisfies your inequality is the first element in my X. It is of course X(1).
kalpana
kalpana 2014 年 7 月 30 日
Thank you for your answer sir.

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

その他の回答 (1 件)

ES
ES 2014 年 7 月 28 日

0 投票

Immediate Response would be, you can run a loop and sum it up and check if the sum is greater than or equal to B at every iteration and break out of loop when it is satisfied.
A more measured approach will be, to use summation by parts. For example if length of a_i is 100, you could first check if summation(a_i*X) for X=1:50. If B is smaller, then you can run the summation for X=1:75. If B is greater, then you can run for X=1:25 and proceed so on.. But for this to work, a_i should be monotonically increasing.
Analysis of a_i (if it is a series or so) could also be useful.

1 件のコメント

kalpana
kalpana 2014 年 7 月 28 日
Thank you for answering. Your suggestion is useful.
Can we have some explicit formula for this if a_i's random numbers.--kalpana

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2014 年 7 月 28 日

コメント済み:

2014 年 7 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by