introduction to matlab engr 1181 matlab 1. opening matlab students, please open matlab now. click...
TRANSCRIPT
![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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022082422/56649f1a5503460f94c2f797/html5/thumbnails/17.jpg)
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’)