MATLAB introduction and Video tutorial
December 22nd 2010 20:15
Some examples:
1) save all typing in a file
diary on;
diary filename;
2) edit file on shell
!ed myfile.m;
or
! vi myfile.m;
3) Condition:
if(decision<3), b=1; elseif(decision >3) else b=0; end
4) Solve matrix equation: AX=B,
X = A\B
5) loops:
a = [ 0.8 0.1; 0.2 0.9 ]; x = [ 1; 0 ]; for i = 1:20, x = a*x, end
6) Comment line: use %
7) batch job:
nice matlab < script.m >& script.out &
8)function:
function [x] = gaussElim(A,b)
% File gaussElim.m
N = max(size(A));
% Perform Gaussian Elimination
for j=2:N,
for i=j:N,
m = A(i,j-1)/A(j-1,j-1);
A(i, : ) = A(i, : ) - A(j-1, : )*m;
b(i) = b(i) - m*b(j-1);
end
end
% Perform back substitution
x = zeros(N,1);
x(N) = b(N)/A(N,N);
for j=N-1:-1:1,
x(j) = (b(j)-A(j,j 1 : N)*x(j 1 : N))/A(j,j);
end
9) 2d Matrix:
A = [1 2 3 6; 4 3 2 3; 9 9 1 -2; 4 2 2 1]
10) output plot to ps file
print -dps error.ps
11) Output precision and format:
format long;
fprintf('%.15f\n',1.2345 6) (output 1.234560000000000, equal to long);
b=12.12345678901234567;
printf ('value of b is %1.10e\n',b) (output value of b is 1.2123456789e 01);
12) plots vector Y versus vector X
PLOT(X,Y)
t = 0:.03:10; y = sin(t); plot(t,y) ( 0 to 10 in step of 0.3)
[x,y] = meshdom(-2 : 0.2 : 2, -2 : .02 : 2); z = x .* exp(-x.^2 - y.^2); mesh(z)
title('Best Least Squares Fit')
plot(x,y1,'--',x,y2,' : ',x,y3,' ')
13) The line- and mark-types are
Linetypes: solid (-), dashed (\tt--). dotted ( : ), dashdot (-.)
Marktypes: point (.), plus ( ), star (*), circle (o), x-mark (x)
10) Fitting: polynomial ,
polyfit(x,y,n) where x,y are the data vectors and 'n' is the order of the
polynomial for which the least-squares fit is desired. The command
'polyfit' returns a vector whose elements are the coefficients of
the polynomial . The first element is the coefficient of the highest order of x and the last element is the
coefficient of the lowest order
11) Text string:
Text strings are entered into MATLAB surrounded by single quotes. For example,
s = 'This is a test'
Text strings can be displayed with the function disp. For example:
disp('this message is hereby displayed')
Error messages are best displayed with the function error
error('Sorry, the matrix must be symmetric')
12) Comparing efficiency of algorithms: flops and etime
The MATLAB function clock gives the current time accurate to a hundreth of a second (see help clock). Given two such times t1 and t2, etime(t2,t1) gives the elapsed time from t1 to t2. One can, for example, measure the time required to solve a given linear system A x = b using Gaussian elimination as follows:
t = clock; x = A\b; time = etime(clock,t)
You may wish to compare this time---and flop count
Video tutorial:
Click video to see Video, click tiny up arrow in control bar to return to menu
1) save all typing in a file
diary on;
diary filename;
2) edit file on shell
!ed myfile.m;
or
! vi myfile.m;
3) Condition:
if(decision<3), b=1; elseif(decision >3) else b=0; end
4) Solve matrix equation: AX=B,
X = A\B
5) loops:
a = [ 0.8 0.1; 0.2 0.9 ]; x = [ 1; 0 ]; for i = 1:20, x = a*x, end
6) Comment line: use %
7) batch job:
nice matlab < script.m >& script.out &
8)function:
function [x] = gaussElim(A,b)
% File gaussElim.m
N = max(size(A));
% Perform Gaussian Elimination
for j=2:N,
for i=j:N,
m = A(i,j-1)/A(j-1,j-1);
A(i, : ) = A(i, : ) - A(j-1, : )*m;
b(i) = b(i) - m*b(j-1);
end
end
% Perform back substitution
x = zeros(N,1);
x(N) = b(N)/A(N,N);
for j=N-1:-1:1,
x(j) = (b(j)-A(j,j 1 : N)*x(j 1 : N))/A(j,j);
end
9) 2d Matrix:
A = [1 2 3 6; 4 3 2 3; 9 9 1 -2; 4 2 2 1]
10) output plot to ps file
print -dps error.ps
11) Output precision and format:
format long;
fprintf('%.15f\n',1.2345 6) (output 1.234560000000000, equal to long);
b=12.12345678901234567;
printf ('value of b is %1.10e\n',b) (output value of b is 1.2123456789e 01);
12) plots vector Y versus vector X
PLOT(X,Y)
t = 0:.03:10; y = sin(t); plot(t,y) ( 0 to 10 in step of 0.3)
[x,y] = meshdom(-2 : 0.2 : 2, -2 : .02 : 2); z = x .* exp(-x.^2 - y.^2); mesh(z)
title('Best Least Squares Fit')
plot(x,y1,'--',x,y2,' : ',x,y3,' ')
13) The line- and mark-types are
Linetypes: solid (-), dashed (\tt--). dotted ( : ), dashdot (-.)
Marktypes: point (.), plus ( ), star (*), circle (o), x-mark (x)
10) Fitting: polynomial ,
polyfit(x,y,n) where x,y are the data vectors and 'n' is the order of the
polynomial for which the least-squares fit is desired. The command
'polyfit' returns a vector whose elements are the coefficients of
the polynomial . The first element is the coefficient of the highest order of x and the last element is the
coefficient of the lowest order
11) Text string:
Text strings are entered into MATLAB surrounded by single quotes. For example,
s = 'This is a test'
Text strings can be displayed with the function disp. For example:
disp('this message is hereby displayed')
Error messages are best displayed with the function error
error('Sorry, the matrix must be symmetric')
12) Comparing efficiency of algorithms: flops and etime
The MATLAB function clock gives the current time accurate to a hundreth of a second (see help clock). Given two such times t1 and t2, etime(t2,t1) gives the elapsed time from t1 to t2. One can, for example, measure the time required to solve a given linear system A x = b using Gaussian elimination as follows:
t = clock; x = A\b; time = etime(clock,t)
You may wish to compare this time---and flop count
Video tutorial:
Click video to see Video, click tiny up arrow in control bar to return to menu
| 18 |
| Vote |
subscribe to this blog















