Need help creating a "for loop" for the following series.
2 views (last 30 days)
Show older comments
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);
end
end
2 Comments
Star Strider
on 26 Sep 2014
‘where u is a scalar input like 3’
Unfortunately, u is nowhere to be found in the expression you posted. Could that be the problem?
Accepted Answer
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)
end
f2y(2*y) = f2y(2*y) + + (-1)^(y) * L(y+1)^2;
end
This seems to produce the expected results for the data provided.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!