MATLAB / Octave Tutorial-------Chapter 2------Arithmetic and Logical operations


CHAPTER 2: Arithmetic and Logic Operations

Using arithmetic and logic operations in MATLAB / Octave is easier than ever before. Its a very simple to use them and implement them in any operations. Just like any other computer language, the operators are same in MATLAB / Octave. For example: 


Operator
Function
Example
+
Add
a+b
-
Subtract
a-b
*
Multiply
a*b
/
Divide
a/b
^
Exponent
a^b
&
And
a & b
|
Or
a | b
!
Not
!a
xor()
Xor
xor(a,b)

The following shows how to use these operators:

Demonstration of arithmetic operations




Demonstration of Logical operators

Before we begin with creation of MATLAB scripts, we must know two forms of data structures: vectors and matrices. Vectors are same as 1D arrays whereas matrices are 2D arrays. We declare vectors and matrices in the following way:


Declaring vectors and matrices

Now, we shall verify the truth table of the logical operators:

1. AND:

Truth table for Y= A . B where . represents AND operator.

A
B
Y
0
0
0
0
1
0
1
0
0
1
1
1

For doing this operation we make two vectors A and B with the values given in the table. Then we perform AND operation of both these vectors.


Verifying truth table of AND


For the rest of the logical operators we shall use MATLAB script to verify their truth tables.

Now we shall learn to make MATLAB scripts. MATLAB scripts have extension .m.

Step 1: Click on File.
Step 2: Click on New
Step 3: Click on New Script.
A new file will be created.      

Now type as given in the following image:

MATLAB script content

After typing, save the file by pressing Ctrl+S. Give an appropriate name to the file and save it at a convenient location.

To run the script, follow the steps shown below:


Process to run a MATLAB script

This marks the end of this tutorial. I request you to practice this in order to become comfortable with MATLAB / Octave. For the ease of practice I suggest you some sample problems:
  1. Verify the formula for XOR operation:    A xor B = A' . B + A . B', where A' denotes complement or NOT of the Boolean variable; + denotes OR and .(dot) denotes AND.
         Hint: From the snap given below, verify whether Y1 and Y2 are equal
                      

  2.    Similarly, verify the distributive property of mathematics: 
                        A × (B + C) = A × B + A × C

Today's script has been uploaded to the 'Tutorials' folder. You may upload your MATLAB script here.

From now onwards, we shall be writing MATLAB scripts and uploading them on GitHub. Happy Learning!!!!!







Comments

  1. Please like and follow my Facebook page to keep yourself updated with the lessons: https://www.facebook.com/tutorformatlab/

    ReplyDelete

Post a Comment

Popular posts from this blog

MATLAB / Octave Tutorial : Generating various signals

MATLAB / Octave Tutorial----Chapter 3-----Visualising the data(Part 3)