Call Now: (800) 537-1660  
The Algebra Buster
The Algebra Buster


May 25th









May 25th

MatLab Basics

1) Solving a system of coupled linear equations :
Consider the system:
3x + 2y = 7
10x – 3y = 4

the Matrix equivalent of this system of equations can be re presented as :

which we can write as:

If the matrix equation is left multiplied by the inverse of matrix A (denoted as A-1), we get the
following:

In Matlab, we use the following commands:

>>A=[3 2;10 -3] define matrix A
>>b=[7;4] define column vector b
>>soln=inv(A)*b ( defined as the variable ‘soln’) =

Matlab gives back:

notes on matrix notation in Matlab:

a) [2 3 4] is a (1x3) row vector spaces separate the column values
b) [2;3;4] is a (3x1) column vector semi-colons separate the rows
c) elements of a matrix:

A(1,1) element in row 1, column 1
A(1,:) row 1, all columns (the ‘:’ symbol means all)
A(:,2) all rows, 2nd column
A(2:3,2:3) sub-matrix consisting of rows 2 and 3, columns 2 and 3

2) if the system of equations is not linearly independent (for instance, if the coefficient matrix is
the following):

>>A=[1 2 3;-2 8 -5;-8 44 -19]
>> inv(A)

Matlab will return:
Warning: Matrix is singular to working precision.

ans =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf

This result means that one of the rows of the matrix can be formed as a linear combination of the
other
2 rows. In the above example:

row #3 = (2* row #1) + (5* row #2).

This situation may arise when working material balance problems for a complex multi -unit
process. You may happen to write down many different mass balances for the different subsystems
(overall balance & several component balances on each sub-system) and not
immediately notice that some of your equations are not independent.

An easy way to test for independence is the determinant of a matrix. For the above example:
>>det(A)
ans = 0

If det(A) = 0, the system is NOT linearly independent and cannot be solved because there are
more unknowns than equations . Therefore, you must search for another balance relation (that is
independent) to solve the problem.

3) plotting (x,y) data

>>x=[1 2 3 4]
>>y=[2 7 12 15]
>>plot(x,y) xy plot, points are joined by a line
>>plot(x,y,’o’) xy plot, points are given the ‘o’ (circle) symbol
>>plot(x,y,’o’,x,y) xy plot, points are o’s joined by a line

>>plot(x1,y1,’o’,x1,y1,x2,y2,’x’,x2,y2)

xy plot of 2 data sets

(x1,y1) as circles , joined by a line AND
(x2,y2) as Xs, joined by a line

to label your plot, use the label commands:
>>xlabel(‘my x label’), ylabel(‘my y label’), title(‘my plot title’)
note: all arguments to label commands must be strings with single quotes ‘dada dah’

4) plotting a function

a) define a function f (it must be a string, enclosed in single quotes):
>>f=’x^2+2*x-7’
>>ezplot(f)

b) to specify a specific range for the plot (xmin to xmax and ymin to ymax), add the limits to the
ezplot command:
>>ezplot(f,[xmin,xmax,ymin,ymax])

c) labeling the graph using ezplot () is the same as the plot() command: xlabel(‘label’) etc.

d) putting multiple plots on the same graph, use the hold command:
>>ezplot(‘function 1’)
>>hold on
>>plot(x,y,’o’)

every plot or ezplot statement after the “hold on” command will be put into the current graph
window.

5) solving non-linear equations

a) solution to x 2 + ex = 40
>>soln=solve(‘x^2+exp(x)=40’) arguments to solve must be a function string
soln= 3.3577789759359147922578614689813

b) solution to x = log (1/ x)
>>soln=solve(‘x=log(1/x)’)
soln = lambertw(1) the answer is a complex function called the lambertw

>>double(soln) we can “type cast” the result to get a numerical value
ans = 0.5671

c) solutions of a polynomial : x3 - 2x2 + 7 = 0
>>soln=solve(‘x^3-2*x^2+7=0’)
soln= -1/6*(692+12*3297^(1/2))^(1/3)-8/3/(692+12*3297^(1/2))^(1/3)+2/3

1/12*(692+12*3297^(1/2))^(1/3)+4/3/(692+12*3297^(1/2))^(1/3)+2/3+1/2*i*3^(1/2)*(-
1/6*(692+12*3297^(1/2))^(1/3)+8/3/(692+12*3297^(1/2))^(1/3))

1/12*(692+12*3297^(1/2))^(1/3)+4/3/(692+12*3297^(1/2))^(1/3)+2/3-1/2*i*3^(1/2)*(-
1/6*(692+12*3297^(1/2))^(1/3)+8/3/(692+12*3297^(1/2))^(1/3))

these are ugly, so type cast the solution:
>>double(soln)
ans =
-1.4288
1.7144 - 1.4000i
1.7144 + 1.4000i

e) solving simultaneous equations :

>>soln=solve(‘x^2+sqrt(y)=8’,’x^3+7*x*y=12’)
soln =

there are 5 (x,y) solutions, represented as 2 column vectors to recover these solutions

>> double(soln.x) ans = OR >>double(soln.y) ans =
 

The only real solution is x=0.0268 and y=63.9885.

6) Regression: the data points are (2,3), (3,4), (4,6), (5,8), and (8,10)

define column vectors for the x and y data:
>>X=[2;3;4;5;8]
>>Y=[3;4;6;8;10]

define a constant column vector (same # of 1’s as there are data points):
>>C=[1;1;1;1;1]

carry out the regression
>>regress(Y,[X C])
ans =
1.2075
0.8868

This means that the best-fit regression line is: y= 1.2075*x + 0.8868
plot the data points and the regression line
>> plot(X,Y,’o’)
>> hold on
>> ezplot(‘1.2075*x+0.8868’,[0,10]), xlabel(‘time’), ylabel(‘flowrate’)

Prev Next
 
Home    Why Algebra Buster?    Guarantee    Testimonials    Ordering    FAQ    About Us
What's new?    Resources    Animated demo    Algebra lessons    Bibliography of     textbooks
 

Copyright © 2009, algebra-online.com. All rights reserved.