First, log on to your account. If you are unable to do that, please get hold of me. On your first UNIX prompt type
% mkdir matlab (or whatever you wish to name this directory)
% cd matlab
Then type
% matlab
You should get a brief graphic display and then the prompt
>>
Type the command
>> A = randn(5,5)
This generates a random 5 by 5 matrix according to the normal distribution.
Open a second X-window and also type
% cd matlab
You will use this second X-window to download and use m-files.
In the matlab window type
>> Ainv=inv(A)
Type
>> I= eye(5,5)
What do inv and eye do? Use help command if this is not obvious.
Compute
>> R= I-Ainv*A
Is the answer what you expected? Now type
>> B=ones(5,5)
and type
>> inv(B)
Type
>> help sum
Use the sum function to sum all of the elements in A.
Type
>> ii=[1 4 5]
>> jj=[2 3 5]
>> C=A(ii,jj)
What did you just do? Do you now see how MATLAB indexes arrays? What is A(:,1: 3 ), A(2:4 ,:) and A(2 : 4, 1 :3 )?
Notice MATLAB automatically prints the results of any computation on the screen. Under some circumstances, that is not a good idea. If we type
>> ii=[1 4 5];
>> jj=[2 3 5];
>> C=A(ii,jj);
then MATLAB does its work ``silently.'' But everything is still there. Type
>> C
to display the answer.
Now a brief introduction to plotting.
Type
>> inc=pi/25
>> x=0:inc:2*pi
>> y=sin(x)
>> plot(x,y)
To label the graph type
>> title('Plot of sin x for one period')
>> xlabel('Domain - From zero to 2*pi')
>> ylabel('Range - From -1 to 1')
You can print the plot clicking on ``File'' and then on ``Print'' and then click on ``Print.'' DO NOT DO THIS NOW. For now, click ``Cancel.'' Print it at your convenience some other time. If you click on ``Save As..'' in the file menu, you can save the graph as .ps (PostScript) file which can be viewed with ghostview.
Type
>> diary
>> a=1
>> b=1e10
>> c=1
>> [x1,x2,ier]=quadroots0(a,b,c)
>> [y1,y2,ier]=quadroots(a,b,c)
>> z=roots([a b c])
>> diary off
In your other window, look in your diary file.
If you want to know what ``roots'' does, type ``help roots.''
Here are a couple of other functions that you will become familiar with.
Type
>> eps
This is the maximum relative spacing between two floating point numbers.
>> realmax
This the largest floating point number.
>> 2*realmax
Does this get what you expected?
>> x= realmin
This is the smallest ``useful'' floating point number. Now try
>> x/2, x*x
Explain this result.
Next we have two functions that you will learn more about this semester, lu and qr.
>> A=ones(5,5)+sqrt(eps)*rand(5,5)
This will yield a mildly ill-conditioned matrix, close to singular, but we can work with it.
>> b=[1:5]'
Don't forget the transpose. We are going to solve A x = b three different ways.
>> format short e
The e format tells us more of what we need to know. (You might try >> help format at this point.)
>> x1=A\b
This asks MATLAB to solve it for you.(A\b stands for A^(-1) *b), allowing you to bypass Chapter Four.
Here are the main steps MATLAB uses to solve the problem.
>> [L,U]=lu(A)
This is the LU decomposition from Chapter Six. L is a row permutation of a lower triangular matrix, U is an upper triangular matrix. The solve requires two more steps. The matrix A satisfies A = L*U. Then, A^(-1) =U^(-1) * L^(-1).
>> y2=L\b
>> x2= U\y2
After this, type
>> demo
and choose whichever demo you wish.