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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by