## Only getting the last iteration from for loop?

Asked by Ed Frost

### Ed Frost (view profile)

on 23 Aug 2019 at 18:38
Latest activity Commented on by Ed Frost

### Ed Frost (view profile)

on 23 Aug 2019 at 19:26
Accepted Answer by Walter Roberson

### Walter Roberson (view profile)

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.

Ted Shultz

### Ted Shultz (view profile)

on 23 Aug 2019 at 18:55
It looks like you are overwriting your previous results every run thought the loop

### Tags ### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 23 Aug 2019 at 18:54

ar0 = zeros(100,n);
int = zeros(100,n);
Okay, looks like you want to create outputs named ar0 and int
for r = 1:n
int = fd0.*sin(r.*Phi);
That line overwrites all of the variable int.
fint = fit (Phi,int,'smoothingspline','SmoothingParam',0.999999999,'Normalize','on');
We can guess from this call that probably Phi is a vector, so probably int will be a vector after you wrote over all of it in the line before. But suppose you had fixed the previous line so it only wrote over part of int, then you would need to be careful because in this line you are using all of the variable int. You originally defined int as being 2D, but the second parameter to fit() must be a vector, not 2D.
ar0 = (2/pi).*(integrate(fint,Phi,0));
That line overwrites all of the variable ar0. We can deduce from this line that Phi must be a vector as otherwise cfit method integrate() would fail on it. The returned value will be the same size as Phi, so we can deduce that a vector is being returned here.
rar2 = ((ar0.^2).*r);
That line overwrites all of the variable rar2. Be careful over whether ar0 is a vector (as returned by integrate) or a 2D array (as you initialized

Ed Frost

### Ed Frost (view profile)

on 23 Aug 2019 at 18:59
Hi Walter,
Thanks for a really insightful answer, I didn't realise I was writing over the entire variable each time.
Forgive my ignorance, but how then do I get the int definition to write a column for each value of 'r', then get the fit function to fit each column, then integrate etc to produce a column of ar0 values for each value of 'r' please?
Would that even be the best way to do it?
Thanks again
Walter Roberson

### Walter Roberson (view profile)

on 23 Aug 2019 at 19:05
ar0 = zeros(100,n);
int = zeros(100,n);
rar2 = zeros(100,n);
for r = 1:n
int(:,r) = fd0.*sin(r.*Phi);
fint = fit (Phi, int(:,r), 'smoothingspline', 'SmoothingParam', 0.999999999, 'Normalize', 'on');
ar0(:,r) = (2/pi).*(integrate(fint,Phi,0));
rar2(:,r) = ((ar0(:,r).^2).*r);
end
Dq0 = (1/l.^2)*(pi/4).*sum(rar2, 2);
Ed Frost

### Ed Frost (view profile)

on 23 Aug 2019 at 19:26
I can't tell you how much time you've saved me Walter, thank you very much for your help.