generating a sequence of numbers

5 ビュー (過去 30 日間)
ektor
ektor 2019 年 5 月 11 日
編集済み: John D'Errico 2019 年 5 月 11 日
Dear all,
I want to generate the following sequence:
L1=P
L2=a1+2*F*P
L3=a2+2*F*a1+3*F^2*P
L4=a3+2*F*a2+3*F^2*a1+4*F^3*P
L5=....
and then take the sum of these Ls.
P,F and the 'a's are numbers.
Is there a fast way to do that?
It is quite challenging

採用された回答

John D'Errico
John D'Errico 2019 年 5 月 11 日
編集済み: John D'Errico 2019 年 5 月 11 日
Not if a1, a2, a3, a4, etc., are separate variables. If that is what you have done, then it is time to learn to program in ways that use vectors.
But, if you have a vector that contains the values a1, a2, a3,..., then yes, it is possible. Will it be faster than simply using loops? Possibly not. Loops can be pretty fast. So, first i'll make up some numbers, just to test things.
n = 1000;
P = pi;
F = 0.98;
a = rand(1,n);
Now, write it as a loop. I'd want to recheck carefully that I got the terms correct here, but they seem right:
tic
Lsum = P;
for i = 2:n+1
Li = a(i-1);
for j = 2:i-1
Li = Li + j*F.^(j-1)*a(i-j);
end
Lsum = Lsum + Li + i*F.^(i-1)*P;
end
toc
Elapsed time is 0.009333 seconds.
For n=1000, is that really that slow? Do you really care if you can make it run twice as fast? In fact, even for n=10000, it only took my computer 0.1 seconds to do the full computation.
Never spend the programming time to try to make something elegant merely because you want it to be as efficient as possible. If you are doing the above computation millions of times and it can vbe seen to be a bottleneck, ONLY THEN do you spend the time to make it more efficient. Don't pre-optimize your code. Programmer time is worth far more than a few extra CPU cycles.
  2 件のコメント
ektor
ektor 2019 年 5 月 11 日
Is the code correct? If it is we are fine!!
John D'Errico
John D'Errico 2019 年 5 月 11 日
編集済み: John D'Errico 2019 年 5 月 11 日
I would not trust me. I'd only trust code that I verified. Even my own code. The simple way to verify the code is to initialize F, P, and a as symbolic objects.
A quick test shows it was close, but was not completely correct. I've now fixed the code I show in my answer to reflect the changes.
syms P F
n = 5;
a = sym('a',[1 5]);
Lsum = P;
for i = 2:n+1
Li = a(i-1);
for j = 2:i-1
Li = Li + j*F.^(j-1)*a(i-j);
end
Li
Lsum = Lsum + Li + i*F.^(i-1)*P;
end
Lsum
That yields the output:
Li =
a1
Li =
a2 + 2*F*a1
Li =
3*a1*F^2 + 2*a2*F + a3
Li =
4*a1*F^3 + 3*a2*F^2 + 2*a3*F + a4
Li =
5*a1*F^4 + 4*a2*F^3 + 3*a3*F^2 + 2*a4*F + a5
Lsum
Lsum =
P + a1 + a2 + a3 + a4 + a5 + 2*F*a1 + 2*F*a2 + 2*F*a3 + 2*F*a4 + 3*F^2*P + 4*F^3*P + 5*F^4*P + 6*F^5*P + 3*F^2*a1 + 3*F^2*a2 + 4*F^3*a1 + 3*F^2*a3 + 4*F^3*a2 + 5*F^4*a1 + 2*F*P
And that seems to be correct.
As you can see, all of the terms Li is now correct, although the way I wrote the loop, I added the last term into the sum at the end. I could probably have written it to be slightly simpler, by defining the first element of a as 0. But then I would need to be careful in how I index the vector a.
Of course, if you want only the total sum, then that code is completely correct. If for some reason, you wanted to store all of the partial sums as a vector L, that too would be an easy modification, as long as you preallocate L to be a vector of the correct length.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by