generating a sequence of numbers

3 views (last 30 days)
ektor
ektor on 11 May 2019
Edited: John D'Errico on 11 May 2019
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

Accepted Answer

John D'Errico
John D'Errico on 11 May 2019
Edited: John D'Errico on 11 May 2019
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 Comments
ektor
ektor on 11 May 2019
Is the code correct? If it is we are fine!!
John D'Errico
John D'Errico on 11 May 2019
Edited: John D'Errico on 11 May 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!