# Introductory MATLAB question

1. Sep 27, 2011

### Eastonc2

so for a practice exam we have the following problem:

1.
Write a commented and well-structured Matlab function that takes two positive integers, a and b, as the input, and then displays the sum of all numbers that are odd or can be divided by 3 without a remainder between a and b (including a and b). For example, if the user inputs 6 and 12, the program should display 45 as the output (sum of 6,7,9,11,12: 6+7+9+11+12=45). If a number is both odd and can be divided by 3 without a remainder, your program should include it only once in the summation (as in the case of 9 in the example above).

2. I am very new to MATLAB, as in, I just started two weeks ago, and have never done any programming of any kind prior to this course, so i have no intuition when it comes to this. After attending every class period, I know that we have not covered how we're supposed to approach this problem. The lab portion is handled by a Chinese study abroad student, and I cannot get past the language barrier with her. I have a tenuous grasp of the subject at hand without trying to interpret what she is trying to say.

3. My request: Please explain how to go about this in a form that I, an idiot, can understand. I honestly don't even know where to begin with this.

We have covered basic structuring of if-elseif, for-loops, and while-loops.

2. Sep 29, 2011

### MATLABdude

Forget about the requirement for MATLAB.

These introductory type programming courses are intended to develop problem-solving methodology (and hopefully give you the tools with which to construct the solution). Tokenize the problem (that's computer scientist fancy-speak for break the problem down into its various components / bulletpoints), and then start developing an algorithm (a method) for solving the problem in plain text. Once you have these, solve the problem

Okay, okay... Then you start doing the hunt for things to help you solve the problem using the method you've developed. In this case, I'd highly suggest the MATLAB getting started site, where you can access videos, tutorials, and learn some of the ins and outs of MATLAB (there is no substitution for doing, at least, not for the average joe like myself):
http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

And of course, the MATLAB documentation page (you can type help <command> at the MATLAB prompt, where <command> is the command you want to learn how to use, but the online documentation is much more in-depth, and all feature excellent examples). For instance, here's the documentation page on looping (bookmark the root page of this--it's invaluable if you use MATLAB a lot):
http://www.mathworks.com/help/techdoc/matlab_prog/brqy1c1-9.html [Broken]

Here's a suggested tokenizing scheme:
1. Input two numbers, a and b
2. List numbers between a and b, including a and b
3. ...
4. Profit! Er... Victory!

Last edited by a moderator: May 5, 2017
3. Sep 29, 2011

### ballzac

I agree with MATLABdude. What he is talking about is also known as pseudocode. You write your code as simple instructions in english that can be converted to any programming language if you know the syntax.

Matlab is a very simple language to do this in without knowing much because you don't need to do anything to get a functional script. For example, a script that only contains the code

Code (Text):
a=10
will produce the result

Code (Text):
a=10
So you can get straight into converting your pseudocode into Matlab code without any preliminaries like including header files and the like. Firstly, if you don't know the syntax for matlab functions, google that. A matlab function will take a number of inputs (in your case 2) and produce a number of outputs. Once you know this syntax, you can get started.

Most tasks are going to require loops of some sort. I usually use for loops. You need to think logically about how you, as a human being, take the numbers given and produce the result you want. Then you write your pseudocode, and then you can simply google the syntax required in matlab to perform each individual task. If you don't supress output, you can see what your code is doing at each stage to see if it's doing what you want. If it produces unexpected results at some point, you know where in your code the problem is.

Your actual pseudocode will have more lines than MATLABdude's scheme. For example,

2. List numbers between a and b, including a and b

may look more like

*subtract a from b to give the required number of iterations
*Initiate loop
*compute a+1 for each iteration
*etc.

I'm sure there are more elegant solutions, the above is just an example off the top of my head.

If you manage to get the required result, I would suggest doing an extension such as error handling for if a>b or either are not an integer, etc. This would require the implementation of if statements.

4. Sep 29, 2011

### ballzac

I'm not sure how clear I was above, so I'll give you a specific example. Lets say you want a function those computes the factorial of an integer. The pseudocode may look like

*set a variable (answer) equal to one
*If the input is not equal to 0, compute the factorial as follows
1. Initiate loop where t goes from 1 to input
2. Multiply (answer) by t in each iteration
*else do nothing

The function then looks like

Code (Text):
function [answer] = fac(input)