_________________________________________________________________________
EXPERIMENT : NO
TITLE :
03 INTRODUCTION TO MATLAB AND USAGE OF SOME BASIC OPERATIONS
DATE OF : EXPERIMENT
______________
GROUP NO
:
______________
BATCH
:
______________
GROUP MEMBERS
_______________________ _______________________ _______________________ _______________________ _______________________ _______________________
:
________________________________________________________________________ Experiment # 03
Applied Physics
Page 1 of 11
_________________________________________________________________________
CONTENTS PURPOSE .......................................................................................................................... 3 INTRODUCTION............................................................................................................. 3 BASIC OPERATIONS ..................................................................................................... 4 TASK................................................................................................................................ 10
________________________________________________________________________ Experiment # 03
Applied Physics
Page 2 of 11
_________________________________________________________________________
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: • Math and computation • Algorithm development • Modeling, simulation and prototyping • Data analysis, exploration and visualization • Scientific and engineering graphics • Application development, including graphical user interface building
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.
________________________________________________________________________ Experiment # 03
Applied Physics
Page 3 of 11
_________________________________________________________________________
BASIC OPERATIONS Array Consider an array ‘u’ of numbers as given below 𝑢 = 0 1 2 3 4 5 6 7 8 9 10 There are various methods to define this array in MATLAB. 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’.
________________________________________________________________________ Experiment # 03
Applied Physics
Page 4 of 11
_________________________________________________________________________
Matrix Let ‘A’ and ‘B’ be two matrices, 2 1 5 10 𝐴 = [2 3 7 ] 𝐵 = [5 6 6 1 1
3 4] 8
In MATLAB, these matrices would be defined as shown below:
To compute the size of any matrix the command size is used. For example, we want to find the size of B matrix.
________________________________________________________________________ Experiment # 03
Applied Physics
Page 5 of 11
_________________________________________________________________________
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:
________________________________________________________________________ Experiment # 03
Applied Physics
Page 6 of 11
_________________________________________________________________________
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, both the variables will be entered in MATLAB as separate arrays and using the ‘plot’ command, their graph would be plotted. To add labels to the x-axis and y-axis respectively, the commands ‘xlabel’ and ‘ylabel’ would be used respectively. And to give a title the command ‘title’ would be used. The whole syntax is given below.
Angle of Attack (deg) 0 5 10 15 17 18 19
Coefficient of lift 0 0.5 1 1.5 1.6 1.55 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 (deg) 0 4 8 12 16 17 18
Coefficient of lift 0 0.5 1.1 1.55 1.6 1.55 1.5
For this you will have to create two more arrays, with different names. The complete code would be: ________________________________________________________________________ Experiment # 03
Applied Physics
Page 7 of 11
_________________________________________________________________________
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.
________________________________________________________________________ Experiment # 03
Applied Physics
Page 8 of 11
_________________________________________________________________________
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.
2nd Case (Equation) If the variables are linked through an equation and you are to plot a graph between the two variables, then one variable is defined as an array and the second variable is given as a function of the first variable. As an example consider the variation of temperature within the troposphere. We know that temperature decreases at a constant rate of 6.5ᵒC/1000 m. So the equation becomes 𝑇 = 𝑇0 −
6.5ℎ(𝑚) 1000
Where 𝑇0 is sea level temperature and 0 < ℎ(𝑚) < 11000 𝑚. If for instance 𝑇0 = 15ᵒ𝐶 . The the equation would be: 𝑇 = 15 −
6.5ℎ(𝑚) 1000
________________________________________________________________________ Experiment # 03
Applied Physics
Page 9 of 11
_________________________________________________________________________
This equation will be plotted in MATLAB as:
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
________________________________________________________________________ Experiment # 03
Applied Physics
Page 10 of 11
_________________________________________________________________________ 1.2 3.04 7.3 3.2 −1 −12 1.2 5 𝑨 = 2.7 1 −2 4 6.6 4.51 0 1 [−3 −2.2 1 6] a. b. c. d.
1.3 −1 𝐵 = 2.5 4.8 [1.6]
Compute is size of A Compute AB Extract the elements of A that are in 2nd to 4th row and 1st & 3rd Columns Solve Ax = B for 𝑥.
Q2 a. Draw the graph of following function: 𝑧 = 𝑒 −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
________________________________________________________________________ Experiment # 03
Applied Physics
Page 11 of 11