MATH
21 B
Mathematics Math21b Spring 2018
Linear Algebra and Differential Equations
CAS
Course Head: Oliver Knill
Office: SciCtr 432



  • The Mathematica project will be due on Friday night, 4/27. Here is the Canon composed at the workshop:
    f={1,1,3,3,5,5,1,1,1,1,3,3,5,5,1,1,5,5,6,6,8,8,8,8,5,5,6,6,
    8,8,8,8,8,10,8,6,5,5,1,1,8,10,8,6,5,5,1,1,3,3,-4,-4,1,1,1,1};
    A=Map[SoundNote,f];
    a1=Sound[{"Piano",A},{0,24}];
    a2=Sound[{"Flute", RotateRight[A, 8]},{0,24}];
    a3=Sound[{"Violin",RotateRight[A,16]},{0,24}];
    Sound[{a1,a2,a3},{0,15}]
    
    Here is a "wild piano". It plays a "random matrix". Each row is an "accord". There are 30 accords. Each tone is chosen in the range -40,40. We pick a random time (length 1 or 2) to play each accord.
    A = Table[RandomChoice[Range[80] - 40], {30}, {6}];
    f[x_] := SoundNote[x, RandomChoice[{1, 2}]];
    Sound[{"Piano", Map[f, A]}, {0, 6}]
    
    The mathematica lab is available here. It is due on Friday, April 27, 2018. You will submit a notebook with your answers. We have a workshop on Wednesday, April 25, 2018, 5:45 PM - 6:45 PM in ScienceCtr Hall B.
    There are lots of plotting possibilities to plot systems x' = A x. Here is one of the simplest. Just enter the following lines into Mathematica and adjust the matrix.
    A = {{0, -1}, {-0.4, -1}};
    Manipulate[Show[{StreamPlot[A.{x,y}, {x,-5,5},{y,-5,5}],
     ParametricPlot[Evaluate[MatrixExp[A t,#] & /@ pt],{t,0,10},
     PlotRange->{{-5,5},{-5,5}}]}],{{pt,{{2,0},{0,1},{-3,0}}},
     Locator},SaveDefinitions -> True]
    
    It is useful to know what can be done with computer algebra systems "the four M's". Mathematica, Matlab or Maple and Maxima. The following example snippets should become self explanatory during the course.


    Mathematica

    Here are links to get Mathematica Students Faculty/Staff. Make sure to use your Harvard email address when registering. Contact me (knill@math.harvard.edu) if you plan to use Mathematica on a linux system. and request a password,
    A={{1,2,3},{4,5,5},{6,7,8}}
    v={5,-2,3}
    Inverse[A]
    A.v
    A.A.A
    LinearSolve[A,v]
    RowReduce[A]
    QRDecomposition[{{1,0,0},{1,1,0},{1,1,1}}]
    Fit[{{0,0},{0,1},{1,3}},{1,x,x^2},x]
    CharacteristicPolynomial[A,x]
    Tr[A]
    Det[A]
    Eigenvalues[A]
    Eigensystem[A]
    

    Matlab

    Matlab is a CAS which is strong in linear algebra. Matlab is available as a student version. Here are some of the above commands in Matlab.

    A = [1 2 3; 4 5 5; 6 7 8]
    v = [5;-2;3]
    inv(A)
    A*v
    A*A*A
    Av
    rref(A)
    qr(A)
    poly(A)
    det(A)
    trace(A)
    eig(A)
    [v,d]=eig(A)
    

    Maple

    Maple is a CAS comparable with Mathematica or Matlab. Here are the same commands in the Maple dialect.

    with(linalg);
    A:=[[1,2,3],[4,5,5],[6,7,8]];
    v:=[5,-2,3];
    inverse(A);
    multiply(A,v); 
    evalm(A*A*A);
    linsolve(A,v);
    rref(A);
    v1:=[1,0,0]; v2:=[1,1,0]; v3:=[1,1,1];
    GramSchmidt({v1,v2,v3});
    charpoly(A,x);
    trace(A); 
    det(A); 
    eigenvalues(A);
    eigenvectors(A); 
    

    Maxima

    Maxima is an open source CAS originally developed by the DOE. While having less features than the commercial CAS, it is GPL'd and free software: you can see the code.
    (echelon(A) is here an upper triangular matrix);
    A: matrix([1,2,3],[4,5,5],[6,7,8]);
    v: [5,-2,3];
    invert(A);
    A.v;
    A.A.A;
    linsolve([x+z=5,x+5*y=-2,x-z=0],[x,y,z]);
    echelon(A);
    load(eigen); gramschmidt(A); 
    determinant(A); 
    charpoly(A,x);
    eigenvalues(A);
    eigenvectors(A);
    


    To fit data with Mathematica, you can use either the built in routines
           data={{4,5},{2,10},{1,100},{5,3}};functions={1,x,Sin[x]};
           Fit[data,functions,x]
           
    or crank in the linear algebra: a+bx+c sin[x] =y
           A=N[{{1,4,Sin[4]},{1,2,Sin[2]},{1,1,Sin[1]},{1,5,Sin[5]}}]; b={5,10,100,3};
           Inverse[Transpose[A].A].Transpose[A].b
           
    With both approaches you get in this example the function 210.3-60x-77.2 Sin[x] which beset fits the data points (4,5),(2,10),(1,100),(5,3).


  • Please send questions and comments to knill@math.harvard.edu
    Math21b Harvard College Course ID:110989| Oliver Knill | Spring 2018 | Department of Mathematics | Faculty of Art and Sciences | Harvard University, [Canvas, for admin], Twitter