MATLAB Question about a matlab program

  • Thread starter Thread starter ramin86
  • Start date Start date
  • Tags Tags
    Matlab Program
AI Thread Summary
The discussion revolves around understanding a simple MATLAB program that calculates values in an array using a loop. The initial program initializes an array and iteratively adds the square of a variable to the previous array element, resulting in specific calculated values. Participants also discuss debugging techniques in MATLAB to better understand variable states during execution. Additionally, a user seeks help with a new problem involving generating client numbers based on specific criteria and creating corresponding Excel files. The conversation highlights the challenges of programming loops and the need for clear guidance in MATLAB.
ramin86
Messages
42
Reaction score
0
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.
 
Physics news on Phys.org
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
 
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?
 
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

x(m+1) = = x(m) + k^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)
 
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.
 
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.
 
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:
hi.it my today's last question.Please help for solving.
Make a Matlab Program named ‘wordcounter’ which returns number of words in a given sentence.(HINT:input will be a sentences which ends with a full stop.Count number commas,semi colons,full stops and spaces.)
 
Back
Top