How Can You Sum Odd Numbers and Multiples of Three in MATLAB?

Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB function to sum all odd numbers and multiples of three between two positive integers, a and b. Participants explore problem-solving methodologies, pseudocode development, and MATLAB syntax, while addressing the challenges faced by a beginner in programming.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Exploratory

Main Points Raised

  • One participant requests guidance on how to approach the problem due to their lack of programming experience and understanding of MATLAB.
  • Another participant suggests breaking down the problem into smaller components, a method referred to as tokenizing, and developing an algorithm in plain text before coding.
  • A different participant emphasizes the importance of pseudocode, explaining that it can be converted into any programming language and that MATLAB allows for straightforward scripting without extensive setup.
  • One participant provides an example of pseudocode for computing a factorial, illustrating how to structure logic before implementing it in MATLAB.
  • There is a suggestion to include error handling in the final implementation, such as checking if a is greater than b or if the inputs are integers.

Areas of Agreement / Disagreement

Participants generally agree on the value of breaking down the problem and using pseudocode as a preliminary step. However, there is no consensus on a specific approach to the MATLAB implementation, as different methods and levels of detail are proposed.

Contextual Notes

Some participants note the importance of understanding MATLAB syntax and functions, while others highlight the need for logical reasoning in programming. The discussion reflects varying levels of familiarity with programming concepts and MATLAB itself.

Who May Find This Useful

Beginners in programming, particularly those learning MATLAB, as well as individuals interested in problem-solving methodologies and algorithm development.

Eastonc2
Messages
19
Reaction score
0
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.
 
Physics news on Phys.org
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 :smile:

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

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:
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:
a=10

will produce the result

Code:
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.
 
I'm not sure how clear I was above, so I'll give you a specific example. Let's 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
*output (answer)

The function then looks like

Code:
function [answer] = fac(input)

    answer=1;
if input~=0

for t=1:input
answer=t*answer;   
end

end
end
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
5K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K