Due Tuesday, February 8, 11:59 P.M.
Unlike Maple, Matlab's design focuses on providing a language for implementing numerical algorithms and working with matrices. While Maple provides a wider range of functionality, using Matlab in its area of specialization is generally more efficient and convenient. This lab will introduce you to working with matrices interactively; Matlab programming will be covered later.
As with Maple, in order to run Matlab you may need to make some changes to your environment variables. Make sure your PATH variable contains the /usr/matlab/6.1/bin directory. Now either start a new shell or type
source ~/.zprofileto update your shell environment without restarting. You should now be able to start Matlab by typing matlab at the command prompt.
The examples that follow will introduce you to all the Matlab commands you need to complete the assignment (with one exception that you must lookup yourself). Enter each of the following Matlab commands before continuing to the assignment section.
diary practiceto record all your practice commands into the textfile named practice in the current working directory.
diary off pwd ls mkdir lab5 cd lab5 pwd diary practice
[1,2,3;4 5,6;7 8 9]
M = ans
lookfor determinant help det det(M) lookfor echelon help rref rref(M)
eye(5) ones(5) triu(ans) tril(ones(5)) ones(3,5) zeros(5) zeros(5,3) diag([2,4,8]) rand rand(3,2) rand(3) diag(ans) diag(ans)
t = 1:10 u = 10:1 u = 10:-1:1 v = 0:5:100 w = 100:-0.25:90
M+5 x=3 M*x M/x ans/3
M(1,1) M(:,3) M(3,:) M(3,2:3)
scol = [2;2;2] dcol = [3 4; 5 6; 7 8] [scol dcol] [dcol; scol scol]
A=[2,4,8,16;3,9,27,81;5,25,125,625] B=[A(3,:);A(2,:);A(1,:)] A-B A+B A*B A' A*B' C=ans C*C*C C^3 C^-1 C*ans inv(C)
A.*B A.^2 A./B C^-1==inv(C)
v = 1.5*10^9; V = [v,2*v,3*v] v V format long V format long g V format short V 1.5 rats(1.5) I = eye(75); I more on I more offNote that Matlab is case sensitive, i.e. v and V are two separate variables. Terminating a Matlab command with a semicolon suppresses the output for that command.
bigmat=eye(5000); who whos size(bigmat) clear bigmat whos size(bigmat) clear whosBe careful about using the clear command without any arguments, this will erase all your currently saved data without prompting for confirmation.
quad('X.^2',-1,1)
quad('1./(2*sqrt(X))',4,16)
quad('cos(X)',0,pi/2)
X=-1:0.1:1
Y=X.^2
dY=diff(Y)
dX=diff(X)
yp=dY./dX
Note that the function passed as the first argument to the quad
command is actually a matrix formula, so you should use componentwise
operators in the function definition unless performing arithmetic between
a matrix and a scalar.
plot(X,Y) plot(X,yp) length(X) length(yp) X2=X(2:length(X)); length(X2) plot(X2,yp) plot(X,Y,X2,yp)
syms syms x int(x^2) int(x^2,-1,1) diff(x^2) syms clear syms x=0:10 diff(x)Note that the behavior of the diff and int commands behave differently when the variable x is declared symbolically. It turns out that there are several separate Matlab programs called diff; Matlab decides which one to use depending on whether x is symbolic. This is called function overloading; the Matlab help files will usually tell you when a function is overloaded. You can read help on the symbolic form of diff by typing help sym/diff.
quitAlternately, just type clear before moving on to the assignment.
clear diary off diary assignmentand type the commands for each of the remaining tasks.
| 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 2 | 2 | 2 | 0 | 0 | 0 |
| 0 | 0 | 0 | 2 | 2 | 2 | 0 | 0 | 0 |
| 0 | 0 | 0 | 2 | 2 | 2 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 3 | 3 | 3 |
| 5x1 | + | 3x2 | + | 2x3 | = | 0 |
| 8x1 | + | 4x2 | + | 2x3 | = | -3 |
| 3x1 | + | 5x2 | + | 7x3 | = | 6 |