The aim of this assignment is to get some exposure to Maple and Mathematica, two symbolic computing packages, and to Matlab, a numerical computing package. These are commercial packages available for both UNIX and Windows environments. You are tempted to ask which one is better...to this the answer is the usual one: "They are all equally terrible, but they are terrible in different ways, therefore, learn to use them all".
math.arizona.eduwhich is in the directory
/incoming/restrepo.
diary hw5.txt(Every command should be followed by hitting ``return'' key). The command diary will record your MATLAB session in the file hw5.txt. Now enter
help diaryto receive on-line information about diary. You can get help on any MATLAB command that you are not sure how to use. you can also use the more useful lookfor command.
a=[1,2,3]You get a row vector or 1X 3 matrix a . Enter
b=[1;2;3]You get a column vector or 3X1 matrix b. Notice that rows of a matrix are separated by ``;'' but entries on the same row are separated by ``,'' or blank space. Enter
A = b*aYou get a 3X3 matrix A which is the product of b with a. Enter
S = a*bYou get a single value or a 1X1 matrix S. Note that there is only one data struture in MATLAB, that is, matrix. To calculate the determinant of A, enter
det(A)Now enter
I = [1,0,0; 0,1,0; 0,0,1]to get a 3X3 identity matrix (or you can get it by simply type
I = eye(3), use ``help eye'' to find why). Now enter
B = A + Iand find the determinant of B with
det(B)Is B invertible? Enter
x = inv(B) * bto solve the linear system of equations
B x = bfor vector x Try also
x = B\ bWhat is the solution of
Bx = b? Does the notation
B*amake sense? Why? Enter
B*aand see what happens.
a = [1,2,3]; b = [1;2;3]; A = b*a; I = eye(3); B = I + A; x = B\bSave the file. In the MATLAB window, type
testwithout the .m. It returns x. Note the inclusion of semicolons at the end of statement suppresses the printing of the results. One can also write several statements separated by semicolons on one line.
help formatEnter
format shortEnter
97.6Enter
format longEnter
97.6Did you see any difference between short and long format? Can you explain? You can add your explanation in the file ``hw1.txt'' after this MATLAB session is over. Find one real number which has the similar phenomena and another one which does not.
for i = 1 : 100 % start a "for" loop. x = 2^(-i); if (1+x) == 1 % logical "if" statement y = 2^(-i+1); disp( sprintf('EPSILON = %22.18f', y) ); % display "EPSILON with 18 digits after % the decimal point. break; % exit the loop once "EPSILON" is found. end % end the "if" statement end % end the "for" loop.The words after ``%'' are comments and ignored by MATLAB. Use MATLAB on-line help to find how to use the commands
disp, sprintf, break, for, if. In fact, this eps is a MATLAB constant. Enter
epsWhat do you get?
for k = 1 : 10 x(k) = 10^(-k); f(k) = sqrt( 1 + x(k)^2 ) - 1; g(k) = ( x(k)^2 ) / ( sqrt( 1 + x(k)^2 ) + 1 ); end loglog( x, f, 'o', x, g, '+' ) print figure.ps % produce a PostScript file of the figureHere the sqrt is the square root function. Why f(x_k) not equal to g(x_k) epecially for larger k? Why do we choose to use loglog instead of plot to draw the figure? Try to use plot to draw f(x_k), g(x_k) against x_k and see why.
x=0:0.1:2*pi; y=cos(1.7*x)}; plot(x,y)The first command creates a vector, x , which includes the values 0, 0.1, 0.2, ... up to 2 pi. The second command creates a vector y of the same length, for which y(i)=cos(1.7x(i)). The last command creates a smooth graph which passes through all the points with coordinates (x(i),y(i)). Hence, this graph only approximates the real graph. The finer spacing we use (say, 0.01 instead of 0.1), the better approximate graph we get. Create a hard copy of your output. To find out how, enter {\bf help printing} at the Matlab prompt.
Sum=0; for i=1:100, Sum=Sum+i; end SumCreate an m-file to do the job (why did we use a capitalized Sum? try help sum ). Then generate a vector
j=0:1:100;then enter
sum(j)
load xy.dat;Type xy. What do you get? Type
size(xy)to find out how big the array is. Now, you can assign the first column of the array xy to xp by typing
xp=xy(:,1);Similarly, the second column can be assigned to yp by typing
yp=xy(:,2);Type yp. What is the result? Plot the array xy and put a grid on the figure. Save the plot as a postscript file.
diary offto end saving the session to file ``hw5.txt''. Then enter quit to quit MATLAB.