MATLAB Programming Tutorials For Beginners With Examples
Hello guys, welcome back to our blog. This article will be on MATLAB programming tutorials for beginners with an example, we will cover many topics in this article so that you can become familiar with MATLAB, etc.
If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics.
Also, read:
- Power Engineering, Definition, Scope, Companies, Job Profile, Salary.
- What Is A MATLAB, Applications, Alternatives, Advantages, Disadvantages.
- Top Free Online Course For Electrical And Electronics Engineers.
MATLAB Programming Tutorials
MATLAB stands for the MATrix LaBoratory. It is a specialized-purpose computer program optimized to perform scientific and engineering calculations. MATLAB is also called a technical programming language. Began with simple matrix manipulation and was built with the capability of solving any technical problem.
It is a proprietary programming language that is developed by MathWorks. MAT-LAB is not available for free or it is not open source software, you need to pay some amount for this. MAT-LAB offers a very extensive library of predefined functions to do technical programming tasks simple, easy, and more effective.
MATLAB is preferred to other languages like C, FORTRAN, etc. The MAT-LAB can be interfaced with different kinds of hardware like Arduino, TMS320C6713, FPGA, NI DAQ, etc to enhance the data processing capabilities of MATLAB.
It is very helpful for people working in the field like research, development, and design challenges. MATLAB can solve many problems that are faced by engineers and scientists such as pressure to meet deadlines while deciding design tradeoffs, developing a variety of projects and tasks that are rarely straightforward, the proliferation of products and technologies, experimenting and programming are expensive, etc these all challenges can be solved by using MATLAB.
Topics That I Will Cover In MATLAB Programming Tutorials
- General commands
- Arithmetic operations
- Vector / array operations
- Logical operations
- Complex numbers and operations
- Control statements
- Loops
- Plots
01. General Commands
MATLAB commands | Operation |
clc | Clear the screen |
disp (‘HELLO’) | Display the string HELLO |
disp(a) | Display the array “a” or value of “a” |
x = input(‘value of x’) | Allow the user to enter the value of x from the keyboard |
02. Arithmetic Operations
MATLAB commands | Operation |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
^ | power |
Examples:
MATLAB Instructions | Result |
a = 3; | |
b = 4; | |
c = 5; | |
d= a+b | >>7 |
e = a*c | >>15 |
f = (a+b)/c | >>1.4 |
e = a^2+c-b | >>10 |
03. Vectors/Array Operations
MATLAB Instructions | Description | Result |
a = [1 2 3] | Create a row vector | a= [1 2 3] |
b = [ 1; 2; 3] | Create a column vector | Watch video for result |
c = [10 2 3; 4 50 6; 7 8 90] | Create a ( 3*3) matrix | |
d = c * b | Matrix multiplication | |
e = inv(c) | The inverse of the square matrix | |
f =transpose(c) |
Note: MATLAB allows arithmetic operations with elements of matrices and arrays. Few examples are shown here.
g = c(1,2)*b(2) | Multiplication of elements of two matrices | >>g = 4 |
h = c(2,3)+a(3) | Addition of elements of two matrices | >>h = 9 |
k = c(3,2)^2 | Square of that element | >>k =64 |
Note: MATLAB allows array operations to be performed element by element. Here are some examples.
a = [1 2 3 4 ]; | ||
b = [2 2 5 1 ]; | ||
c = a.*b | Multiplies element by element | c = [2 4 15 4 ] |
c = a./b | Divide element by element | c = [0.5 1.0 0.6 4.0] |
04. Logical Operations
MATLAB commands | Operation |
Assuming both ‘a’ and ‘b’ have Boolean values ( 1 or 0) | |
c=a|b | Logical OR |
c=a&b | Logical AND |
c= ~a | Logical NOT |
C= xor(a,b) | Logical XOR |
The same logical operations can also be done with arrays. |
Examples:
MATLAB Instructions | Result |
a = 1; | |
b = 0; | |
c = 0; | |
d= a|c | >>1 |
e = b|c | >>0 |
f = a&b | >>0 |
e = a&d | >>1 |
g = ~a | >>0 |
h = xor(a,b) | >>1 |
a=[1 0 1]; | |
b=[0 0 1]; | |
c=a|b | >>[ 1 0 1] |
c=a&b | >>[ 0 0 1] |
c=~b | >>[ 1 1 0] |
c=xor(a,b) | >>[1 0 0] |
05. Complex Numbers And Operations
MATLAB commands | Operation |
Assuming both ‘a’ and ‘b’ real numbers | |
c=complex(a,b) | form a complex number c= a+ j b |
d = real (c) | extract the real part of complex number c |
d= imag(c) | extract the imaginary part of complex number c |
d= abs(c) | The absolute value of complex number c |
d = conj (c) | The conjugate of c ( a – j b) |
d = angle (c ) | It will find an angle. |
All other arithmetic operations can be performed on complex numbers, which result in a complex number. |
Examples:
MATLAB Instructions | Result |
a = 3.0; | |
b = 4.0; | |
c1 = complex(a,b) or c1=a+1i*b | >>c1 = 3+j 4 |
c2 = complex(6,8) or c2=6+8i | >>c1 = 6+j 8 |
d = c1 + c2 | >>d = 9 + j 12 |
d = c1 * c2 | >>d = -14 + j48 |
d = c1 – c2 | >>d = -3 – j4 |
c3 = conj (c1) | >>c3 = 3 – j4 |
e = real (c3) | >>e = 3 |
f = imag (c3) | >>f = -4 |
Phi =angle(c3) | >>phi =-0.9273 |
c4 = c3^2 | >>c4 = -7- j 24 |
c1 = (3+4i) | >>c1 = 3+j4 |
a=3; | |
b=4; | |
c1=(a+b*i) | >>3+j 4 |
06. Control Statements
General form:
if expression 1
statements
elseif expression 2
statements
elseif expression 3
statements
...............
...............
else
statements
Where,
Expressions 1, 2, 3, . . . .n are some relational expressions employing the following relational operators.
a = =b | if a equal to b |
a>=b | if a is equal to and greater than b |
a<=b | if a is equal to and less than b |
a~=b | if a is not equal to b |
Example 01:
a= 2;
b=3;
c=10;
if c<a*b
disp(‘ hello 1’);
elseif c>a*b
disp(‘ hello 2’)
else
disp(‘hello 3’)
end
Example 02:
a=1;
b=2;
c=3;
d=b^2-4*a*c
if d > 0.0
disp(‘ real roots ‘);
root1= (-b + sqrt(d))/2.0
root2 = (-b – sqrt(d))/2.0
elseif d < 0.0
disp(‘ complex roots ‘);
f=sqrt(-d)/2
root1 = complex((-b/2),f)
root1 = complex((-b/2),-f)
else d==0.0
disp(‘ repeated roots’);
root1= -b/2
root2 = -b/2
end
07. Loops
a. For loop
General form:
for i=1:n
statements;
statements;
end
In the above example, “i” will be incremented by one after every cycle.
for time = 0.0 : 0.05 : 1.2
statements
statements
end
This FOR loop will be executed with an increment of 0.05 for the variable “time”.
Example:
This example computes electrical power Pe = 1.2 Sin (del) for 0.0 < del < pi
MATLAB Instructions | Result |
n=0; for del = 0.0 : 0.4 : pi n=n+1; pe(n)=1.2*sin(del); end disp(pe); | >>pe = [ 0 0.467 0.86 1.118 1.199 1.09 0.81 0.402 ] |
b. While loop
General form:
While expression
statements
.......
.....
end
MATLAB Instructions | Result |
n=0; del = 0.0 while del <=pi n=n+1; pe(n)=1.2*sin(del); del=del+0.4 end disp(pe); | pe = [ 0 0.467 0.86 1.118 1.199 1.09 0.81 0.402 ] |
08. Plots
MATLAB commands | Operation |
plot (a) | Plots the columns of array a versus their index |
plot (a,b) | Plots vector b versus vector a |
plot (a,b,a,c,a,d) | overlaid plots of (b Vs a), (c Vs a), ( d Vs a) |
subplot(m,n,p) | Breaks the figure into (m x n ) matrix |
grid on | Adds grid on the plot |
title (‘ text’) | Adds the title ‘ text’ on top of the plot |
xlabel(‘text’) | Adds ‘text’ along the x-axis |
ylabel(‘text’) | Adds ‘text’ along the y-axis |
Example:
MATLAB Instructions | Result |
a=[0.5 0.77 0.91 0.82]; plot(a); grid on; xlabel(‘sample no.’); ylabel(‘width’); title(‘SAMPLE WIDTH’); | Watch the video for the result |
MATLAB Instructions | Result |
a=[0.5 0.7 0.9 1.2]; b = [ 1.2 1.44 1.67 1.06] plot(a,b); grid on; xlabel(‘time.(secs)’); ylabel(‘temp(deg c)’); title(‘TEMP PLOT’); | Watch the video for results |
Watch the video on my YouTube Channel to understand it clearly. I hope this article may help you all a lot. Thank you for reading. If you have doubts related to this article “MATLAB Programming Tutorials”, then comment below.
Also, read:
- 10 Tips To Maintain Battery For Long Life, Battery Maintainance
- 10 Tips To Save Electricity Bills, Save Money By Saving Electricity
- 100 (AI) Artificial Intelligence Applications In The Automotive Industry
- 100 + Electrical Engineering Projects For Students, Engineers
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Control System Quiz, Top MCQ On Control System
- 1000+ Electrical Machines Quiz, Top MCQs On Electrical Machines
- 1000+ Electronics Projects For Engineers, Diploma, MTech Students
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- 1000+ MATLAB Simulink Projects For MTech, Engineering Students