function out=wise(n); % % A sequence of examples, comments written for the 1997 SWE-WISE % MAPLE and MATLAB workshops. Typically download this ascii-file % on the WWW, save it in a subdirectory in the MATLAB-path, and % execute individual paragraphs by calling wise(n), n an integer. % % You may want to haveTWO simulataneous MATLAB session running, one % for going thru thiese commensta nd examples, and one for trying out % your own examples.... % if n==1 disp('1997 SWE-WISE MAPLE-MATLAB workshop, part 1') disp('Getting started. The first problem solved. ') disp(' ') disp('MATLAB is a user-friendly first-class software package') disp('for numerical calculations, with excellent built-in graphix.') disp('Recently, the additional symbolic toolbox also provides ') disp('symbolic capabilities by giving access to MAPLE (many ') disp('(consider the interface clumsy -- why not use MAPLE in the ') disp('first place? -- but there are valid reasons, e.g. only minor') disp('symbolic needs within large numerical calculations).') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('MATLAB essentially knows only one data-structure -- but do not') disp('underestimate what one can do with matrices (with numeric entries!)') disp('The interface is designed for maximum speed - input is very fast! ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('The following demonstrates how to assign values to a (define a) matrix ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('A=[3 .4 -5; 6e-3 ,sqrt(2),8;0 ,pi,Inf]') A=[3 .4 -5; 6e-3 ,sqrt(2),8;0 ,pi,Inf] disp(' ') disp('Note: The entries are enclosed in a pair of square brackets. ') disp(' Rows are separated by semi-colons. ') disp(' Entries in each row are seprareted by commas or spaces.') disp(' The = sign denotes assignments, == dnotes logical equality.') disp(' Individual statements are tertminated with a line-break, but ') disp(' mulit-line statements are possible using the notation ...') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('First example: ') disp('Solve the system of 2 equations') disp(' ') disp('3 u - 4 w = 7 ') disp('4 u - 5 w = 9 ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('First enter the matrix of coefficients, and the rite hand side:') disp(' ') disp('A=[3 -4;4 -5]') A=[3 -4;4 -5] disp('b=[7;9]') b=[7;9] disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('To solve the matrix equation Ax=b opne thinks of dividing both') disp('sides of the equation (on the left) by A. THAT IS THE WAY TO TELL') disp('MATLAB what to do! We check by multiplying back:') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('x=A\b') x=A\b disp('A*x') A*x disp('b') b disp(' ') disp('Strike any key when ready to return to the MATLAB command window. ') disp('To get to the nexct paragraph enter wise(2) at the prompt. ') pause disp(' ') disp(' ') end if n==2 disp('1997 SWE-WISE MAPLE-MATLAB workshop, part 2') disp('Moving around (or not), getting help, print, copy, save etc. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('MATLAB allows you to print out an entire session, or a limited selection') disp('Unless you use m-files, you are essentially limited to sequentially') disp('entering commands at the command line -- using the arrow keys, you may') disp('revisit and reexecute previous commnads, one at a time.') disp('The usual CNTRL-C etc copy, cut and paste commands work, but you ') disp('canNOT paste into previous output or commands.... ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('MATLAB provides on-screen-help. You may either use the pull-down') disp('menu, which gives you access to a word search, and a list of all') disp('available functions, or type "help keyword" at the prompt in the') disp('command window.') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('Exercise:') disp(' ') disp('1. Create your own square matrix, e.g. 3 by 3 matrix. ') disp(' ') disp('2. Use the help pages to find out how to compute eigenvalues and ') disp(' and eigenvectors of a matrix. Hint: MATLAB is rather "minimalist",') disp(' i.e. try the shortest imagineable keyword, or simply stroll thru') disp(' the index until you spot a suitable string.') disp(' ') disp('3. Calculate the eigenvalues and eigenvactors of your matrix. ') disp(' Convince yourself that you indeed have a matrix U of eigenvectors') disp(' By calculating the products A*V and V*D.') disp(' ') disp('4. Bonus: If you have spare time, first help the others in the class. ') disp(' If nobody takes tour help, find out how to calculate the inverse of ') disp(' your matrix, and how to get its transpose.') disp(' ') disp('Use the scroll-bar of the command-winmdow to revisit this exercise! ') disp(' ') end if n==3 disp('1997 SWE-WISE MAPLE-MATLAB workshop, part 3') disp('Some warnings and hints') disp(' ') disp('Practice a lot, and be very cautious when using the extremely slick') disp('syntax of MATLAB. For example if a matrix is not diagonalizable, or') disp('an overdetermined system of linear equations has no solutions, then') disp('MATLAB will NOT crash, but rather provide you with its best guess of ') disp('of what might be the most useful solution for the user!') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('Note that MATLAB is case sensitive, e.g. a and A are different. ') disp('Also there is a big difference between A\B and B/A standing for ') disp('A^(-1)*B and B*A^(-1) in ordinary matrix notation. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('The following example exhibits the least squares solution when ') disp('using the simple command x=A\b ') disp(' ') disp('Example: Fit the data points (-3,10),(-2,8),(-1,5),(0,4),(1,3),(3,1) ') disp(' with a linear function y=b+m*t, usinmg a least squares fit.') disp(' ') disp('MATLAB can do this in one step -- here we do it in several steps for ') disp('illustration purposes only. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('First define the data (using range notation : and transposes '' ') disp(' ') disp('tt:=[-3:1,3]'' ') tt=[-3:1:1,3]' disp('yy=[10,8,5,0,3,5]'' ') yy=[10,8,5,4,3,1]' disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('The unknowns are m and b, and we write x=[b,m]'' for these unknowns.') disp('We obtain an overdetermined system A*x=yy where yy is the column vector') disp('of y values, x are the unknowns, and the coefficient matrix contains ') disp('only ones in the first column, and the t-data-values in the 2nd column. ') disp(' ') disp('Rather than entering all these ones by hand, we utilize some shortcuts:') disp(' ') disp('A=[ones(size(tt)),tt] ') A=[ones(size(tt)),tt] disp(' ') disp('Strike any key when ready. ') pause disp('We now solve the (overdetermined) system of 6 equations in 2 unknowns') disp('using the automatic "least squares" interpretation of the \ command. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('x=A\yy') x=A\yy disp(' ') disp('To get a feeling for the solution, we overlay the data-points with') disp('the calculated least squares solution -- for the details in the') disp('following steps see the nest paragraph: wise(4) ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('clg') clg axis([-4,4,0,12]) disp('plot(tt,yy,''m*'')') plot(tt,yy,'m*') disp('hold on ') hold on disp('ttt=[-3:0.25:3]') ttt=[-3:.5:3]' disp('yyy=[ones(size(ttt)),ttt]*x') yyy=[ones(size(ttt)),ttt]*x disp('plot(ttt,yyy,''yo'') ') plot(ttt,yyy,'yo') disp('plot(ttt,yyy,''c-'') ') plot(ttt,yyy,'c-') disp(' ') end if n==4 disp('1997 SWE-WISE MAPLE-MATLAB workshop, part 4') disp('Array operations and plotting.') disp(' ') disp('The previous paragraph already illustrated an example of how') disp('to plot in MATLAB. The basic way is to generate ALL the points') disp('that are to be connected, i.e. make a table of x-values, and ') disp('calculate the corresponding y-values. ') disp(' ') disp('This is much facilitated by the almost automatic interpretation') disp('of commands as "array-operations". We give a few examples here, ') disp('in particular what to do to avoid interpreattion as an array') disp('operation. As a sample, we revist the previous data for a quadratic ') disp('and a sinusoidal fit. ') disp('We conclude with a brief 3d-plot, actually an animation. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('Multiplying matrices two ways - an example illustrates all:') disp('A=[1 2;3 4]') A=[1 2;3 4] disp('B=[10 0;0 -1]') B=[10 0;0 -1] disp(' ') disp('Strike any key when ready. ') pause disp('A*B') A*B disp('A.*B') A.*B disp('Can you see what is going on? Next divide matrices in two ways:') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('A/B') A/B disp('A./B') A./B disp('There are plenty of such componentwise - array - operations:') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('A^2') A^2 disp('A.^2') A.^2 disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('Example: Fit the data points (-3,10),(-2,8),(-1,5),(0,4),(1,3),(3,1) ') disp(' with a quadratic function y=b+m*t+c*t^2, using a least squares') disp(' fit. Repeat with a sinusoidal fit') disp(' y=a+b*cos(t)+c*sin(t)') disp(' ') disp('MATLAB can do this in one step -- here we do it in several steps for ') disp('illustration purposes only. First, again define the data.') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('tt:=[-3:1,3]'' ') tt=[-3:1:1,3]' disp('yy=[10,8,5,0,3,5]'' ') yy=[10,8,5,4,3,1]' disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('The unknowns are a,b and c, and we write x=[a,b,c]'' for these unknowns.') disp('We obtain an overdetermined system A*x=yy where yy is the column vector') disp('of y values, x are the unknowns, and the coefficient matrix contains ') disp('only ones in the first column, and the t-data-values in the 2nd column. ') disp(' ') disp('Rather than entering all these ones by hand, we utilize some shortcuts:') disp(' ') disp('A2=[ones(size(tt)),tt,tt.*tt] ') A2=[ones(size(tt)),tt,tt.*tt] disp('A3=[ones(size(tt)),cos(tt),sin(tt)] ') A3=[ones(size(tt)),cos(tt),sin(tt)] disp(' ') disp('Strike any key when ready. ') pause disp('We now solve the (overdetermined) systems of 6 equations in 3 unknowns') disp('using the automatic "least squares" interpretation of the \ command. ') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('x2=A2\yy') x2=A2\yy disp('x3=A3\yy') x3=A3\yy disp(' ') disp('To get a feeling for the solution, we overlay the data-points with') disp(' ') disp('Strike any key when ready. ') pause disp(' ') disp('First clear the graphix. Make sure that you can see it on the screen! ') disp('clg') clg axis([-4,4,0,12]) disp('plot(tt,yy,''r*'')') plot(tt,yy,'r*') disp('hold on ') hold on disp('ttt=[-3:0.25:3]') ttt=[-3:.5:3]' disp('yyy2=[ones(size(ttt)),ttt,ttt.*ttt]*x2') yyy2=[ones(size(ttt)),ttt,ttt.*ttt]*x2 disp('plot(ttt,yyy2,''b-'') ') plot(ttt,yyy2,'b-') disp('yyy3=[ones(size(ttt)),ttt,ttt.*ttt]*x3') yyy3=[ones(size(ttt)),cos(ttt),sin(ttt)]*x3 disp('plot(ttt,yyy3,''g-'') ') plot(ttt,yyy3,'g-') disp(' ') disp('As conclusion let us enjoy an animation of random circles w/ random colors') disp('Type wise(5) at the prompt in the command window.') disp(' ') end if n==5 disp('1997 SWE-WISE MAPLE-MATLAB workshop, part 5') disp('An animation for fun.') disp(' ') disp('The following is a silly creation of randomly colored circles') disp('with random radii at random locations. It is very easy to create') disp('animations of any sequnce of plots -- simply check the help for ') disp(' ') disp('help movie') disp(' ') disp('Strike any key when ready. ') pause disp(' ') help movie disp('Strike any key when ready. ') pause disp(' ') help moviein disp(' ') disp('Strike any key when ready. ') pause disp(' tt=pi*[0:.1:2];') disp(' M = moviein(n);') disp(' for j=1:n') disp(' rr=rand(1);') disp(' fill(rand(1)+rr*cos(tt),rand(1)+rr*sin(tt),[rand(21,1)]);'); disp(' M(:,j) = getframe;') disp(' end') disp(' movie(M,-3)') disp('Strike any key when ready. ') pause clg axis([-1,2,-1,2]) axis('off') hold on tt=pi*[0:.1:2]; M = moviein(12); for j=1:12 clf rr=rand(1); fill(rand(1)+rr*cos(tt),rand(1)+rr*sin(tt),[rand(21,1)]); axis([-1,2,-1,2]) M(:,j) = getframe; end movie(M,-3,6) disp(' ') disp(' ') disp('For useful animations relating to eigenvectors etc visit the new') disp('ONE-STOP WWW SERVER http://calculus.la.asu.edu and check out the') disp('m-files eignmovy.m and eignplot.m under MAT342-LinearAlgebra -->') disp('-->calculators/computers -->MATLAB. Download these and save them ') disp('in your MATLAB search path. ') disp(' ') disp('For fun visit the demo package -- simply type demo, followed by ENTER') disp(' ') disp('Strike any key when ready. ') pause demo disp(' ') end