Introduction to MATLAB. Alternative MATLAB Introduction Lecture authored by Annie Abell. Introduction to MATLAB. MATLAB is a powerful program for numerical computations, plotting and programming . Use it as a calculator. Define variables and use them in calculations.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Introduction to MATLAB
Alternative MATLAB Introduction Lecture authored by Annie Abell
MATLAB is a powerful program for numerical computations, plotting and programming.
Open MATLAB now:
Click on the shortcut icon
or select: start / All programs / MATLAB
There will be a fairly long initialization process, then following prompt should appear in the command window:
>>
Command prompt
Workspace (variable values)
Current Directory (lists file, etc.)
Command History (what has been typed)
Command Window (where commands are entered)
When you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device.
Keep all of your files in the same folder so you do not have to constantly change your directory
Order of Precedence
Higher-precedence operations are executed before lower-precedence operations (like Excel or graphing calculators).
If two operations have the same precedence, then the expression is executed from left to right.
PRECEDENCEOPERATION
FirstParentheses (innermost pair first)
SecondExponentiation.
ThirdMultiplication and division
FourthAddition and subtraction
Precedence of Operations
Task : Find the average of 7 and 8
>> (7+8)/2
ans =
7.500
… correct!
>> 7 + 8 / 2
ans =
11
…not what we wanted
Precedence of Operations
Task: Add the cube root of 27 to the 5th root of 32
>> 27 ^ 1/3 + 32 ^ 0.2
ans =
11 … not what we wanted
>> 27 ^ (1/3) + 32 ^ 0.2
ans =
5
Variable Definition
>> x = 5
x =
5
>> x = x + 5
x =
10
This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.
Calculations with Variables
We can use the variables defined previously to do calculations.
Define the following: a=8, my_var =12
>> a + my_var
ans =
20
>> a * my_var^2
ans =
1152
Rules About Variable Names
Examples of Variable Names
Acceptable:
My_name
MyName
myname1
Unacceptable:
1Name starts with numeral
My Name spaces not allowed
My-Name special characters not allowed
Built-In Math Functions: Examples
exp(x)exponential (ex)
log(x)natural logarithm (log base 'e')
log10(x)base 10 logarithm (log)
sqrt(x) square root
abs(x) absolute value
Built-In Math Functions
In MATLAB, trigonometric functions use radians. So for these examples, x is in radians:
MATLAB also has equivalent functions where x is in degrees:
Built-In Math Functions: Examples
Task: calculate sin(∏/ 2). Note: ∏ is defined by MATLAB as pi.
>> sin(pi/2)
ans =
1
Built-In Math Functions: Examples
Task: calculate e2
>> e ^ 2
??? Undefined function or variable 'e'
How do we fix this?
>> exp(2)
ans =
7.3891
Built-In Math Functions: Examples
Task: calculate sin2x, where x = ∏/4
>> x = pi/4
x =
0.7854
>>sin(2*x)
ans =
1
Built-In Math Functions: Examples
Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.
>> x = 30
x =
30
>> x = x * pi / 180
x =
0.5236
Built-In Math Functions: Examples
Task: Using (cos2x sin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.
>> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x)
ans =
0.0817
Saving a Script File
Saving a Script File
Valid File Names
Invalid File Names
Prob-1a.m Special Characters not allowed
Prob 1a.m Spaces not allowed
1aProb.m Cannot start with number
Useful Programming Commands
To clear the command window: clc
>> clc
To clear all variables previously defined: clear
>> x = 5
x =
5
>> clear
>> x
??? Undefined function or variable 'x'
The Display Function: disp()
This function displays a message to the command window. It can be a string of text or variablecontents.
To display your message as text,use single quotes around your message:
>> disp(‘Brutus Buckeye’)
Brutus Buckeye
The Display Function: disp()
To display any variable contents, do not use quotes:
>> x = 5
x =
5
>> disp(x)
x =
5
Suppressing Output
This is good practice! You don't need all of your program displayed in the command window, just the relevant info.
>> x = 6
x =
6
>> y = 7;…what happens when you type this?
Script File Example
Pythagoras' Theorem: Hypotenuse2 = Height2 + Base2
Hypotenuse
Height
Base
How To Turn In All Assignments
You must print two things for every MATLAB assignment:
… if you do not turn in both, you will lose points
Example Script File Printout
Example Command Window Printout
Self Check… Do you understand these concepts?
Homework Assignment
MAT-01
Introduction to Matlab