Está en la página 1de 5

Review of Matlab Programming

We will be using Matlab as our computing environment. So Ill follow our text and begin with a
review of some of the features of this program. An important consideration for our purposes is that
Matlab has been written so as to handle matrices with ease basically every variable we use is a
matrix with ordinary scalars just 1 by 1 matrices.
>> format compact
>> %entering a matrix
>> a=[1 2 3; 4 7 9; 5 4 3]
a =
1
2
3
4
7
9
5
4
3
>> b=[2 4;1 9;3 6];
>> %matrix multiplication
>> a*b
ans =
13
40
42
133
23
74
>> %inversion
>> c=inv(a)
c =
2.5000
-1.0000
0.5000
-5.5000
2.0000
-0.5000
3.1667
-1.0000
0.1667
>> %check
>> c*a
ans =
1.0000
0.0000
-0.0000
-0.0000
1.0000
-0.0000
0.0000
0.0000
1.0000
>> %linear equation solution ax=f
>> f=[1;2;3];
>> x=a\f
x =
2.0000
-3.0000
1.6667
>> %check
>> a*x
ans =
1.0000
2.0000
3.0000
>> %matrix decomposition pa=lu
>> [l,u,p]=lu(a)
1

l =
1.0000
0.8000
0.2000

0
1.0000
0.3158

0
0
1.0000

5.0000
0
0

4.0000
3.8000
0

3.0000
6.6000
0.3158

u =

p =
0
0
1
0
1
0
1
0
0
>> %check (note: the conjugate transpose of a matrix m is produced by m)
>> a-p*l*u
ans =
0
0
0
0
0
0
0
0
0
>> %matrix decomposition a=qr (q orthogonal, r upper triangular)
>> [q,r]=qr(a)
q =
-0.1543
-0.2630
-0.9524
-0.6172
-0.7270
0.3008
-0.7715
0.6342
-0.0501
r =
-6.4807
-7.7152
-8.3324
0
-3.0783
-5.4296
0
0
-0.3008
>> %checks: qq=I
>> q*q
ans =
1.0000
-0.0000
-0.0000
-0.0000
1.0000
-0.0000
-0.0000
-0.0000
1.0000
>> a-q*r
ans =
1.0e-014 *
0.0333
0.2665
0.5329
0
0.0888
0.1776
0.0888
0.2665
0.5329
Matlab has a large built-in library of functions. Most of these take a matrix argument. That is, if X
is an m n real matrix, and f : R R is some built-in function like sine, cos, exp, etc., then f (X)
is an m n matrix with elements f (xij ). Similar statements apply of functions defined for complex
values. In addition to the usual elementary functions Matlab supplies many special functions like
Bessel functions, etc.
The feature of Matlab that will be of most use to us is the possibility of creating new functions
2

using Matlabs C-like programming language. These new functions can use all of Matlabs built-in
capabilities. As an example, lets create a function that does a least squares fit of a straight line to
a n 2 data array

x1 y1
..
..
.
.
xn

yn

The problem is to fit P


the line y = ax + b to this data in such a way that the sum of the squares of
n
the errors, E(a, b) = i=1 (yi axi b)2 =minimum. Recall from your Calculus courses that we
find the minimum of E by setting the partial derivatives
E
= 0.
b

E
= 0,
a
This gives the system of equations
nb + (

Pn

i=1

xi )a

Pn
Pn
( i=1 xi )b + ( i=1 x2i )a

=
=

Pn

i=1

Pn

i=1

yi

xi yi

First, lets create some data and store it in a disk file using the built-in function fprintf which
should be familiar to C programmers:
>> x=[20.5 20.6 20.65 20.75 20.8 20.85 20.9 21];
>> y=[118.1 118.25 118.2 118.4 118.4 118.5 118.45 118.5];
>> fid=fopen(data.txt,w+);
>> fprintf(fid,%10.5f %10.5f\n, [x;y]);
>> fclose(fid);
%============================
%this results in the file
20.50000 118.10000
20.60000 118.25000
20.65000 118.20000
20.75000 118.40000
20.80000 118.40000
20.85000 118.50000
20.90000 118.45000
21.00000 118.50000
%============================
In programming our function, well suppose that the vectors x and y are available in Matlab. We
could program our function to read them in, but well assume they have already been read into
Matlab by the built-in function fscanf. (There other functions available for reading and writing
data try help iofun.) Heres how to use fscanf.
>> yy=fscanf(fid,%f %f,[2,inf]);
>> yy=yy
>> x=yy(:,1); y=yy(:,2);
3

>> x
x =
20.5000
20.6000
20.6500
20.7500
20.8000
20.8500
20.9000
21.0000
>> y
y =
118.1000
118.2500
118.2000
118.4000
118.4000
118.5000
118.4500
118.5000
>> fclose(fid)
ans =
0
Now we create our program file using any convenient text editor or the one built into Matlab.
function [av]=leastsq(x,y)
% function [a,b]=leastsq(x,y)
% Input: x, y vectors of same length, n
% Output: av=[b;a] coefficients in best fit straight line y=ax+b
n=max(size(x)); m=max(size(y));
if(n~=m) error(input vectors not same size); end
c=[n sum(x);sum(x) sum(x.^2)];
z=[sum(y); dot(x,y)];
av=c\z;
Notice how we use the functions max, size, error, sum, dot, and the matrix inverter \. Think of how
much longer our function would be if we had to create all these ourselves. Running this program on
the data gives:
>> av=leastsq(x,y)
av =
100.8000
0.8455
>> xx=linspace(20.4,21.0);
>> yy=av(2)*xx+av(1);
>> plot(x,y,o,xx,yy)
4

118.6

118.5

118.4

118.3

118.2

118.1

118
20.3

20.4

20.5

20.6

20.7

20.8

20.9

21

También podría gustarte