0% found this document useful (0 votes)
20 views41 pages

MATLAB

This document discusses the MATLAB software and its user interface. It describes the various windows in the MATLAB interface, including the command window, workspace window, current folder window, and others. It also covers basic MATLAB operations like scalars, matrices, and scientific notation.

Uploaded by

Hashir Ameen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views41 pages

MATLAB

This document discusses the MATLAB software and its user interface. It describes the various windows in the MATLAB interface, including the command window, workspace window, current folder window, and others. It also covers basic MATLAB operations like scalars, matrices, and scientific notation.

Uploaded by

Hashir Ameen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Computing Fundamentals

Ch.E- 111

Muhammad Umar Mushtaq


2 Introduction to MATLAB
 MatLab stands for "Matrix Laboratory"
 A program/software for doing numerical computations,
solve complex mathematical and engineering problems
 Used extensively in both academia and industry
 Excellent programming features and graphics capability
 Easy to learn and flexible
 The syntax is simple and straightforward
 In general, easier to program in MatLab than in C/C++
or Fortran

Computing Fundamentals Ch.E-111


3
User Interface

Computing Fundamentals Ch.E-111


4
User Interface
 The default MATLAB screen, which opens each time you start the program,
is shown in Figure.
Command Window:
 The command window is located in the center pane of the default view of the
MATLAB ® screen, as shown in Figure.
 The command window offers an environment similar to a scratch pad.
 Using it allows you to save the values you calculate, but not the commands
used to generate those values.
 If you want to save the command sequence, you will need to use the editing
window to create an M-file.

Computing Fundamentals Ch.E-111


5
User Interface
Command History
 The command history window records the commands you issued in the
command window. When you exit MATLAB ® , or when you issue the clc
command, the command window is cleared. However, the command history
window retains a list of all your commands.
 For example, first clear the contents of the command window by typing clc
 This action clears the command window but leaves the data in the command
history window intact.
 You can transfer any command from the command history window to the
command window by double-clicking (which also executes the command)
or by clicking and dragging the line of code into the command window.
 You may clear the command history with the edit menu.

Computing Fundamentals Ch.E-111


6
User Interface
Workspace Window
 The workspace window keeps track of the variables you have defined as
you execute commands in the command window.
 These variables represent values stored in the computer memory, which
are available for you to use.
 Set the workspace window to show more about the displayed variables by
right clicking on the bar with the column labels. (This feature is new to
MATLAB ® 7 and will not work if you have an older version.)
 Check size and bytes , in addition to name, value, and class.
 In describing the command window, we introduced the clc command. This
command clears the command window, leaving a blank page for you to
work on. However, it does not delete from memory the actual variables
you have created.
 The clear command deletes all of the saved variables. The action of the
clear command is reflected in the workspace window.

Computing Fundamentals Ch.E-111


7
User Interface
Current Folder Window
 The current folder window lists all the files in the active directory. When
MATLAB either accesses files or saves information, it uses the current
folder unless told differently.

Computing Fundamentals Ch.E-111


8
User Interface
Document Window
 Double-clicking on any variable listed in the workspace window
automatically launches a document window, containing the variable
editor.
 Values stored in the variable are displayed in a spreadsheet format. You
can change values in the array editor, or you can add new values.
Graphics Window
 The graphics window launches automatically when you request a graph.

Computing Fundamentals Ch.E-111


9
User Interface
Edit Window
 To open the edit window, choose File from the menu bar, then New , and,
finally Script.
 This window allows you to type and save a series of commands without
executing them. You may also open the edit window by typing edit at the
command prompt
 or by selecting the New Script button on the toolbar.

Computing Fundamentals Ch.E-111


Solving Problems with MATLAB-Using
10 Variables
 Although you can solve many problems by using MATLAB ® like a
calculator, it is usually more convenient to give names to the values you are
using. MATLAB uses the naming conventions that are common to most
computer programs:
 All names must start with a letter. Although MATLAB ® will let you create long
variable names, excessive length creates a significant opportunity for error. Names
should be short enough to remember and should be descriptive.
 The only allowable characters are letters, numbers, and the underscore. You can
check to see if a variable name is allowed by using the isvarname command.
 As is standard in computer languages, the number 1 means that something is true
and the number 0 means false.
 Names are case sensitive. The variable x is different from the variable X.
 MATLAB ® reserves a list of keywords for use by the program, which you cannot
assign as variable names. The iskeyword command causes MATLAB ® to list
these reserved names:

Computing Fundamentals Ch.E-111


Matrices in MATLAB
11
 The basic data type used in MATLAB ® is the matrix .
 A single value, called a scalar, is represented as a 1 x 1 matrix.
 A list of values, arranged in either a column or a row, is a one-dimensional
matrix called a vector.
 A table of values is represented as a two dimensional matrix.
 In mathematical nomenclature, matrices are represented as rows and columns
inside square brackets:

 In this example, A is a 1 x 1 matrix, B is a 1 x 2 matrix, and C is a 2 x 2


matrix.

Computing Fundamentals Ch.E-111


12
Scalar Operations
 MATLAB ® handles arithmetic operations between two scalars much as do
other computer programs and even your calculator. The syntax for addition,
subtraction, multiplication, division, and exponentiation is shown in Table 2.1
.

Computing Fundamentals Ch.E-111


13
Scalar Operations
 A single equals sign (=) is called an assignment operator in MATLAB.
 The assignment operator causes the result of your calculations to be stored in
a computer memory location.
 The assignment operator is significantly different from an equality. Consider
the statement
x=x+1
 This is not a valid algebraic statement, since x is clearly not equal to x + 1.
 However, when interpreted as an assignment statement, it tells us to replace
the current value of x stored in memory with a new value that is equal to the
old x plus 1.

Computing Fundamentals Ch.E-111


14
Order of Operations
 In all mathematical calculations, it is important to understand the order in
which operations are performed.
 MATLAB ® follows the standard algebraic rules for the order of operation:
 First perform calculations inside parentheses, working from the innermost set to
the outermost.
 Next, perform exponentiation operations.
 Then perform multiplication and division operations, working from left to right.
 Finally, perform addition and subtraction operations, working from left to right.

Computing Fundamentals Ch.E-111


15
Array Operations
 As described previously, the simplest way to define a matrix is to use a list of
numbers, called an explicit list. The command
x = [1 2 3 4]
 returns the row vector
x=
1234
 A new row is indicated by a semicolon, so a column vector is specified as
y = [1; 2; 3; 4]
 and a matrix that contains both rows and columns is created with the statement
 a = [1 2 3 4; 2 3 4 5 ; 3 4 5 6]
 and will return
a=
1234
2345
3456
Computing Fundamentals Ch.E-111
16
Scientific Notation
 Scientific notation expresses a value as a number between 1 and 10,
multiplied by a power of 10 (the exponent).
 In MATLAB ® , values in scientific notation are designated with an e
between the decimal number and the exponent. (Your calculator probably
uses similar notation.) For example, you might have
Avogadro's_constant = 6.022e23;
Iron_diameter = 140e-12; or
Iron_diameter = 1.4e-10;
 It is important to omit blanks between the decimal number and the exponent.

Computing Fundamentals Ch.E-111


17
Script M-Files
 Using the command window for calculations is an easy and powerful tool.
However, once you close the MATLAB ® program, all of your calculations
are gone.
 Fortunately, MATLAB ® contains a powerful programming language.
 As a programmer, you can create and save code in files called M-fi les. These
fi les can be reused anytime you wish to repeat your calculations.
 You can find out what M-files are in the current folder by typing
what
 into the command window. You can also browse through the current folder by
looking in the current folder window.

Computing Fundamentals Ch.E-111


18
Comment Operator
 The comment operator in MATLAB is the percentage sign, as in
% This is a comment
 MATLAB ® will not execute any code on a commented line.
 You can also add comments after a command, but on the same line:
 a = 5 %The variable a is defined as 5

Computing Fundamentals Ch.E-111


19
Transpose
 Another useful matrix operator is transposition. The transpose operator
changes rows to columns and vice versa. For example,
 degrees = [10 15 70 90];
 To change the values to radians, you must multiply by p>180:
 radians = degrees*pi/180
 This command returns a matrix called radians , with the values in radians.
 degrees’ returns

Computing Fundamentals Ch.E-111


20
Creating Table
 This makes it easy to create tables. For example, to create a table that
converts degrees to radians, enter
 table = [degrees', radians']
 which tells MATLAB ® to create a matrix named table, in which column 1 is
degrees and column 2 is radians:

Computing Fundamentals Ch.E-111


Exercise
21  One performance characteristic that can be determined in a wind tunnel is
drag.
 Drag is the force generated as an object, such as an airplane, moves through a
fluid. Of course, in the case of a wind tunnel, air moves past a stationary
model, but the equations are the same. Drag is a complicated force that
depends on many factors. One factor is skin friction, which is a function of
the surface properties of the aircraft, the properties of the moving fluid (air in
this case), and the flow patterns caused by the shape of the aircraft (or, in the
case of the Mars Climate Observer, by the shape of the spacecraft). Drag can
be calculated with the drag equation

 where Cd = drag coefficient, which is determined experimentally, usually in


a wind tunnel,
 Ρ = air density,
 V = velocity of the aircraft,
 A = reference area (the surface area over which the air flows).
Computing Fundamentals Ch.E-111
Exercise
22  Although the drag coefficient is not a constant, it can be taken to be constant
at low speeds (less than 200 mph). Suppose the following data were measured
in a wind tunnel:
 drag = 20,000 N
 ρ = 1 x 106 kg/m3
 V = 100 mph (you’ll need to convert this to meters per second)
 A = 1 m2
 Calculate the drag coefficient. Finally, use this experimentally determined
drag coefficient to predict how much drag will be exerted on the aircraft at
velocities from 0 mph to 200 mph.

Computing Fundamentals Ch.E-111


Exercise-Solution
23

Computing Fundamentals Ch.E-111


Built-In MATLAB Functions
24
 The vast majority of engineering computations require quite
complicated mathematical functions, including logarithms,
trigonometric functions, and statistical analysis functions.
 MATLAB has an extensive library of built-in functions to allow you to
perform these calculations. For example, to take the square root of the
variable x , we type

 A big advantage of MATLAB is that function arguments can generally


be either scalars or matrices. In our example, if x is a scalar, a scalar
result is returned. However, the square-root function, sqrt x, can also
accept matrices as input.

Computing Fundamentals Ch.E-111


Built-In MATLAB Functions
25
 You can create more complicated expressions by nesting functions. For
instance,

 NESTING: Using one function as the input to another.


 Nesting functions can result in some complicated MATLAB code.
 Be sure to include the arguments for each function inside their own set
of parentheses.
 Often, your code will be easier to read if you break nested expressions
into two separate statements. Thus,

Computing Fundamentals Ch.E-111


Built-In MATLAB Elementary Math
26
Functions

Computing Fundamentals Ch.E-111


Rounding Functions
27

Computing Fundamentals Ch.E-111


Discrete Mathematics
28

Computing Fundamentals Ch.E-111


Trigonometric Functions
29

Computing Fundamentals Ch.E-111


Maximum and Minimum
30

Computing Fundamentals Ch.E-111


Maximum and Minimum
31

Computing Fundamentals Ch.E-111


Mean and Median
32

Computing Fundamentals Ch.E-111


Sums and Products
33

Computing Fundamentals Ch.E-111


Sums and Products
34

Computing Fundamentals Ch.E-111


Manipulating Matrices in MATLAB
35

Using the Colon Operator


 The colon operator is very powerful in defining new matrices and
modifying existing ones. First, we can use it to define an evenly
spaced matrix. For example,

 The default spacing is 1. However, when colons are used to


separate three numbers, the middle value becomes the spacing.
Thus,

Computing Fundamentals Ch.E-111


Plotting in MATLAB-Simple x-y Plot
36
 The most useful plot for engineers is the x–y plot.
 The commands used to create a simple plot are summarized in
Table below:

Computing Fundamentals Ch.E-111


Plots with More than One Line
37
 A plot with more than one line can be created in several ways.
 By default, the execution of a second plot statement will erase the
first plot.
 However, you can layer plots on top of one another by using the
hold on command. Execute the following statements to create a
plot with both functions plotted on the same graph

Computing Fundamentals Ch.E-111


Plots with More than One Line
38
 Semicolons are optional on both the plot statement and the hold
on statement. MATLAB will continue to layer the plots until the
hold off command is executed.
 Another way to create a graph with multiple lines is to request
both lines in a single plot command. MATLAB interprets the
input to plot as alternating x and y vectors. Using the data from
the previous example:

 This function produces the same graph as previously, with one


exception: The two lines are different colors.

Computing Fundamentals Ch.E-111


Line, Color, and Mark Style
39  You can change the appearance of your plots by selecting user-
defined line styles and line colors and by choosing to show the
data points on the graph with user specified mark styles.
 Example: plot(x,y,':ok')

Computing Fundamentals Ch.E-111


Axis Scaling and Annotating Plots
40  MATLAB ® automatically selects appropriate x -axis and y -axis
scaling. Sometimes, it is useful for the user to be able to control
the scaling. Control is accomplished with the axis. Thus, the
command axis([-2, 3, 0, 10]) fixes the plot axes to x from 2 to 3
and y from 0 to 10.

Computing Fundamentals Ch.E-111


Practice Exercise
41

Computing Fundamentals Ch.E-111

You might also like