MATLAB: How to store output values from a while loop nested in a for loop

arrayfor loopgibbs phenomenastoring output in arraywhile loopwhile loop nested in for loop

Hello,
I am looking to generate a series of plots to explore the Gibbs Phenomena. I have generated a code that accurately computes the series function value and compares it to that of the targeted function. The range of input variables (x) is from -pi:pi/4:pi. I need to generate a plot with the corresponding input variable and returned function value (f(x)=…) for varying ranges of terms in the series (k). I have attempted storing them in an array however with my index including negative and non integer numbers it will not allow me to.
The program is,
function Gibbs(n)
for x = -pi:pi/4:pi
ssum = 0;
k = 1;
while abs((k-1)-n)> 0
term = ((sin(k*x)*((-1)^(k-1)))/k);
ssum = ssum + term;
f = pi + (2*ssum);
k = k + 1;
F = x + pi;
end
fprintf('Gibbs approximated function (f)= %10.10f\n',f);
fprintf('Target funtion (F)= %10.10f\n',F);
end

Best Answer

X(k) = x;
f(k) = pi + (2*ssum);
Then later
plot(X,f)