You are on page 1of 9

Electromechanical Systems Lab

Lab No.1

Date: 6/04/21

Submitted by: Syed Suleman Ayub (19pwmct0678)


Lab Instructor: Engr. Shahbaz Khan

MECHATRONICS ENGINEERING DEPARTMENT


UET PESHAWAR

LAB 01: Introduction to MATLAB


Abstract:
Solving a complex problem takes time and sometimes one cannot get the correct solution. In this lab
we introduced Matrix Laboratory (MATLAB) which is a high-performance program used for solving
complex engineering problems. It can be used for symbolic and numerical calculations, plotting graphs
and is a problem solver in science and mathematics. In this lab I went through the usage and some
basics of MATLAB. I used it as calculator and found exact solutions to some problems. I used it to
verify some laws as well as plot the graphs of two functions on one plane.

Objectives:
 To learn the basics of MATLAB
 Open and save .m files
 Execute commands in MATLAB
 Creating matrices
 Plot graphs in MATLAB
 Creating functions

Introduction:
In engineering or in any other scientific field, we have to do big data analysis, simulation and modeling
or calculations. We can analyze or model somethings ourselves to some extent but we can solve
complex engineering of scientific problems. That is why most of work is done by softwares. But the
problem here is that some softwares can do simulation or modeling but cannot do calculations, develop
algorithms or analyze data and vice versa. This is where MATLAB comes in.

Matrix Laboratory (MATLAB) is a high performance program used for technical computing and
solving complex engineering problems[CITATION CIM21 \l 1033 ]. It simplifies the analysis of
mathematical models. It has convenient tools and built in functions that makes its usage easier for
beginners. We can link other softwares to it such as Solid works etc. it also has an option for add-ons.

Matlab is extensively used for:

 Design, test and implement control systems


 Machine learning
 Acquire, analyze and explore data
 Convert robotics ideas to autonomous systems
 Deep learning [ CITATION Cle21 \l 1033 ]etc.

User Interface:
We can open MATLAB by clicking on the icon on desktop. The user interface of MATLAB is shown
in Fig 1. We will look into each section one by one.

Figure 1: Graphical User Interface

1. Workspace:
This section shows which command is executed in command section. It also shows
solution.
2. Command Window:
In this section we write commands. It also solution to problems if any.
3. Editor:
In this section we write our code. We can add comments to the code. It does not show
solution. The solution is shown in command sections or you can display it on another
window. This section opens up when you open a new script file.
4. Current Folder:
This section show the location directory of the file you are working on.
5. Tools section:
This section contains open and save options. You can also open new script from here.
You can run code from this section.

Methodology:
To understand how MATLAB works, we will execute some commands on it. We will perform some
tasks which will help to get started with MATLAB.
Creating .m File:
 Open MATLAB from its directory or desktop.
 To create a .m file, click on ‘New Script’ on top left corner of tools section as shown in Fig 2.
 A new file named ‘Untitled’ will open.
 Before executing any commands save the .m file using the save option on top left corner of
tools section as shown in Fig 2.
 A window will pop up, save the file in directory easily accessible.
 When you execute a code a window will pop up saying .m file not found. Simply click on add
to path and you are good to go.

Figure 2: Creating new script

Creating matrices:
 To create a matrix, first assign a variable to it and then type the elements of the matrix as
follows;
A=[45 6 4 8; 12 6 8 97; 32 2 4 6; 45 2 32 16];
B=[11 21 35 8; 23 15 3 4; 6 4 8 12; 15 24 65 2];

 The semicolon is used to add another row in matrix. You can create a matrix of any order.

Multiplication of matrices:
 To multiply the matrices, we will use a simple command i.e.
A*B.
 As the matrices are of order 3x3 so multiplication can occur
 Always make sure that columns of A is equal to rows of B.
Associative law for addition and multiplication:
 For associative we need three variables. In this case we will add another matrix i.e.
C=[2 4 5 6; 45 16 22 45; 32 45 68 12; 5 2 4 9];
 Associative law for addition;
For associative law for addition we will write the following code.

LHS=(A+B)+C
RHS=A+(B+C)
if
LHS == RHS
disp('LHS = RHS = ');
disp(LHS)

end

LHS must be equal to RHS.


The results are given in Results section.
 Associative law for multiplication;
For associative law for multiplication we will write the following code.

lhs=(A*B)*C
rhs=A*(B*C)
if
lhs == rhs
disp('lhs = rhs = ');
disp(lhs)

end

lhs must be equal to rhs.


The results are given in Results section.

Creating two functions with intervals:


 Write any function and create an interval using the following code;

X=4:2:14;
Y=X.^2+2*X+1
Z=2*X.^2+X+12

 Here X=4:2:14 represents the interval. The first number represents the starting point. The last
number is the end point. The middle number between the two colons represents the increment.
So it goes as X= starting point: iteration: ending point.
Plotting the two functions:
 To plot a graph first write ‘figure’ in editor section.
 Then write plot (X, Y, ‘r’). The x and y in the parenthesis represent the two functions. You
change the color of the graph line or curve by typing the first letter of the color you want in the
parenthesis. Here r represent red color
 You can label the graphs axis by the following code;

xlabel ('X')
ylabel ('F1 & F2')

 Write the name of x-axis and y-axis in the parenthesis.


 To plot a second function on same graph, write ‘hold on’ and then repeat the same steps as you
did for the graph of first function.
 You can add legend and title by using the code given below;

legend ('F1' , 'F2')


title (' Graph of F1 & F2')

 The complete code for plotting a graph will look as follow;

X=4:2:14;
Y=X.^2+2*X+1
Z=2*X.^2+X+12
% Graphs of two functions
figure
plot (X,Y,'b')
xlabel ('X')
ylabel ('F1 & F2')
hold on
plot (X,Z, 'r')
legend ('F1' , 'F2')
title (' Graph of F1 & F2')
 The resultant graph is given in results.

Results:
The result for the given tasks are given below.

Matrix Multiplication:
I declared 3 matrices as shown in Fig 3. The result is shown in Fig 4.
Figure 3: Code for matrices and multiplication

Figure 4: Result of multiplication

Associative laws:
For associative laws I created three matrices and then made a code for it according to formula of
associative laws i.e. (A+B)+C=A+(B+C) and (A*B)*C=A*(B*C) as shown in Fig 5.

Figure 5: Code for Associative law of addition and


multiplication

The result for addition and multiplication is shown in Fig 6 and Fig 7 respectively.

Figure 6: Associative law of addition result Figure 7: Associative law of Multiplication result
Graph of Two functions:
I created two functions with an interval [4, 14] as shown in Fig 8. The graph is shown in Fig 9.

Figure 8: Code forFigure 9: Graph


plotting of two functions
two functions on one graph

Conclusion:
After doing some tasks we can say that MATLAB makes problem solving much easier and saves a lot
of time. As discussed earlier many problems requires a lot of time and effort, but with MATLAB we
can calculate and graph solution to complex problems in seconds. Also we can see that it has a simple
user interface which makes it easier for a beginner to understand the basics of MATLAB.
References

[1] CIMSS, "What is MATLAB," Cooperative Institute for Meteorological Satellite Studies, 2021. [Online].
Available: https://cimss.ssec.wisc.edu/wxwise/class/aos340/spr00/whatismatlab.htm. [Accessed 4 June
2021].

[2] C. Moler, "MATLAB," MathWorks, 1994-2021 . [Online]. Available:


https://www.mathworks.com/products/matlab.html?s_tid=hp_ff_p_matlab. [Accessed 4 June 2021].

You might also like