Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB Students, please open MATLAB now. CLICK...

17
Introduction to MATLAB ENGR 1181 MATLAB 1

Transcript of Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB Students, please open MATLAB now. CLICK...

Page 1: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Introduction to MATLABENGR 1181MATLAB 1

Page 2: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Opening MATLAB Students, please open MATLAB

now.

CLICK on the shortcut icon → • Alternatively, select…

start/All programs/MATLAB

The following prompt should appear in

the command window: >>

Page 3: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

MATLAB Display Ribbon• Useful operations

Current Directory• List of files

Workspace• Defined variable

values

Command history• Displays what has

been typed

Command prompt

Command Window

Page 4: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

MATLAB’s Working Directory Current working

directory is where files are saved and run from

When you first start MATLAB, it is a good practice to change the working directory to your Z: drive or USB device

Browse icon available

Page 5: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Working with VariablesVariables defined previously can be used to do calculations

Define: a = 8; my_var = 12;

>> a + my_varans = 20>> a*my_var^2ans = 1152

Page 6: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Rules in Naming Variables Names must begin with a letter

May contain letters, digits, and the underscore character• No spaces are allowed

MATLAB is case sensitive, it distinguishes between uppercase and lowercase letters

Avoid naming variables with currently defined MATLAB functions• Ex: exp, sin, cos, sqrt, length, mean,

max, min etc.

Page 7: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

MATLAB Built-in Math Functions For trigonometric functions, x is

entered in radians• sin(x), cos(x), asin(x), acos(x),

tan(x), cot(x)

To enter x in degrees…• sind(x) where x is in degrees

Ex: To calculate sin(/2)>>sin(pi/2) is defined as “pi”

ans = 1

Page 8: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Saving a Script File

Script files must be saved after completion

In our class use Save As to your Z:\ or USB drive• This should be the same as the working

directory you specified upon starting MATLAB• SAVE YOUR WORK AS YOU GO!

Page 9: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Saving a Script File The name of the script file is governed by the following

rules:• No spaces are allowed• The name cannot start with a numeric• No special characters are allowed (except

underscore)Allowed: Not Allowed:Prob1a.m Prob 1a.m (blank space)Prob_1a.m 1aProb.m (can’t start with

numeric)Prob-1a.m (no special

characters)

Page 10: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Useful Programming Commands

To clear the command window, use “clc”>> clc

To send a text message to the command window or display contents of a variable, use “disp(…)”

>> disp(‘Brutus Buckeye’) NOTE: single quotes

Page 11: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

What is a vector?A vector is an ordered list of several numbers and is a one-dimensional array

Row Vectors are horizontal: [1 2 3 4]

Column Vectors are vertical: [1 2

3 4]

Page 12: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Row Vectors vs. Column Vectors You need to choose which type to

create

This will depend on your data and the type of calculations you need to do

If you choose the 'wrong' type, you can transpose your row vector to a column vector, or a column to a row

Page 13: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Creating Row Vectors A row vector is created by typing the

elements inside square brackets [ ]

The elements are separated by commas OR spaces

You will likely assign your vector to a variable• Remember to use valid variable

names

Page 14: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Creating Column Vectors A column vector is separated by typing

the elements inside square brackets [ ]

The elements are separated by a semicolon OR by an 'enter'

You will likely assign your vector to a variable• Remember to use valid variable

names

Page 15: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Vector - Vector Math Summary

For two vectors x and y :Addition x + y or y + xSubtraction x – y or y – x

Multiplication x .* y or y .* x

Division x ./ y or y ./ x

Exponent x .^ y or y .^ xYou must always use the dot operator

for Multiplication, Division, and Exponent

Page 16: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Function: plot() Ex: >> plot(x_vec, y_vec)

Will generate a line where x_vec is the vector of x values and y_vec is the vector of corresponding y values

Page 17: Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.

Plot Formatting Title: title('Distance vs. Intensity')

Axis Labels: xlabel('Intensity , w/m^2') ylabel('Distance, m')

Legend: legend('Data Set 1', 'Data Set 2’)