Question about a matlab program

In summary, the program will look for the sentence that ends with a full stop and count the number of words in that sentence.
  • #1
ramin86
42
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
  • #2
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
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
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)
 
  • #5
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
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
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:
  • #8
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.)
 

Related to Question about a matlab program

1. How do I run a MATLAB program?

To run a MATLAB program, simply open the MATLAB software, navigate to the folder where your program is saved, and type the name of the program in the command window. Alternatively, you can click on the "Run" button at the top of the MATLAB editor.

2. How do I debug my MATLAB program?

To debug a MATLAB program, you can use the built-in debugging tools such as breakpoints, step into, and step over. You can also use the "dbstop" command to set breakpoints at specific lines of code. Additionally, you can use the "disp" function to print out values of variables at different points in your code to check for errors.

3. Can I use loops in my MATLAB program?

Yes, MATLAB supports various types of loops, including for loops, while loops, and do-while loops. These can be used to perform repetitive tasks or iterate through arrays or matrices.

4. How do I input data into my MATLAB program?

You can input data into your MATLAB program by using the "input" function. This function allows the user to enter data through the command window during program execution. You can also load data from external files using the "load" function.

5. How do I save the results of my MATLAB program?

To save the results of your MATLAB program, you can use the "save" function. This function allows you to save variables or data from your program to a specified file. You can also use the "fprintf" function to output results to a text file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top