Question about a matlab program

  • Context: MATLAB 
  • Thread starter Thread starter ramin86
  • Start date Start date
  • Tags Tags
    Matlab Program
Click For Summary

Discussion Overview

The discussion revolves around various MATLAB programming questions and concepts, including understanding specific code snippets, debugging techniques, and creating programs to solve specific tasks. Participants seek clarification on how certain MATLAB constructs work and request assistance with programming challenges.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks help understanding the operation of a MATLAB loop and how values are calculated in an array.
  • Another participant explains the mechanics of the loop and suggests debugging techniques to observe variable values during execution.
  • A new question is posed regarding how to find the sum of terms in a series generated by a while loop, indicating a struggle with loops in programming.
  • Another participant presents a problem involving generating client numbers with specific formatting and saving them to an Excel file, requesting guidance on how to implement this in MATLAB.
  • Further questions are raised about creating a program to filter numbers from an input matrix based on a condition and counting words in a sentence, indicating ongoing exploration of MATLAB capabilities.

Areas of Agreement / Disagreement

Participants generally share their understanding and seek clarification on programming concepts, but no consensus is reached on the specific solutions to the programming challenges presented.

Contextual Notes

Some participants express uncertainty about their understanding of MATLAB loops and debugging, while others provide insights without resolving all questions. The discussions involve varying levels of familiarity with MATLAB, which may affect the clarity of the responses.

Who May Find This Useful

This discussion may be useful for individuals learning MATLAB programming, particularly those seeking help with loops, debugging, and file handling 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.)
 

Similar threads

Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K