# Using nested FOR() loops to populate a matrix

6 views (last 30 days)
paul on 26 Sep 2022
Commented: paul on 27 Sep 2022
Hello,
Having previously populated a matrix to demonstrated the MacLaurin Series, but entering all the data essentially manually, I'm having a go at using FOR() loops to do the same job again.
I'm trying to demonstrate how the MacLaurin series progressively and more accurately approximates e^x, by creating a vector series of X values from 0 to 1 in steps of 0.01 and then 25 Y series in a single matrix, each row being a progressively better approximation, i.e. row 1 being f(x)=f(0).x^0.1/0!, row 2 being f(x)=f(0).x^0*1/0! + f(0).x^1*1/1!, row 3 being f(x)=f(0).x^0*1/0! + f(0).x^1*1/1! + f(0).x^2*1/2! etc...
Here's my attempt at the code so far:
% MacLaurin Series of e^x
format longG
% define vector to hold series x values
X = 0 : 0.01 : 1;
% Create matrix to contain the results of each function in the series
% A FOR loop is used to populate the matrix without having to write out all
% the series iterations manually
for i = 0:25
F(i+1,:) = [(X(1,:).^i)/factorial(i)]
end
% Create increasingly better approximations by summing increasing numbers
% of function results
for i = 1:26
for j = 1:i
Y(i,:) = [Y(i,:) + F(j,:)]
end
end
I'm pretty sure vector F is created correctly, but trying the same method with matrix Y results in the error:
Unrecognized function or variable 'Y'.
Error in tutorial_6d (line 21)
Y(i,:) = [Y(i,:) + F(j,:)]
The more I think about the error, though, the less certain I am that the entire concept of what I'm trying to do is correct...
If anybody has the time to help me wit the logic of this, I'd be very grateful,
Thanks,
Paul
##### 2 CommentsShowHide 1 older comment
paul on 26 Sep 2022
Hi Stephen,
thanks for your help with this.
I added the square brackets, as I was of the belief that I needed them to distinguish between creating a matrix and creating a vector - in this case, i thought I needed a matrix.
I'm rather new to all the array functions matlab is capable of - my background is more low-level, assembly and C type stuff. Your solution is definitely very much more succint than my laborious attempt!
Thanks very much for your time and help on this,
Paul

Sign in to comment.

### Accepted Answer

VBBV on 26 Sep 2022
% MacLaurin Series of e^x
format longG
% define vector to hold series x values
X = 0 : 0.01 : 1;
% Create matrix to contain the results of each function in the series
% A FOR loop is used to populate the matrix without having to write out all
% the series iterations manually
for i = 0:25
F(i+1,:) = [(X(1,:).^i)/factorial(i)];
end
size(F)
ans = 1×2
26 101
% Create increasingly better approximations by summing increasing numbers
% of function results
Y = zeros(26,101);
for i = 1:26
for j = 1:i
Y(i,:) = [Y(i,:) + F(j,:)];
end
end
Y
Y = 26×101
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.2 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1 1.01005 1.0202 1.03045 1.0408 1.05125 1.0618 1.07245 1.0832 1.09405 1.105 1.11605 1.1272 1.13845 1.1498 1.16125 1.1728 1.18445 1.1962 1.20805 1.22 1.23205 1.2442 1.25645 1.2688 1.28125 1.2938 1.30645 1.3192 1.33205 1 1.01005016666667 1.02020133333333 1.0304545 1.04081066666667 1.05127083333333 1.061836 1.07250716666667 1.08328533333333 1.0941715 1.10516666666667 1.11627183333333 1.127488 1.13881616666667 1.15025733333333 1.1618125 1.17348266666667 1.18526883333333 1.197172 1.20919316666667 1.22133333333333 1.2335935 1.24597466666667 1.25847783333333 1.271104 1.28385416666667 1.29672933333333 1.3097305 1.32285866666667 1.33611483333333 1 1.01005016708333 1.02020134 1.03045453375 1.04081077333333 1.05127109375 1.06183654 1.07250816708333 1.08328704 1.09417423375 1.10517083333333 1.11627793375 1.12749664 1.13882806708333 1.15027334 1.16183359375 1.17350997333333 1.18530363375 1.19721574 1.20924746708333 1.2214 1.23367453375 1.24607227333333 1.25859443375 1.27124224 1.28401692708333 1.29691974 1.30995193375 1.32311477333333 1.33640953375 1 1.01005016708417 1.02020134002667 1.0304545339525 1.04081077418667 1.05127109635417 1.06183654648 1.07250818108917 1.08328706730667 1.0941742829575 1.10517091666667 1.11627806795917 1.12749684736 1.13882837649417 1.15027378818667 1.1618342265625 1.17351084714667 1.18530481696417 1.19721731464 1.20924953049917 1.22140266666667 1.2336779371675 1.24607656802667 1.25859979736917 1.27124887552 1.28402506510417 1.29692964114667 1.3099638911725 1.32312911530667 1.33642662637417 1 1.01005016708417 1.02020134002676 1.03045453395351 1.04081077419236 1.05127109637587 1.0618365465448 1.07250818125257 1.08328706767076 1.09417428369561 1.10517091805556 1.11627807041967 1.1274968515072 1.13882838319807 1.15027379864436 1.16183424238281 1.17351087044836 1.18530485048857 1.1972173618792 1.20924959584067 1.22140275555556 1.23367805628711 1.24607672549876 1.25860000297457 1.2712491409408 1.28402540418837 1.29693007019636 1.30996442925651 1.32312978459876 1.33642745251767 1 1.01005016708417 1.02020134002676 1.03045453395352 1.04081077419239 1.05127109637602 1.06183654654536 1.0725081812542 1.08328706767492 1.0941742837051 1.1051709180754 1.11627807045833 1.1274968515783 1.13882838332257 1.15027379885351 1.16183424272182 1.17351087098097 1.18530485130273 1.19721736309392 1.20924959761422 1.22140275809524 1.2336780598607 1.24607673044788 1.25860000973017 1.27124915004094 1.28402541629852 1.29693008613249 1.30996445001118 1.32312981137044 1.33642748674361 1 1.01005016708417 1.02020134002676 1.03045453395352 1.04081077419239 1.05127109637602 1.06183654654536 1.07250818125422 1.08328706767496 1.09417428370521 1.10517091807564 1.11627807045886 1.12749685157936 1.13882838332459 1.15027379885717 1.16183424272818 1.17351087099162 1.18530485132003 1.19721736312125 1.20924959765634 1.22140275815873 1.23367805995451 1.24607673058398 1.2586000099244 1.27124915031395 1.28402541667696 1.29693008665041 1.30996445071165 1.32312981230745 1.3364274879843 1 1.01005016708417 1.02020134002676 1.03045453395352 1.04081077419239 1.05127109637602 1.06183654654536 1.07250818125422 1.08328706767496 1.09417428370521 1.10517091807565 1.11627807045887 1.12749685157938 1.13882838332462 1.15027379885723 1.16183424272828 1.17351087099181 1.18530485132036 1.1972173631218 1.20924959765723 1.22140275816014 1.2336780599567 1.24607673058731 1.25860000992936 1.27124915032123 1.28402541668747 1.29693008666537 1.30996445073267 1.3231298123366 1.33642748802428
##### 3 CommentsShowHide 2 older comments
Steven Lord on 26 Sep 2022
Alternately take a look at the cumsum function.

Sign in to comment.

### More Answers (1)

John D'Errico on 26 Sep 2022
Edited: John D'Errico on 26 Sep 2022
You have a solution. It works. Do you need for loops, or worse, nested for loops? Of course not. The entire point of this answer is to show that you don't need to use loops to solve a problem like this. Use MATLAB as it was designed to be used.
Nmax = 10; % maximum number of terms in the expansion
Nx = 20; % number of points to be sampled in [0,1]
N = 0:Nmax; % explicitly a row vector
X = linspace(0,1,Nx)'; % see that A is explicitly a column vector
M = X.^N./factorial(0:Nmax); % % using .^ and ,/ to expand the vectors automatically
M = cumsum(M,2); % accumulation, instead of a loop
err = M - exp(X); % the error of approximation
Note that several of the lines above require R2016b or later to operate correctly, but by now, most people should have that much.
[XX,NN] = meshgrid(X,N);
surf(X,N,err')
xlabel X
ylabel N
zlabel 'error of approximation'
Next, looking at the error in terms of a logged z-axis so we can better see the behavior near zero...
surf(X,N,abs(err'));
xlabel X
ylabel 'Number of terms'
zlabel 'Absolute error'
set(gca,'zscale','log')
So as expected, the error is maximal near x==1. As x grows larger, you need more terms in the Taylor series.
Near x==0 we see almost full double precision accuracy for only or one or two terms. Again, this is expected. You need more terms as x moves away from zero.
##### 4 CommentsShowHide 3 older comments
paul on 27 Sep 2022
Thanks John - I'm very grateful for the time you've taken to write your answer and the explanations you've given - to my mind, your responses exemplify the purpose of this forum 👍.
Thanks,
Paul

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by