Vous êtes sur la page 1sur 23

The Very Basics of MATLAB

MAM1043H

What is MATLAB?
MATrix LABoratory
MATLAB is a numerical computing
environment (Google)
Useful for numerical scientific and
engineering calculations
MANY useful toolboxes available for a
wide variety of applications.

Why do I need it?


Very few real world problems can be
solved analytically!
Have to resort to computational methods
to solve problems.
MATLAB is a powerful environment for
solving such problems.

Example Parallel Plate Capacitor

Example Golf ball with spin

The MATLAB Desktop


Workspace
For keeping track of
variables

Command Window
For entering commands

So what do I do???
MATLAB behaves like a really cool calculator!
(That can be programmed to perform complex
calculations)
Simply enter commands in the command window.
Try:
>>5*3
>>sin(pi/2)

Statements
A statement is some instruction for the
computer to perform some task.
Example:
>>a=2

Tells the MATLAB to create a new


variable a and assign it a value of 2.
Key Idea:
= means to assign a value obtained on the RHS
to the variable on the LHS

Statements
Can then modify a variable:
>>a=a*3

Tells MATLAB to take the value of what


was in the variable a, multiply it by 3, and
then assign the result to a again.

Key Idea:
Variables will keep their value until some instruction
is given to change it.

Some Useful Commands

clc Clears the command window


clear Clears variables from the workspace
close all Closes all figure windows
who Lists all the variables in the workspace

Basic Math Operators

Addition: >>a=2+3
Subtraction: >>a=2-3
Multiplication: >>a=2*3
Division: >>a=2/3
Powers: >>a=2^3

Variables
Rules for variable names:
Consist of letters (a..z), numbers (0..9) and
underscore ( _ )
Must start with a letter (a..z)
Variables are case sensitive, so velocity and
Velocity are different variables!

Ok: a, a2, a_2


NOT Ok: a 2, 2a, a#

Types of Variables
Scalars:
>>a=2

Vectors:
>>v=[1 2 3]

Matrices:
>>A=[1 2 3; 4 5 6; 7 8 9]
Key Idea:
All variables in MATLAB are matrices!!

Operations on Vectors
For the vector:
>>v=[1 2 3]

try the following operations:


Addition: >>v=v+3
Subtraction: >>v=v-1
Multiplication: >>v=v*4
Division: >>v=v/2
Powers: >>v=v^2

Operations on Vectors
The power operation gives an error! Why??
Does this make sense?
>>[1 2 3]^2
which means
>>[1 2 3]*[1 2 3]
How do we multiply vectors together? Is there a
unique way of doing this?...
The answer is NO!!

Array Operators
Performs operation on each component in
two vectors (must be the same size!):
>>[1 2 3].*[1 2 3]
>>[2 8 27]./[1 2 3]
>>[1 2 3].^2

Constructing Arrays
Use the colon : operator:
>>x=1:10

same as x=[1 2 3 4 5 6 7 8 9 10]


If want smaller increments, use
>>x=1:0.5:4

same as x=[1 1.5 2 2.5 3 3.5 4]

Built in functions
MATLAB has many built in functions, for
example:
Trigonometric (sin(),cos(), etc)
Exponentials, logs (log(),exp())
Roots (sqrt())
Plotting (plot(),surf(), etc)
Vector product (dot(), cross() )

plus more
And you can create your own!! (Later.)

Plotting Graphs
Must create vectors to plot:
>>x=0:pi/10:2*pi;
>>y=sin(x);

Now just plot!


>>plot(x,y);

Playing with the Graph


Add a grid: >>grid on;
Add a title: >>title(My Sine Graph);
Add labels: >>xlabel(\theta);
>>xlabel(sin(\theta));

Colours, etc:

>>plot(x,y,r);
>> plot(x,y,rx);

An exercise in dropping a ball


Know: x=x0 + v0t + a t2
Plot for t from 0 to 5 sec
Dropped from height of 100m.

Still to come
Creating our own functions.
How can we solve linear system of
equations?
Making decisions and repeating steps.

What if I get stuck?


Dont just keep trying the same thing.
Read a textbook(s).
Use the MATLAB help.
Ask somebody else.
But most importantly
Dont give up!
EVERYBODY makes mistakes and
struggles when programming.

Vous aimerez peut-être aussi