Matlab Programming for Engineers Introduction to Matlab Basics

Matlab Programming for Engineers Introduction to Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir Nouri

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB Objectives: § The Advantages of Matlab § Disadvantages of Matlab § The Matlab Environment § Using Matlab as a scratch Pad Mechanical Engineering Department 2

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB Advantages and Disadvantages of 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 3

CH 1: INTRODUCTION TO MATLAB PROGRAMMING FOR ENGINEERS The Matlab Environment Matlab Command Window User input area Current directory browser Also results appear in this window Command history Mechanical Engineering Department 4

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment Mechanical Engineering Department 5

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment Script files / M-files Mechanical Engineering Department 6

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment The Edit/Debug Window Mechanical Engineering Department 7

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment Mechanical Engineering Department 8

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Figure Windows) Mechanical Engineering Department 9

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Figure Windows) Mechanical Engineering Department 10

CH 1: INTRODUCTION TO MATLAB PROGRAMMING FOR ENGINEERS The Matlab Environment (Workspace) • whos • Display the value of any variable in the WS • clear var 1 var 2 • Double-click any var in the WS browser Mechanical Engineering Department 11

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Workspace) Mechanical Engineering Department 12

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Getting Help) Mechanical Engineering Department 13

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Getting Help) Mechanical Engineering Department 14

MATLAB PROGRAMMING FOR ENGINEERS CH 1: 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 CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Getting Help) help: Search function name Mechanical Engineering Department 16

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment (Getting Help) Mechanical Engineering Department 17

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB The Matlab Environment A few important commands clc: clears command window clf: clears current figure clear: clear variables in the WS Ctrl+c: aborts the execution of the program exit: exits Matlab !(command): runs the command in the operating system (ex. dir, copy …) save/load (WS): saves and loads the WS Mechanical Engineering Department 18

MATLAB PROGRAMMING FOR ENGINEERS CH 1: 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 Mechanical Engineering Department Sequence is important 19

CH 1: INTRODUCTION TO MATLAB PROGRAMMING FOR ENGINEERS MATLAB Math & Assignment Operators § Power: (^) or (. ^) a^b or a. ^b § Multiplication: (*) or (. *) a*b or a. *b § Division: (/) or (. /) a/b or a. /b () or (. ) ba or b. a or NOTE: 56/8 = 856 § Addition: (+) a+b § Subtraction: (-) a–b § Assignment: (=) a=b Mechanical Engineering Department (assign b to a) 20

MATLAB PROGRAMMING FOR ENGINEERS CH 1: INTRODUCTION TO MATLAB HW 1 The distance traveled by a ball falling in the air is given by the equation Use the Matlab to plot the path of the ball (y) as a function of time from t=0 s to t=5 s, if Mechanical Engineering Department 21
- Slides: 21