You are on page 1of 21

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

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

Objectives:

The Advantages of Matlab Disadvantages of Matlab

The Matlab Environment


Using Matlab as a scratch Pad

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

Advantages and Disadvantages of Matlab Matlab == MATrix LABoratory Advantages: Ease of Use Platform Independent (All versions of Windows, Linux, Unix, Mac.) Predefined Functions and Toolboxes Device-Independent Plotting Graphical User Interface Matlab compiler

Disadvantages: Interpreted programming language, slower than C++ and V. Fortran.

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

Matlab Command Window User input area Current directory browser Also results appear in this window

Command history

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

Script files / M-files

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

The Edit/Debug Window

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Figure Windows)

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Figure Windows)

Mechanical Engineering Department

10

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Workspace)

whos Display the value of any variable in the WS clear var1 var2 Double-click any var in the WS browser

Mechanical Engineering Department

11

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Workspace)

Mechanical Engineering Department

12

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Getting Help)

Mechanical Engineering Department

13

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Getting Help)

Mechanical Engineering Department

14

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Getting Help)

lookfor: Search the quick summery information in each file

Mechanical Engineering Department

15

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Getting Help)

help: Search function name

Mechanical Engineering Department

16

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment (Getting Help)

Mechanical Engineering Department

17

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Environment

A few important commands clc: clf: clear: clears command window clears current figure clear variables in the WS

Ctrl+c: aborts the execution of the program

exit:

exits Matlab
runs the command in the operating system (ex. dir, copy )

!(command):

save/load (WS): saves and loads the WS

Mechanical Engineering Department

18

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

The Matlab Search Path

If a command (name) is executed Matlab seeks for: A variable with the same name in the current WS. M-file in the same directory M-file in the any directory in the search path
Sequence is important

Mechanical Engineering Department

19

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

MATLAB Math & Assignment Operators

Power: Multiplication: Division: or NOTE:

(^) or (.^) (*) or (.*) (/) or (./) (\) or (.\)

a^b or a.^b a*b or a.*b a/b or a./b b\a or b.\a

56/8 = 8\56

Addition:
Subtraction: Assignment:

(+)
(-) (=)

a+b
ab a=b (assign b to a)

Mechanical Engineering Department

20

MATLAB PROGRAMMING FOR ENGINEERS

CH1: INTRODUCTION TO MATLAB

HW1 The distance traveled by a ball falling in the air is given by the equation

1 2 y y0 v0t at 2
Use the Matlab to plot the path of the ball (y) as a function of time from t=0s to t=5s, if

y0 10m v0 15m / s a 9.81m / s


2

Mechanical Engineering Department

21

You might also like