You are on page 1of 39

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Matlab Programming for Engineers

Introduction to Matlab
Matlab Basics
Branching Statements
Loops
User Defined Functions
Additional Data Types
Input/Output Functions
Simulink Toolbox
Important Toolboxes (if time is available)
Dr. Nidal Farhat
Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Objectives:
Variables and Arrays
Initializing Variables in MATLAB
Multidimensional Arrays
Subarrays

Special Values

Displaying Output Data


Data Files
Scalar and Array Operations
Hierarchy of Operations
Built-in MATLAB Functions
Introduction to Plotting
Debugging MATLAB Programs

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

Variables and Arrays

The fundamental unit of data in any Matlab program is the Array


Any element of the array can
be retrieved by its indexes
Arrays could be matrices or
vectors (row or column)
Matlab variable is the region
a(3,2)
of memory containing an array
Matlab variable
letter + (letter, number, _)
Variable type:
1. double precession
(real, complex)
2. Character
'How are you?'

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

Creating and initializing variables in Matlab


1. Initializing variables in assignment statements
var = expression;
var = 40i;
var2 = var/5;

Mechanical Engineering Department

var = [1 2 3 4];

MATLAB PROGRAMMING FOR ENGINEERS

Creating and initializing variables in Matlab


1. Initializing variables in assignment statements

a = [0 1+7];
a = [0 8]
b = [ a(2) 7 a];
b = [8 7 0 8]
c(2,3) = 5;
c= 0 0 0
0 0 5

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Creating and initializing variables in Matlab


2. Initializing variables with Shortcut Expression
colon operator (:)
first : incr : last
Ex:
x = 1:2:10;
x = [1 3 5 7 9]
angles = (0.01:0.01:1)*pi;
f = [1:4]';
if no incr is used default incr=1
(') is the transpose operator.
g = [1:4];
h = [g' g'];

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Creating and initializing variables in Matlab


3. Initializing variables with Built-in Function

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Creating and initializing variables in Matlab


4. Initializing variables with Keyboard Input
my_val = input('Enter an input value: ');

v1 = input('Enter data: ');


v2 = input('Enter data: ', 's');

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Multidimensional Arrays

Mechanical Engineering Department

CH2: MATLAB BASICS

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Multidimensional Arrays

Array as introduced

2. Accessing this array with one dimension index

Mechanical Engineering Department

How the array is stored in computer memory

1. Storing Multidimensional Array in Memory

MATLAB PROGRAMMING FOR ENGINEERS

Subarrays

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Subarrays
1. The end Function (highest value of a given subscript)

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

2. Left-hand side Subarrays


assignment statement

Subarrays
3. Assigning a scalar to
a subarray

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Special Values

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

Displaying Output Data


Default Matlab Format == Four Digits After the Decimal Point

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Displaying Output Data (NUMBER FORMAT)

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

Displaying Output Data (disp and fprintf functions)

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Data Files (save)


save filename var1 var2 var3
save filename var1 var2 var3 -ascii

Mechanical Engineering Department

*.mat
text format (ascii)

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Data Files (save)


save filename var1 var2 var3
save filename var1 var2 var3 -ascii

Mechanical Engineering Department

*.mat
text format (ascii)

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Data Files (load)


load filename.mat var1 var2
load filename.*

(*.mat file format, loads specific variables)


(any other format, loads all the file
data in the file must have array structure)

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Scalar and Array Operations

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Scalar and Array


Operations

Very important

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Scalar and Array Operations


Matrix left division (a\b)

Mechanical Engineering Department

CH2: MATLAB BASICS

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Hierarchy of Operations
Distance = 0.5 * acceleration* (time^2)
Distance = (0.5 * acceleration* time)^2

Mechanical Engineering Department

Not the same!

MATLAB PROGRAMMING FOR ENGINEERS

Class Work

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Built-in Matlab Functions


1 . Optional results

Optional

2. Most of functions
accept array inputs

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Built-in Matlab Functions (Common Functions)

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Built-in Matlab Functions (Common Functions)

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Plotting in Matlab

Mechanical Engineering Department

CH2: MATLAB BASICS

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Printing a Plot / Exporting a Plot as a Graphical Image


>> print <options> <filename>

>> help print

If no filename is specified,
plot is printed on the printer

(to see print options)

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH2: MATLAB BASICS

Printing a Plot / Exporting a Plot as a Graphical Image

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Multiple plots
Plot the following functions on the same figure
f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Multiple plots
Plot the following functions on the same figure
f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Line Color, Line Style, Marker Style, and Legends

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Line Color, Line Style, Marker Style, and Legends


Line Color, Line Style, and Marker Style:

Mechanical Engineering Department

CH2: MATLAB BASICS

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Line Color, Line Style, Marker Style, and Legends


Legends:
>> legend off (removes the legend from the plot)
>> legend('string1' , 'string1' , ,'location', 'value from table')

NW

NL

NC

NR

NE

TW

TL

TC

TR

TE

MW

ML

MC

MR

ME

BW

BL

BC

BR

BE

SW

SL

SC

SR

SE

Limits of
plot axes

Be aware of the version of Matlab you are using, see help legend
Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Logarithmic Scale
>> plot
>> semilogx
>> semilogy
>> loglog

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Example

Mechanical Engineering Department

CH2: MATLAB BASICS

MATLAB PROGRAMMING FOR ENGINEERS

Home Work
Solve the following problems
2. [9, 10 , 11, 16]

Mechanical Engineering Department

CH2: MATLAB BASICS

You might also like