How to write your first MATLAB program

Previously, we learn where to write and save MATLAB program. Now we will discuss how to write your first MATLAB program.


To create a matlab program, we are taking a simple problem i.e. creating two matrix and performing basic functions like Addition, Subtraction, Multiplication and Division. For this, screen capture of 8 lines program has been shown in figure.1.

Figure.1 - Screen capture of MATLAB program for creating matrix A and B, and performing basic matrix functions in editor window.

Executable commands are written in black color letter from line 6 to line 13, which has been discussed below:

Line-6: Create a 2x2 matrix and store it in a variable A. Elements are written in big bracket in which space is used to shift into another column while Semicolon is used to shift into another row.

Line-7: Create a 2x2 matrix and store it in a variable B. Procedure for this matrix will be similer to matrix A.

Line-8: Element of matrix A is added with element of matrix B and saved into the variable ADD. For example, a11 is added with b11 and saved into ADD11. This operation is done with all the element of the matrix to calculate matrix ADD.

Line-9: Element of matrix B is subtracted from the element of matrix A and saved into the variable SUB. For example, b11 is subtracted form a11 and saved into SUB11. This operation is done with all the element of the matrix to calculate matrix SUB.

Line-10: Perform matrix multiplication i.e. row to column multiplication. For example, (MUL1)11 = a11 x b11 + a12 x b21. Similarly, all the elements of MUL1 matrix is calculated.

Line-11: Element of matrix A is multiplied with element of matrix B. For example, a11 is multiplied with b11 and saved into (MUL2)11. This operation is done with all the element of the matrix to calculate all the element of MUL2 matrix.

Line-12: Perform matrix division i.e. matrix A is divided by matrix B, in other words you can say matrix A is multiplied with inverse of matrix B. For example, DIV1 = A/B = A*(B^-1), which is done in similer way to matrix multiplication in line 10.

Line-13: Element of matrix A is divided with element of matrix B. For example, a11 is divided with b11 and saved into (DIV2)11. This operation is done with all the element of the matrix to calculate all the element of DIV2 matrix.

Run the program after click on the icon circle with orange color and named as 'Run Mod0103.m'. The word written in '%' and '%%' are not executable which is only used to mark the matlab program code with its function so that another user can easily understand its meaning.

Video Tutorial: How to write your first MATLAB Program.

2 comments:

  1. Keep it up sir, I continuously follow ur post sir. Good explanation, waiting for next post

    ReplyDelete
  2. Thanks Dear Rajeev. Your complement is the food for me which provides me positive energy to write new post.

    ReplyDelete

Powered by Blogger.