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

2 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2014-9-26
‘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?

请先登录,再进行评论。

采纳的回答

Nalini Vishnoi
Nalini Vishnoi 2014-9-29
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by