Top Banner
The Very Basics of MATLAB MAM1043H
23

The Very Basics of MATLAB

Jan 18, 2016

Download

Documents

carioca carioca

The Very Basics of MATLAB
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Very Basics of MATLAB

The Very Basics of MATLAB

MAM1043H

Page 2: 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.

Page 3: The Very Basics of MATLAB

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.

Page 4: The Very Basics of MATLAB

Example – Parallel Plate Capacitor

Page 5: The Very Basics of MATLAB

Example – Golf ball with spin

Page 6: The Very Basics of MATLAB

The MATLAB Desktop

Command WindowFor entering commands

WorkspaceFor keeping track of variables

Page 7: The Very Basics of MATLAB

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)

Page 8: The Very Basics of MATLAB

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

Page 9: The Very Basics of MATLAB

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.

Page 10: The Very Basics of MATLAB

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

Page 11: The Very Basics of MATLAB

Basic Math Operators

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

Page 12: The Very Basics of MATLAB

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#

Page 13: The Very Basics of MATLAB

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

Page 14: The Very Basics of MATLAB

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

Page 15: The Very Basics of MATLAB

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

Page 16: The Very Basics of MATLAB

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

Page 17: The Very Basics of MATLAB

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]

Page 18: The Very Basics of MATLAB

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

Page 19: The Very Basics of MATLAB

Plotting Graphs

• Must create vectors to plot:>>x=0:pi/10:2*pi;

>>y=sin(x);

• Now just plot!>>plot(x,y);

Page 20: The Very Basics of MATLAB

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’);

Page 21: The Very Basics of MATLAB

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

Page 22: The Very Basics of MATLAB

Still to come

• Creating our own functions.

• How can we solve linear system of equations?

• Making decisions and repeating steps.

Page 23: The Very Basics of MATLAB

What if I get stuck?

• Don’t just keep trying the same thing.• Read a textbook(s).• Use the MATLAB help. • Ask somebody else.But most importantly

Don’t give up!• EVERYBODY makes mistakes and

struggles when programming.