Pages - Menu

Sunday 7 October 2012

Using MATLAB as a calculator

The purpose of this article is to show you how you can use MATLAB for evaluating an arithmetical expression.

After launching MATLAB, in the Command Window you can see the prompt sign >>.
Let's say we want to evaluate the following expression: 1+3*2^2/4.
In order to do this, simply type it after the prompt, then press Enter.
>> 1+3*2^2/4 
ans =

     4

You can notice that MATLAB has automatically created and assigned  the result to a variable, ans (shortening for answer). However, after performing another evaluation, this variable will be overwritten. In order to avoid this, you can create a new variable ( I will call it res) and assign the outcome to it.

Be aware that variable names are case sensitive, must start with a letter and can contain only letters, numbers and the underscore character.
(After the prompt you can type res, then press the up-arrow key. This will recall a previous command, in this case 1+3*2^2/4. Command History window shows the list of commands used in the past).

The order of precedence in MATLAB is the same as in algebra, so you're free to use parentheses if necessarily.

The expression assigned to a variable can be a combination of numerical values, mathematical operators, variables, build-in functions and functions defined by user (those shall be discussed later).

You can use a variable name to refer to the result of a expression in other computations. For example: 
>> 2*res 
ans =

      8

Now, to terminate MATLAB, you can either go File, then Exit MATLAB, press Ctrl+Q or type one of the following commands in the Command Window: quit or exit.

My next post will focus on basic MATLAB control commands, like the two mentioned above.

No comments:

Post a Comment