Professional Documents
Culture Documents
Applied Physics Exp 3 - Intro To Matlab
Applied Physics Exp 3 - Intro To Matlab
EXPERIMENT : 03
NO
DATE OF
: ______________
EXPERIMENT
GROUP NO : ______________
BATCH : ______________
_______________________
_______________________
_______________________
GROUP
: _______________________
MEMBERS
_______________________
_______________________
________________________________________________________________________
CONTENTS
PURPOSE .......................................................................................................................... 3
INTRODUCTION............................................................................................................. 3
BASIC OPERATIONS ..................................................................................................... 4
TASK................................................................................................................................ 10
________________________________________________________________________
PURPOSE
The purpose of this experiment is to give students the introduction to MATLAB, and a
know how about some commonly used MATLAB commands.
INTRODUCTION
MATLAB is an interactive package for numerical analysis, matrix computation, control
system design and linear system analysis and design. The name MATLAB stands for
matrix laboratory. MATLAB is a high performance language for technical computing. It
integrates computation, visualization, and programming in an easy to use environment
where problems and solutions are expressed in familiar mathematical notation. Typical
uses include:
Working in MATLAB
While working with MATLAB you get on your screen always a prompt. This is >>.
This means that MATLAB is ready to accept commands. If you press the enter key, the
commands get executed. Unless you finish your command with a semicolon (; ),
MATLAB displays the result on screen.
With clear you de-allocate memory space for those variables.
________________________________________________________________________
BASIC OPERATIONS
Array
Consider an array ‘u’ of numbers as given below
𝑢 = 0 1 2 3 4 5 6 7 8 9 10
1st Method
In this method, all the numbers are separately written in MATLAB
2nd Method
In this method the command is written in such a manner that MATLAB automatically
defines the array with an interval of 1 between the 1st and the last number. However for
this method to work, the array should be in ascending order.
3rd Method
In this method the user specifies the 1st and the last number of the array and the interval
between two consecutive numbers of the array. However for this method to work, the array
should be in ascending order. For the array under consideration the interval required is ‘1’.
________________________________________________________________________
Matrix
Let ‘A’ and ‘B’ be two matrices,
1 5 10 2 3
𝐴 = [2 3 7 ] 𝐵 = [5 4]
6 1 1 6 8
To compute the size of any matrix the command size is used. For example, we want to
find the size of B matrix.
________________________________________________________________________
Two matrices can be easily multiplied in MATLAB. However, the number of columns
in the 1st matrix should be equal to the number of rows in the 2nd matrix.
Any row or column or a single element of a matrix can be extracted using MATLAB
command. Suppose we want to extract the 1st and 2nd row of matrix A.
Similarly, if you want to extract the 2nd element (column) in the 3st row of matrix A.
Matrix equation can also be solved using MATLAB. Suppose that we want to solve the
system 𝐴𝑥 = 𝐵.
This can be written as:
𝑥 = 𝐴−1 𝐵
In MATLAB, this can be written as:
________________________________________________________________________
Plotting
Plotting a graph between two variables can be done in different ways depending on how
the variables are linked.
1st Case (Data Points)
As an example, consider the variation of Coefficient of lift of an airfoil with angle of
attack, such that you have the values of the coefficient of lift at each angle of attack.
To plot the graph between the two variables, Angle of Attack Coefficient of
both the variables will be entered in MATLAB (deg) lift
as separate arrays and using the ‘plot’ 0 0
command, their graph would be plotted. To add 5 0.5
labels to the x-axis and y-axis respectively, the 10 1
commands ‘xlabel’ and ‘ylabel’ would be used 15 1.5
respectively. And to give a title the command 17 1.6
‘title’ would be used. The whole syntax is 18 1.55
given below. 19 1.5
Let’s consider a more complex case. For example you have the lift curve data for
another airfoil shown below and you want to compare both the curves on one graph.
Angle of Attack
Coefficient of lift
(deg)
0 0
4 0.5
8 1.1
12 1.55
16 1.6
17 1.55
18 1.5
For this you will have to create two more arrays, with different names. The complete code
would be:
________________________________________________________________________
In the above code, first of all, the values of angles of attack and coefficients of lift for both
airfoils are entered. Then plot command is used. Firstly, the x and y variables of the first
airfoil are called. The term ‘g- -‘gives the first curve green color and a dashed line style.
‘g’ is used to give the green color and ‘- -‘ is used to give a dashed line style. Then the x
and y variables of the second airfoil are called. The term ‘r’ gives red color to the second
curve. The term ‘linewidth’,2 is used to give both the curves a width of 2 units (default line
width is 1 unit). The term … is used, so that by pressing enter, the command is not executed
and you can continue the same command in the next line. Then using xlabel, and ylabel,
labels are given to both axis. To turn the grid on, grid is used. A title can be given using
title. If there are two curves, then it is necessary that each one is identified properly. So for
identification of curves legend is used. Finally to give a specific font size to the labels and
axis, set is used. After pressing enter, the following graph then appears.
________________________________________________________________________
If for instance, you have already plotted 1 curve on a figure and without writing a new code
you want to add a new curve to the same plot, then the ‘hold on’ command is used. The
code for this case would be:
If you have plotted one graph and want to plot another graph in a separate window, then
by default if you just use the plot command, MATLAB would overwrite the existing graph
but this not what you want. So for this, figure() is used. This would open both curves in
separate figures.
Note that the commands such as figure(), hold on and other commands that were used in
the 1st case can be used here as well.
TASK
Q1 Define Following Matrices
________________________________________________________________________
a. Compute is size of A
b. Compute AB
c. Extract the elements of A that are in 2nd to 4th row and 1st & 3rd Columns
d. Solve Ax = B for 𝑥.
Q2
𝑧 = 𝑒 −2𝑡 cos(3𝑡 + 1) − 𝜋 ≤ 𝑡 ≤ 5𝜋
b. The deflection of a cantilever beam is the distance its end moves in response to a force applied
at the end. The following table gives the deflection 𝑥 that was produced in a particular beam
by the given applied force 𝑓. Plot the graph:
Force 𝑓 (pound) 0 100 200 300 400 500 600 700 800
Deflection 𝑥 (inches) 0 0.09 0.18 0.28 0.37 0.46 0.55 0.65 0.74
________________________________________________________________________