Blog

Simple Expressions in Matlab

To run simple expressions in MATLAB we can use the command window. Here is how you open the command window.
Command Window is inside the Desktop menu in Matlab.

This is the Command Window open in Matlab:

Type 1+2 now and press enter
This is the output in the Command Window:

When you write an expression in the Matlab Command Window. Matlab evaluates it and stores it in a default variable called ans.

Then prints the answer if no semi colon is added after the expression.

if I write 1+2; then no output is given by default. If I want the output I can simply write ans in the next line and get the output.

% starts a comment, and clc clears the window.

The following scalar arithmetic operators are supported in Matlab.  At this moment we are not dealing with matrices.
+

*
/
\
^
Uses of these operators.
3+4 will give 7
3-4 will be -1
3*4 is 12
4/5 is 0.8
4\5 reverse the order of division. Answer will be 1.25
3^4 is 3 raised to the power 4. This will be 81.
These are the precedence rules for arithmetic eperators in Matlab.

  1. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)
  2. Addition (+), subtraction (-)Operators in the same level are ealuated from left to right.

Some Problems:

Problem 1:
Find the area of a triangle having sides 3,4 and 5.

>> a=3;b=4;c=5;
>> s=(a+b+c)/2;
>> area=(s*(s-a)*(s-b)*(s-c))^0.5;
>> area

area =
6
>>

Problem 2:
Solve the Quadratic Equation:
x2-5x+6=0

 

>> a=1;b=-5;c=6;
>> discr=b*b-4*a*c;
>> root1=(-b + discr^0.5)/(2*a);
>> root2=(-b – discr^0.5)/(2*a);
>> root1

root1 =
3
>> root2
root2 =
2
>>

 

Leave a Reply

Your email address will not be published. Required fields are marked *