MATLAB: Need to store for-loop values in array

arrayfor loopMATLABtable

% Hi, I want to store the approximation for each k value that I have here in this code into an array so that I can print it out as a table.
% but I can't figure out how to do that.
% Any help appreciated. Thank you
for k = 1:10
x = pi/5;
approximation = (-1^(k-1))*((x^(2*k-1))/(2*k-1));
prevApprox = (-1^(k-2))*((x^(2*k-2))/(2*k-2));
true = atan(x);
trueError = ((true - approximation) / true)*100;
approxError = ((approximation - prevApprox)/approximation)*100;
end

Best Answer

% Hi, I want to store the approximation for each k value that I have here in this code into an array so that I can print it out as a table.
% but I can't figure out how to do that.
% Any help appreciated. Thank you
for k = 1:10
x = pi/5;
approximation(k) = (-1^(k-1))*((x^(2*k-1))/(2*k-1));
prevApprox = (-1^(k-2))*((x^(2*k-2))/(2*k-2));
true = atan(x);
trueError = ((true - approximation(k)) / true)*100;
approxError(k) = ((approximation(k) - prevApprox)/approximation(k))*100;
end
format long g
[(1:10).', approximation(:), approxError(:)]
ans = 10×3
1 -0.628318530717959 -Inf 2 -0.0826834044807995 -138.732414637843 3 -0.019585259826258 -98.9436788648692 4 -0.00552282190222042 -85.6807669405445 5 -0.00169580653985827 -79.0493109783823 6 -0.000547754389827752 -75.0704374010849 7 -0.000182976340006646 -72.4178550162199 8 -6.26046751416604e-05 -70.523153312745 9 -2.18076486108318e-05 -69.1021270351388 10 -7.70307094740397e-06 -67.9968843747784