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.
Alternative MATLAB Introduction Lecture authored by Annie Abell
MATLAB is a powerful program for numerical computations, plotting and programming.
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:
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
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.
First Parentheses (innermost pair first)
Third Multiplication and division
Fourth Addition and subtraction
Task : Find the average of 7 and 8
>> 7 + 8 / 2
…not what we wanted
Task: Add the cube root of 27 to the 5th root of 32
>> 27 ^ 1/3 + 32 ^ 0.2
11 … not what we wanted
>> 27 ^ (1/3) + 32 ^ 0.2
>> x = 5
>> x = x + 5
This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.
We can use the variables defined previously to do calculations.
Define the following: a=8, my_var =12
>> a + my_var
>> a * my_var^2
1Name starts with numeral
My Name spaces not allowed
My-Name special characters not allowed
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
In MATLAB, trigonometric functions use radians. So for these examples, x is in radians:
MATLAB also has equivalent functions where x is in degrees:
Task: calculate sin(∏/ 2). Note: ∏ is defined by MATLAB as pi.
Task: calculate e2
>> e ^ 2
??? Undefined function or variable \'e\'
How do we fix this?
Task: calculate sin2x, where x = ∏/4
>> x = pi/4
Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.
>> x = 30
>> x = x * pi / 180
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)
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
To clear the command window: clc
To clear all variables previously defined: clear
>> x = 5
??? Undefined function or variable \'x\'
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’)
To display any variable contents, do not use quotes:
>> x = 5
This is good practice! You don\'t need all of your program displayed in the command window, just the relevant info.
>> x = 6
>> y = 7; …what happens when you type this?
Pythagoras\' Theorem: Hypotenuse2 = Height2 + Base2
You must print two things for every MATLAB assignment:
… if you do not turn in both, you will lose points
Example Command Window Printout
Introduction to Matlab