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
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!