# Question about a matlab program

1. Sep 11, 2004

### ramin86

m = 0;
x(1)=10;
for k = 2:3:11
m = m+1;
x(m+1) = x(m) + k^2;
end

Its a simple program, but for some reason I cannot explain how it works, I was wondering if I can get some help.

2. Sep 11, 2004

### TenaliRaman

m = 0;
%initialisation of m
x(1)=10;
%initialisation of x(1)
for k = 2:3:11
%k goes from 2 to 11 with step size of 3
%i.e k will have values 2,5,8,11
m = m+1;
%increment value of m
x(m+1) = x(m) + k^2;
%calculate next value of x_array
end
%end of for loop

-- AI

3. Sep 11, 2004

### ramin86

Well, the only thing I really don't get is x(m+1) = x(m) + k^2;

The calculated values are 10, 14, 39, 103, 224, how are these values calculated through that statement?

4. Sep 11, 2004

### enigma

Staff Emeritus
What it's doing is increasing the size of the 'x' array.

x(1) is 10
m is 1

The first iteration of the 'for' loop, k is 2

That means that its taking the value in x(m), which is x(1), which is 10; it is adding k^2 or 4 to it (14)

and storing it in x(m+1), x(2), or the second slot in the 'x' array.

It then proceeds on down the line.

Might I suggest you familiarize yourself with the debugging commands?

If you hit 'set breakpoint' (I think it's F12) and then run, the program will pause at that line. You can then either mouse over the variables in question and let you see what's stored there, or you can doubleclick on the variables in the Matlab main page's workspace to bring up a dialog.

To continue to the next breakpoint (or end of the program) i think the key is F5 (continue)

5. Sep 11, 2004

### ramin86

Alright, now I understand it. My next question relates to the following program:

x = 0;
k = 0;
sum = 0;
while x < 2000
k = k + 1;
x = 2^k;
end

I was asked to find the number of terms for the series to exceed 2000. But now, I am asked to find the sum of those terms. How would I do that? I know its rather simple, but for whatever reason I've always had problems with loops when dealing with programming.

6. May 7, 2011

### Rauf080

hi.i am new in matlab.and i am learning it alone.and i found interesting question tuesday form internet.and i couldn't solve it.please help me to solve it.
question is this
You are IT manager of company having at most clients.You are desired to give clien numbers containing 8 digits to clients in such a way that first 2 digits will be 11,the next three digits will be 030,and the last three digits will be a randomly chosen number from 1 to 999 with zeros on the left digits(if it is needed).

a)First create an excel file named ‘numberlist’ in which the numbers from 1 to 999 are written in the first column of the file.
b)make an M-file named ‘clientnumber’ such that when it begins to run,it will ask from the user name of client;and it will read randomly a number from the created excel file (numberlist) and form the client number as explained above.Then it will create a new excel file named ‘client’ and write the order of client,the name of client and the client number of the client in this file in such a way that first column contains orders of clients,second one contains names of clients an the third one contains client number of clients.
c)You should be careful with giving same client number to two or more different clients.You should not give same client number to two or more different clients.

7. May 7, 2011

### Rauf080

hi it is my second question about Matlab.
Make a Matlab program named ‘comp’ which returns the row matrix containing all elements of input row matrix of numbers,which are greater then or equal to 30(HINT:input will be a row matrix of numbers,the output will be the row matrix containing all elements of input matrix which are greater then equal then or equal to CIN.CIN=30)
I think it is very good question.

Last edited: May 7, 2011
8. May 7, 2011