Unit 5 - Matlab

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

MATLAB - Overview

Presentation by
R.Sakthivel Murugan,
Assistant Professor,
Dept of Mechanical Engg, KCET.
Contents
• Microsoft Excel • Charts using Excel
• Excel – Components
• Excel Tutorials
• Cells and Their Addresses
• Plotting in Excel
• Creating Formulas in Excel
• Matrix Computation in Excel
• Basic Excel Arithmetic Operations
• Cell References
• Absolute Cell Reference
• Relative Cell Reference
• Mixed Cell Reference
• Excel Functions
• For Engineering Analysis
• Trigonometric Functions
• Exponential and Logarithmic Functions
• Logical Functions
• Now () & Today() Functions
07/09/2024 2
MATLAB
• MATLAB stands for Matrix Laboratory.
• MATLAB is a very powerful tool, especially
for manipulating matrices.
• MATLAB is also versatile enough that you
can use it to write your own programs. (Desktop App)
MATLAB
• MATLAB environment handles tasks of the
declaration of the data type of the
variables and provision for an appropriate
amount of storage for the variables.

07/09/2024 3
MATLAB - Basics
1. Menu Tabs/Bar:
• Contains the commands you can use to perform
certain tasks, for example, to save your workspace or
import data.

2. Current Folder:
• Shows the active directory, but you can also use it to
change the directory.

3. Current Folder Window:


• Shows all files, their types, sizes, and descriptions in
the current directory.

4. Command Window:
• This is where you enter variables and issue MATLAB
commands.

5. Command History Window:


• Shows the time and the date at which commands were
issued during the previous MATLAB sessions. It also
shows the history of commands in the current (active)
session.
MATLAB’s desktop layout in default mode is divided into four windows:
6. Workspace: 1. Current Folder 2. Command Window 3. Workspace, and
• Shows variables you created during your MATLAB 4. Command History.
session
07/09/2024 4
MATLAB – Assigning Values
• In the MATLAB environment, you
can assign values to a variable or
define the elements of a matrix.
• For example,
• To assign a value of 5 to the variable
x, in the Command Window after the
prompt sign >> simply type x = 5.

07/09/2024 5
Scalars & Vectors
• Some variables are identifiable by a single value or by magnitude. These
types of physical variables which are identifiable by a single value are
called scalars.
• For example, time can be described by a single value such as two hours.
• Temperature is another example described by single value as Celsius/Fahrenheit.
• The physical variables that possess both magnitude and direction
are called vectors.
• For Eg, velocity of a vehicle, not only have to specify how fast it is moving (speed), but also
its direction.
• To describe the location of a car parked in a multistory garage (with respect to the garage
entrance), you would need to specify the floor (the z coordinate), and then the location
ofthe car on that floor (x and y coordinates).

07/09/2024 6
Matrix Defining in MATLAB
• A matrix is often used to describe situations that require many values.
• In general, a matrix is an array of numbers, variables, or mathematical
terms.
• To define the elements of a matrix, for example

• Type 
Note that in MATLAB, the elements of the matrix are
enclosed in brackets [ ] and are separated by blank
space, and the elements of each row are separated by a
semicolon (;)

07/09/2024 7
format, disp, and fprintf Commands
format Command
• The MATLAB format command
allows you to display values in
certain ways.
• For example,
if you define x = 2/3,
then MATLAB will display
x = 0.6667.
• By default, MATLAB will display
four decimal digits.

07/09/2024 8
format, disp, and fprintf Commands
disp Command
• The disp command is used to
display text or values.
• For example,
x = [1 2 3 4 5], then the
command disp(x) will display
1 2 3 4 5.
• disp('Result =') will display
Result = 1 2 3 4 5
• Note that the text that you
want displayed must be
enclosed within a set of single
quotation marks

07/09/2024 9
format, disp, and fprintf Commands
fprintf Command
• The fprintf is a print (display)
command that offers a great
deal of flexibility.
• Use it to print text and/or
values with a desired number
of digits.
• You can also use special
formatting characters, such as \
n and \t, to produce linefeed
and tabs.

 Note that the text and formatting code are enclosed within a set of single quotation marks.
 Also note that the %g is the number format character

07/09/2024 10
Saving Your MATLAB Workspace
• Can save the workspace to a file by issuing the command:
save *filename*
• The *filename* is the name that you would like to assign to the
workspace.

• Later, you can load the file from the disk to memory by issuing the
command
load *filename*.
• The dir command to list the content of the directory.

07/09/2024 11
Generating a Range of Values
• When creating, analyzing, or plotting
data, it is often convenient to create a
range of numbers.
• To create a range of data or a row For example,
To generate a set of x values in the range of zero to 100 in
matrix, you only need to specify the increments of 25 (i.e., 0 25 50 75 100),
starting number, the increment, and in the Command Window, type

the end number.

07/09/2024 12
Creating Formulas in MATLAB
• Use MATLAB to input engineering formulas and compute the results.

07/09/2024 13
Element-by-Element Operation
• MATLAB’s symbols for element-by-element operations are shown in Table.

Note the multiplication symbol (.*) for element-by-element operation.

07/09/2024 14
Matrix Operations
• MATLAB offers many tools for matrix operations and manipulations.
• Refer Table, for few matrix operations.

07/09/2024 15
MATLAB Functions
• MATLAB offers a large selection of built-in
functions that you can use to analyze data.
• The MATLAB functions are available in
various categories, including mathematical,
trigonometric, statistical, and logical
functions.
• The Help button is marked by a question
mark ? located on the main tab bar.
• We can also type help followed by a
command name to learn how to use the
command.

07/09/2024 16
MATLAB Functions - Examples
• Mass =[102 115 99 106 103 95 97 102 98 96].

07/09/2024 17
MATLAB Functions – (Trigonometric, Exponential and Logarithmic)

07/09/2024 18
MATLAB Loop Control
for Loop
while Loop
• Using the for loop, you can execute a line or a
block of code a specified (defined) number of • Using the while loop, you can execute a line or a
times. The syntax of a for loop is block of code until a specified condition is met.
The syntax of a while loop is

For example, suppose you


want to evaluate the function For example, suppose you
for x values of want to evaluate the function
22.00, 22.50, 23.00, 23.50, for x values of
and 24.00. 22.00, 22.50, 23.00, 23.50,
and 24.00.

07/09/2024 19
MATLAB Logical and Relational Operators
• Logical Operators allow to test various conditions.
• Relational or comparison operators that allow for the testing of relative
magnitude of various arguments.

07/09/2024 20
The Conditional Statements—if, else
• When writing a computer program, sometimes it For example,
becomes necessary to execute a line or a block of
Suppose we have a set of scores for an exam:
code based on whether a condition or set of
conditions is met (true). 85, 92, 50, 77, 80, 59, 65, 97, 72, 40.
• A code that shows that scores below 60 indicate ‘FAIL’ and
if statement equal & above 60 indicate ‘PASS’
• Using the if statement, you can execute a line or a block of
your program as long as the expression following the if
statement is true.
• The syntax for the if statement is

else statement
• The else statement allows us to execute other line(s)
of computer code(s) if the expression following the if
statement is not true.

07/09/2024 21
The M-File
• To write a program that is more than a few lines
long, use an M-file.
• It is called an M-file because of its .m extension.
• Can create an M-file using any text editor or using
MATLAB’s Editor/Debugger.
• To create an M-file, open the M-file Editor by going to
HOME→ New Script.
• Type the program and save it. To save the file, simply
click EDITOR→ Save As and type in the file-name.
• The filename should not be the same as a MATLAB
command.
• To run your program, in the EDITOR tab click on Run.
• Debugger is used to find any mistakes in program.
07/09/2024 22
Plotting with MATLAB
• MATLAB offers many different types of charts. Some
of the important charts are:
• XY charts,
• Column charts (or histograms),
• Contour plots
• Surface plots.

• An engineering chart must contain proper labels with


proper units for each axis.
• The chart must also contain a figure number with a title
explaining what the chart represents.
• If more than one set of data is plotted on the same
chart, the chart must also contain a legend or list
showing symbols used for different data sets.

07/09/2024 23
Plot ( ) Command
Plot ( ) Command:

Syntax – plot (x,y) or plot (x,y,s)

It plots y values versus x values.


‘s’ is a character string that defines a
particular line type, plot symbol, or
line color. (Refer Table)
(Eg). plot (x,y, 'k*-')

title('text') command – Can add text on top of the plot


xlabel('text') command – creates the title for the x-axis.
ylabel('text') command – creates the title for the y-axis.
grid On/Off/Minor Command – For turn on or remove the grid lines. Minor for turn on minor grids.

07/09/2024 24
Plot ( ) Command
Plot ( ) Command:

Syntax – plot (x,y) or plot (x,y,s)

It plots y values versus x values.


‘s’ is a character string that defines a
particular line type, plot symbol, or
line color. (Refer Table)
(Eg). plot (x,y, 'k*-')

title('text') command – Can add text on top of the plot


xlabel('text') command – creates the title for the x-axis.
ylabel('text') command – creates the title for the y-axis.
grid On/Off/Minor Command – For turn on or remove the grid lines. Minor for turn on minor grids.

07/09/2024 25
Curve Fitting with MATLAB
• MATLAB offers a variety of curve-fitting
options.
polyfit (x, y, n) command
• Used to obtain an equation that closely fits
a set of data points.
• determines the coefficients of a
polynomial of order n that best fits the
data according to

07/09/2024 26
MATLAB TUTORIALS

07/09/2024 27
Engineering Problems 1

07/09/2024 28
Engineering Problems 2
Using MATLAB, create a table that shows the relationship between the interest earned and the amount
deposited, as shown in Table 15.6

07/09/2024 29
Engineering Problems 3

07/09/2024 30
Engineering Problems 4

07/09/2024 31
Thank You

07/09/2024 32

You might also like