MATLAB [Matrix Labortory] is a numerical computing software which is used by Electrical Engineers.
You can plot the three-dimensional graphs or can simulate your electrical models for testing their behavior.
Also, learn about Simulink for Electrical Engineers
Today you’ll learn the basic concepts, some lines of code and some terminologies that are essential to Get started with MATLAB.
Contents
Introduction to Main Screen
The main screen of MATLAB has different directories:
Command window is the main screen where you write the code.
Workplace stores the variable and their values.
Basic arithmetic calculations
MATLAB can be used as a calculator for performing the basic arithmetic operations. Simply write the equation and press Enter to obtain an answer.
Arithmetic addition
Code:
ans =
Arithmetic subtraction
Code:
ans =
20
Similarly, you can divide and multiply two numbers.
Variables
Variables can be used to store numbers as well to solve the expressions.
The following lines of code illustrate the use of variables in command window:
a =5
>> b = 6b =6
>> c = a * b
c =
30
The figure below displays execution of above codes in
The Use of Semicolon (;)
Once you hit Enter button, MATLAB simply executes it. However one can suppress the output by using a semicolon (;) at the end of the line.
Trigonometric functions
Trigonometric functions such as sin, cos, tan, cot, sec, cosec can be solved in MATLAB.
Code | Output |
---|---|
sin(90) | 0.8940 |
cos(90) | -0.4481 |
tan(90) | -1.9952 |
sec(90) | -2.2318 |
csc(90) | 1.1186 |
cot(90) | -0.5012 |
The above codes are for radians. In case of degree the codes are:
Code | Output |
---|---|
sind(90) | 1 |
cosd(90) | 0 |
tand(90) | Inf |
secd(90) | -Inf |
cscd(90) | 1 |
cotd(90) | 0 |
The M file in MATLAB
So far we entered our codes in MATLAB command window, there is another alternative way to enter the code. The MATLAB m-file is a simple text file where a user enters the command.
You can create an M-file in one hit.
Head to the top left the menu, click on File > New > M-file
You can run the same codes (which we executed previously) using the M-file.
M-file can also be used to take input from the user.
The following lines of code can be used to take input in MATLAB:
a = input(b);
Where a and b are two variables.
Let’s understand the use of input function to find the square of any number.
x = input(num);
y = x*x
str = input(num,’s’);