logo for matrixlab-examples.com
[?] Subscribe To This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Subscribe with Bloglines


Home
Welcome Matrixmania Blog
-> Sitemap / Search <-
-> Books <-
Forums and Help
Contact
Basics Quick Matlab Guide
Matlab Tutorial
Matlab Examples
Matlab Flow Control
Boolean Logic
Plots and GUI Matlab 2D Plots
Matlab 3D Plots
Matlab GUI
Applications Calculus
Linear Algebra
Matlab Cookbook I
Matlab Cookbook II
Electrical Calculations
Probability and Stats
Finance Apps
Other Relevant Links
Notes on Computing
Online Calculators
Fun!
Your own Website?
Terms/Policies
leftimage for matrixlab-examples.com

Tutorial Lesson: Vector Algebra (Algebra with many numbers, all at once...)


You'll learn to create arrays and vectors, and how to perform algebra and trigonometric operations on them. This is called Vector Algebra.

An array is an arbitrary list of numbers or expressions arranged in horizontal rows and vertical columns. When an array has only one row or column, it is called a vector. An array with m rows and n columns is a called a matrix of size m x n.

Launch MATLAB and reproduce the following information. You type only what you see right after the '>>' sign. MATLAB confirms what you enter, or gives an answer.

Let x be a row vector with 3 elements (spaces determine different columns). Start your vectors with '[' and end them with ']'.

>> x=[3 4 5]

x =     3     4     5


Let y be a column vector with 3 elements (use the ';' sign to separate each row). MATLAB confirms this column vector.

>> y=[3; 4; 5]

y =
     3
     4
     5


You can add or subtract vectors of the same size:
>> x+x

ans =      6     8    10

>> y+y

ans =
     6
     8
    10


You cannot add/subtract a row to/from a column (Matlab indicates the error). For example:

>> x+y
??? Error using ==> plus
Matrix dimensions must agree.

You can multiply or divide element-by-element of same-sized vectors
(using the '.*' or './' operators) and assign the result to a different variable vector:

>> x.*x

ans =      9    16    25

>> y./y

ans =
     1
     1
     1

>> a=[1 2 3].*x

a =     3     8    15

>> b=x./[7 6 5]

b =     0.4286    0.6667    1.0000


Multiplying (or dividing) a vector with (or by) a scalar does not need any special operator (you can use just '*' or '/'):

>> c = 3*x

c =     9    12    15

>> d = y/2

d =
    1.5000
    2.0000
    2.5000


The instruction 'linspace' creates a vector with some elements linearly spaced between your initial and final specified numbers, for example:
r = linspace(initial_number, final_number, number_of_elements)

>> r = linspace(2,6,5)

r = 2     3     4     5     6

or

>> r = linspace(2,3,4)

r = 2.0000    2.3333    2.6667    3.0000


Trigonometric functions (sin, cos, tan...) and math functions (sqrt, log, exp...) operate on vectors element-by-element (angles are in radians).

>> sqrt(r)

ans =     1.4142    1.5275    1.6330    1.7321

>> cos(r)

ans =    -0.4161   -0.6908   -0.8893   -0.9900


Well done!
So far, so good?

Experimenting with numbers, vectors and matrices is good for you and it does not hurt! Vector algebra can even be fun if you work with a computer and a good software, can't it?

Go on!


 From 'Vector Algebra' to home

 From 'Vector Algebra' to Tutorials
 
Top

Next: 2D plots





footer for vector algebra page