Only getting the last iteration from for loop?
이전 댓글 표시
I've seen similar questions posted, but I can't figure out where my code is going wrong. Any insight would be hugely appreciated.
The code is as follows:
ar0 = zeros(100,n);
int = zeros(100,n);
for r = 1:n
int = fd0.*sin(r.*Phi);
fint = fit (Phi,int,'smoothingspline','SmoothingParam',0.999999999,'Normalize','on');
ar0 = (2/pi).*(integrate(fint,Phi,0));
rar2 = ((ar0.^2).*r);
end
Dq0= (1/l.^2)*(pi/4).*sum(rar2)
Essentially this is just a Fourier sine series, so the higher n is (defined at the start), the more accurately the iterations should converge on a solution.
int should be the line I want to integrate, where fd0 and Phi both have 100 values, but this line should change with each 'r' value.
Each line should then be fit and integrated according to the ar0 equation, so I should end up with as many ar0 values as n values.
rar2 is computing r times each ar0 value squared.
The final Dq0 is a function of the sum of all these rar2 values, but I'm only getting the Dq0 using the last iteration's value.
Is there a clear error in my code here?
Thanks very much in advance.
댓글 수: 1
Ted Shultz
2019년 8월 23일
It looks like you are overwriting your previous results every run thought the loop
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!