180 likes | 439 Views
Introduction to MATLAB. ENGR 1181 MATLAB 1. 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 : >>. MATLAB Display. Ribbon Useful operations
E N D
Introduction to MATLAB ENGR 1181 MATLAB 1
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: >>
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
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
Working with Variables • Variables defined previously can be used to do calculations • Define: a = 8; my_var = 12; >> a + my_var ans = 20 >> a*my_var^2 ans = 1152
Rules in Naming Variables • Names must begin with a letter • May contain letters, digits, and the underscore character • No spaces are allowed • MATLAB iscase 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.
MATLAB Built-in Math Functions • For trigonometric functions, x is entered inradians • 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
Saving a Script File • Script files must be saved after completion • In our class use Save Asto 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!
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)
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
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]
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
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
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
Vector - Vector Math Summary For two vectors x and y : Addition x + y or y + x Subtraction x – y or y – x Multiplication x .* y or y .* x Division x ./ y or y ./ x Exponent x .^ y or y .^ x You must alwaysuse the dot operator for Multiplication, Division, andExponent
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
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’)