"New" Maple lab 5
Lagrange multiplier problems in R4

Please report comments (especially errors and suggestions for improvement) to S. Greenfield (greenfie@math.rutgers.edu). Thank you.


We will create a collection of Lagrange multiplier problems in R4 using Maple. These problems will be checked as they are created using the techniques we have used in our three-dimensional discussion. Raising the dimension may help students to recognize the usefulness of a computational tool like Maple.
in this lab, each student will be given a constraint and an objective function in R4. The constraint is a homeomorphic image of the sphere, so "theory" guarantees that the objective function achieves maximum and minimum values. The students are to assemble the set of five non-linear Lagrange multiplier functions and then have Maple find all solutions. These

A detailed discussion of the design considerations of this problem is available here.

Here is the fifth assignment (so the course number and date can be particularized), which can be a pdf file on a webpage (less paper to struggle with in class):

Fifth Maple lab Plain TeX source

Again, there is a discussion (link below) supporting the lab in a student accessible webpage. This web page too was deliberately designed to help students learn more about Maple, and not just to complete the assignment. Students will need to select the ideas and instructions themselves.

Lab5_Background.html

How to generate the individualized data

The instructions below explain how to generate a webpage listing student data. Here are instructions on distributing the data via email. The manner in which data is distributed is up to you. Using a webpage opens the door for some enterprising student to do the lab for other students using each student's data. Using email, however, can cause many headaches as you need to deal with things like spam filters and full inboxes.
  1. Create a text file called "Lab5Data.txt" (as always, file extensions are optional).
  2. Use this Maple code. Please see comments after this block of code to learn how to use it, and to read a caution.
    
    
    with(LinearAlgebra): with(RealDomain): with(StringTools):
    creatingconstraint := proc()
    local letters, A, B, k;
      letters:=LinearAlgebra:-Transpose(Matrix(1,4,[x,y,z,w]));
      A:=LinearAlgebra:-RandomMatrix(4,density=0);
      while LinearAlgebra:-Determinant(A)=0 do
        A:=0.1*LinearAlgebra:-RandomMatrix(4,density=.3)
      end do;
    B:=evalm(`\&*`(A,letters));
    RealDomain:-expand(sum(RealDomain:-`^`(B[k, 1], 2), k = 1 .. 4))
    end proc;
    nterms:=rand(3..5);coefficients:={-1,1,-2,2,3,-3};ch:=rand(1..6);
    vars:={x,y,z,w};ch2:=rand(1..4);wt:=rand(1..2);
    creatingobjective:=proc()
    local obj;
      obj:=0;
      while obj=0 do obj:=expand(add(coefficients[ch()]*mul(vars[ch2()],j=1..wt()),k=1..nterms()))
    end do
    end proc;
    cv:=v -> StringTools:-RegSubs("0+\\*" = "\*", convert(v, string));
    tcR:=x->type(x,RootOf);
    TCR:=v->seq(seq(tcR(op(2,op(j,op(k,v)))),j=1..nops(op(k,v))),k=1..nops(v));
    check:=sols->(evalb(true in {TCR({sols})}));
    LMproblem:=proc()
    local g, f, eqx, eqy, eqz, eqw, sols, values, v, v1;
      sols := {a = RootOf(RealDomain:-`^`(x, 3) - 1, x)}, {b = 1};
      v := 0;
      g := creatingconstraint();
      while check(sols) do
        f := creatingobjective();
        eqx := diff(f, x) = L*diff(g, x);
        eqy := diff(f, y) = L*diff(g, y);
        eqz := diff(f, z) = L*diff(g, z);
        eqw := diff(f, w) = L*diff(g, w);
        sols := RealDomain:-solve({eqx, eqy, eqz, eqw, g = 1})
      end do;
      values := seq(subs(sols[j], f), j = 1 .. nops({sols}));
      print(convert(cv(g=1),symbol));
      print(convert(cv(f),symbol));
      print(nops({sols}),max(values),min(values))
    end proc;
    interface(quiet=true);
    
    
    The very last line of Maple above (the interface) turns off all the system messages, including the prompt (the > beginning each input line). This can be disconcerting to the unwary user. The program actually uses a great deal of storage space and computation to create the Lagrange multiplier problems, and without the interface(quiet=true); instruction, messages such as memory used=168.0MB, alloc=79.4MB, time=3.21 are produced.

    Here is the result of a sample run, generating 10 Lagrange multiplier problems. (So the instruction seq(LMproblem(),j=1..37); would produce 37 problems.) Note also that some of the lines (and formulas) are very long.

    seq(LMproblem(),j=1..10);
                     54.29*x^2+152.18*y*x+117.13*y^2+5.98*x*z+16.38*y*z+99.7*z^2+26.69*w^2+99.*z*w = 1
    
                                                        -2*y*z-2*x
    
                                               2, 1.116220909, -0.9170145682
    
                         53.89*w^2+82.81*x^2+41.86*y*x+82.73*y^2-45.76*y*z+98.92*z^2+57.6*z*w = 1
    
                                                      6*z-2*z*w+2*y*w
    
                                              2, 0.6950983883, -0.6754678265
    
                               53.*x^2+43.52*y*x+10.24*y^2+37.21*w^2-25.48*x*z+30.77*z^2 = 1
    
                                                       y-x-2*w-3*x^2
    
                                               4, 1.177783332, -4.599832884
    
                             76.52*y^2+64.01*w^2-12.8*y*z+8.*y*w+16.*z^2-20.*z*w+94.09*x^2 = 1
    
                                                     2*y*z+x+x*w-3*x*z
    
                                              2, 0.1292704024, -0.1241373198
    
            41.81*x^2+65.58*y*x+1.64*x*w+97.71*y^2-43.1*y*w+82.86*w^2-171.48*y*z+95.3*z^2-20.02*z*w-97.*x*z = 1
    
                                                      2*y*z-3*x+2*x*w
    
                                               4, 1963.367388, -0.9682079790
    
                                    70.56*w^2+8.41*z^2+72.9*x^2+26.46*y*x+24.01*y^2 = 1
    
                                                  -2*z+2*w+3*z*w-x*z-y*x
    
                                              2, 0.7007868828, -0.7737250976
    
                                    13.69*w^2+74.81*z^2+77.44*x^2-28.16*x*z+49.*y^2 = 1
    
                                                       2*x^2+y*x-z^2
    
                                             8, 0.02878778939, -0.01354921923
    
                      75.69*y^2+146.16*y*z+92.81*z^2+.4000000000e-1*x^2+1.*x*z+30.29*w^2+44.*z*w = 1
    
                                                   -2*y*w+z*w+2*z^2+2*x
    
                                               2, 166.2489301, -178.3977726
    
                         26.28*y^2-103.08*y*z+115.58*z^2+9.72*y*w-17.82*z*w+7.29*w^2+17.64*x^2 = 1
    
                                                       -x*w-5*y+y*w
    
                                               2, 2.771222576, -2.855328867
    
                               52.52*y^2-44.68*y*w+95.42*w^2+1.44*x^2+20.64*x*w+.49*z^2 = 1
    
                                                   -2*y*w+2*y^2+3*z^2-w
    
                                               4, 6.125841229, -0.3302305335
    
    Let's suppose that the output generated above has been put in a file called lmproblems.
  3. Suppose you also have a file called Students.txt with contents as follows:
    Argon,_Fred
    Prime,_Optimus
    Skeletor
    Kahn,_Kublai
    Wyndam-Price,_Wesley
    Taft_II,_William_H
    
    Now we use an awk script to construct a web page for students and a page with answers to help the grader. Please create a file called Lab5_PageMaker.txt with the following as contents:
    
    BEGIN{if (ARGC!=4) exit;
    stud_file=ARGV[2];ARGV[2]="";grade_file=ARGV[3];ARGV[3]="";OFS="";
    command="wc -l " stud_file;
    command | getline temp; split(temp,TEMP); sn=TEMP[1];
    print "<html><head><title>Maple data for lab #4</title></head><body bgcolor=white>";
    print "<center><h3>Maple Data for Lab 5</h3><p>"
    print "<table border cellspacing=1 cellpadding=5>";
    j=0}
    {if (NF==0) next}
    {if (j==0) {A="Constraint: "$1"=1"}}
    {if (j==1) {B="Objective function: "$1}}
    {if (j==2) {C="Number of sols: "$1" Max. value: "$2" Min. value: "$3;
    j=-1;
    getline Q < stud_file; split(Q,name_mail," ");
    print name_mail[1]," "C >> grade_file; 
    print "    "A" "B >> grade_file;
    print "<tr><td align=\"center\">"name_mail[1]"</td><td><pre><font size=+1><br>"A"<p>"B"</font></pre></td></tr>";sn--}}
    {j++}
    {if (sn==0) exit}
    END{if (sn>0) print "The last " sn " students have no data.";
    if (ARGC!=4) print "Number of files incorrect.";
    print "</table></center></body></html>"}
    
    Be sure that you have all the files lmproblems, Students.txt, and Lab5_PageMaker.txt in the same directory. Then execute the following command line:
    awk -f PM.txt lmproblems Students.txt grading.txt > Lab5_DataPage.htm
    The result with the files whose contents were given above will be the creation of two additional files.

    A file similar to grading.txt should be sent or given to the grader. Information about each student's problem is displayed (the constraint, the objective function, the number of Lagrange multiplier solutions, and the max and min values of the objective function on the constraint).

    A file similar to Lab5_DataPage.htm is a web page which can be used to distribute data to students.

  4. When the labs are collected, the person grading the assignment can be sent the grading.txt and some instructions to guide the grading of the first assignment. I might use the following as instructions:
    Grading guide for Maple assignment #5
    Please grade each student's assignment as follows:
    There will be a total of 10 (ten) points:
    
    4 points for including all relevant Maple instructions, especially 
    including the explicit setup of the Lagrange multiplier problem.
    
    4 points for the collection of solutions to the Lagrange multiplier
    problem (this is a collection of 4-tuples).
    
    2 points for max and min values. 
    
    ---------
    
    Please note that different implementations of Maple may give (floating point)
    numerical answers which are not identical to those supplied you have been given.
    For example, the same (complicated!) series of instructions which result in
    461539.8116 in one Maple implementation gives 461539.801 in another Maple
    implementation. Both answers should be graded as correct.
    

Maintained by greenfie@math.rutgers.edu and last modified 3/3/2010.