Vous êtes sur la page 1sur 23

The Very Basics of MATLAB


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

For keeping track of

Command Window
For entering commands

So what do I do???
MATLAB behaves like a really cool calculator!
(That can be programmed to perform complex
Simply enter commands in the command window.

A statement is some instruction for the
computer to perform some task.

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

Can then modify a variable:

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

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

>>v=[1 2 3]

>>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:

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

If want smaller increments, use

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

Built in functions
MATLAB has many built in functions, for
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:

Now just plot!


Playing with the Graph

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

Colours, etc:

>> 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
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.