Need help creating a "for loop" for the following series.

1 view (last 30 days)
the above equation is used to determined certain polynomial coefficients. I tried creating a "for" loop for the function where y = 1,2,....,v and L0 = 1. where v is a scalar input like 3. I tried creating a "for loop" where instead starting from i = 0, it starts from 1, but the L vector (input) does not want sum up the correct values. I am suppose to get f2y = [0 -0.75 0 0.1875 0 -0.156] but cannot seem to work backwards. Please help!!!
Here is what I came up so far,
L = [1.5 0.75 0.125 0 0 0];
v = 3;
f2y = zeros(1,2*v);
for y = 2:v,
f2y(2) = 2*L(1)*L(3) + (-1)^(1) * L(2)^(2);
for ii = 1:v,
f2y(2*y) = (-1)^(ii-1)*2*L(ii)*L(2*y-ii) + (-1)^(y) * L(y+1)^(2);

Sign in to comment.

Accepted Answer

Nalini Vishnoi
Nalini Vishnoi on 29 Sep 2014
In my understanding you are trying to convert the above equation to MATLAB code using 'for' loops.
The last term in the equation is outside the summation.
Since the L vector starts from 0, I would recommend appending the first index of vector L with a 1 and using L(t+1) wherever L(t) is required. Again, as MATLAB indices start from 1, we can use (i-1) wherever an 'i' is occurring in the equation. This would get rid of the requirement to compute f(2) separately.
The code may look like the following:
L = [1 1.5 0.75 0.125 0 0 0];
v = 3;
f2y = zeros(1,2*v);
for y = 1:v,
for i = 1:y
f2y(2*y) = f2y(2*y)+(-1)^(i-1)*2*L(i)*L(2*y-(i-1)+1) ; % L((i-1)+1) becomes L(i)
f2y(2*y) = f2y(2*y) + + (-1)^(y) * L(y+1)^2;
This seems to produce the expected results for the data provided.

More Answers (0)


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



Community Treasure Hunt

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

Start Hunting!