Running Matlab's Genmex from Maple

Excercise work for Mat-1.192 Numeric and Symbolic Computing

Here is a transcript of a Maple session that demonstrates the use of the Maple program c05.mpl.

The program is run in cypress.csc.fi. It generates files for use with Matlab's Genmex extension, transfers them to dopey.hut.fi, where Matlab and Genmex are run to calculate the zeroes of nonlinear function using NAG routines. The results are written to a file which is transferred back to cypress and read into Maple.

First we define a few functions, plot their functions in the same graph using polimpplot, and solve them with Maple's solve function:


> helppof1:=x1*x2-x2^3-1; 3 helppof1 := x1 x2 - x2 - 1 > helppof2:=x1^2*x2+x2-5; 2 helppof2 := x1 x2 + x2 - 5 > read `polimpplot.mpl`; Warning, `tempdir` is implicitly declared local > plot1:=polimpplot(helppof1, x1=-3..3, x2=-3..3); > plot2:=polimpplot(helppof2, x1=-3..3, x2=-3..3); > with(plots): > display({plot1,plot2}); > evalf(solve({helppof1=0, helppof2=0}, {x1,x2})); {x2 = 1., x1 = 2.}, {x1 = .420809921 + 2.062359942 I, x2 = - 1.232862814 - .6956229500 I}

Then we set up our program.
> read `c05setup.mpl`;
> c05setup();

   C05 setup files copied to dopey.hut.fi into directory /usr/tmp/kenny/c05/.

               Matlab setup script c05setup.m run in dopey.hut.fi.


Now we read the program into Maple and use it to solve the functions.
> read `c05.mpl`;
>
> c05([helppof1, helppof2], [x1,x2], [1,1]);

         ASP for c05 (function and jacobian) generated in file c05fj.m.

             ASP for c05 (function only) generated in file c05fun.m.

            Files c05fj.m,c05fun.m and c05.m copied to dopey.hut.fi.

                    Matlab calculations done in dopey.hut.fi.

                     Output files copied from dopey.hut.fi.

                                        -11                        -10
     [[[2., 1.]], [[.5377476200000000*10   ], [.1330047200000000*10   ]],

                                           -11                        -10
         [[2., 1.]], [[.5377920300000000*10   ], [.1330224800000000*10   ]]]

The output is an array consisting of:
  1. the roots found using C05NBF, a NAG routine that uses the function's value only
  2. the value of the function at that point
  3. the roots found using C05PBF, a NAG routine that uses the values of both the function and its jacobian
  4. the value of the function at that point

Then we do the the same with another set of functions:


> toinenesimf:=[z^2-x^2-y^2+2*a*x+2*a*z-a^2, y*z-a*y-a*x+a^2, -2*a+x+y];

  toinenesimf :=

        2    2    2                    2                     2
      [z  - x  - y  + 2 a x + 2 a z - a , y z - a y - a x + a , - 2 a + x + y]

> a:=1:
> toinenesimf;

            2    2    2
          [z  - x  - y  + 2 x + 2 z - 1, y z - y - x + 1, - 2 + x + y]

> evalf(solve(convert(toinenesimf,set)));

            {z =  - 1. I, y = 1. I, x = 2. - 1. I},

            {z = -2.732050808, y = -.366025404, x = 2.366025404}


The approximate real solution (x,y,z) = (2.3660, -3.660, -2.7321) was found with Maple's solve function. Will our approach find it, too?
> c05(toinenesimf, [x, y, z], [-2.7,-.4,2.4]);

         ASP for c05 (function and jacobian) generated in file c05fj.m.

             ASP for c05 (function only) generated in file c05fun.m.

            Files c05fj.m,c05fun.m and c05.m copied to dopey.hut.fi.

                    Matlab calculations done in dopey.hut.fi.

                     Output files copied from dopey.hut.fi.

[[[2.3660254000000, -.36602540000000, -2.7320508000000]],

                   -11                       -12                      -15
[[.56621374000000*10   ], [-.39634962000000*10   ], [.11102230000000*10   ]],

[[2.3660254000000, -.36602540000000, -2.7320508000000]],

                    -11                       -12                      -16
[[.56648020000000*10   ], [-.39723780000000*10   ], [.55511151000000*10   ]]]


Very well, but in addition, our approach finds another root that Maple's solve did not:
> c05(toinenesimf, [x, y, z], [0,0,0]);

         ASP for c05 (function and jacobian) generated in file c05fj.m.

             ASP for c05 (function only) generated in file c05fun.m.

            Files c05fj.m,c05fun.m and c05.m copied to dopey.hut.fi.

                    Matlab calculations done in dopey.hut.fi.

                     Output files copied from dopey.hut.fi.

[[[.6339746000000001, 1.366025400000000, .7320508100000001]],

                      -12                        -13
[[.5753175700000000*10   ], [.2120526000000000*10   ], [0]],

[[.6339746000000001, 1.366025400000000, .7320508100000001]],

                    -12                      -13                       -15
[[.57553962000000*10   ], [.20983215000000*10   ], [-.22204460000000*10   ]]]


Further Work in Matlab

After the Maple program has been run once, the Matlab files generated are left on both the remote and local hosts. If further computations are wished to be done (e.g., with different initial guesses), this can be done quite easily.

Configuration and Technical Notes

In order to run, three files must be located in the Maple search path:
Also the X-window version of Maple (xmaple) should be run in foreground, because the "system" commands cause an output stream to be opened to the standard output. This is a bug.
Kenrick Bingham 3rd April, 1996