Applications, Examples and Libraries

Share your work here

Here is a short wrapper which automates repeated calls to the DirectSearch 2 curve-fitting routine. It offers both time and repetition (solver restart) limits.

The global optimization package DirectSearch 2 (see Application Center link, and here) has some very nice features. One aspect which I really like is that it can do curve-fitting: to fit an expression using tabular data. By this, I mean that it can find optimal values of parameters present in an expression (formula) such that the residual error between that formula and the tabular data is minimized.

Maple itself has commands from the CurveFitting and Statistics packages for data regression, such as NonlinearFit, etc. But those use local optimization solvers, and quite often for the nonlinear case one may need a global optimizer in order to produce a good fit. The nonlinear problem may have local extrema which are not even close to being globally optimal or provide a close fit.

Maplesoft offers the (commercially available) GlobalOptimization package as an add-on to Maple, but its solvers are not hooked into those mentioned curve-fitting commands. One has to set up the proper residual-based objective function onself in order to use this for curve-fitting, and some of the bells and whistles may be harder to do.

So this is why I really like the fact that the DirectSearch 2 package has its own exported commands to do curve-fitting, integrated with its global solvers.

But as the DirectSearch package's author mentions, the fitting routine may sometimes exit too early. Repeat starts of the solver, for the very same parameter ranges, can produce varying results due to randomization steps performed by the solver. This post is branched off from another thread which involved such a problematic example.

Global optimization is often a dark art. Sometimes one may wish to simply have the engine work for 24 hours, and produce whatever best result it can. That's the basic enhancement this wrapper offers.

Here is the wrapper, and a few illustrative calls to it on the mentioned curve-fitting example that show informative  progress status messages, etc. I've tried to make the wrapper pretty generic. It could be reused for other similar purposes.

Other improvements are possible, but might make it less generic. A target option is possible, where attainment of the target would cause an immediate stop. The wrapper could be made into an appliable module, and the running best result could be stored in a module local so that any error (and ensuing halt) would not wipe out the best result from potentially hours and hours worth of conputation.

restart:
randomize():

repeater:=proc(  funccall::uneval
               , {maxtime::numeric:=60}
               , {maxiter::posint:=10}
               , {access::appliable:=proc(a) SFloat(a[1]); end proc}
               , {initial::anything:=[infinity]}
              )
          local best, current, elapsed, i, starttime;
            starttime:=time[real]();
            elapsed:=time[real]()-starttime;
            i:=1; best:=[infinity];
            while elapsed<maxtime and i<=maxiter do
              userinfo(2,repeater,`iteration `,i);
              try
                timelimit(maxtime-elapsed,assign('current',eval(funccall)));
              catch "time expired":
              end try;
              if is(access(current)<access(best)) then
                best:=current;
                userinfo(1,repeater,`new best `,access(best));
              end if;
              i:=i+1;
              elapsed:=time[real]()-starttime;
              userinfo(2,repeater,`elapsed time `,elapsed);
            end do;
            if best<>initial then
              return best;
            else
              error "time limit exceeded during first attempt";
            end if;
          end proc:


X := Vector([seq(.1*j, j = 0 .. 16), 1.65], datatype = float): 

Y := Vector([2.61, 2.62, 2.62, 2.62, 2.63, 2.63, 2.74, 2.98, 3.66,
             5.04, 7.52, 10.74, 12.62, 10.17, 5, 2.64, 11.5, 35.4],
            datatype = float):

F := a*cosh(b*x^c*sin(d*x^e));

                                    /   c    /   e\\
                         F := a cosh\b x  sin\d x //

infolevel[repeater]:=2: # or 1, or not at all (ie. 0)
interface(warnlevel=0): # disabling warnings. disable if you want.

repeater(DirectSearch:-DataFit(F
                      , [a=0..10, b=-10..10, c=0..100, d=0..7, e=0..4]
                      , X, Y, x
                      , strategy=globalsearch
                      , evaluationlimit=30000
                              ));
repeater: iteration  1
repeater: new best  9.81701944539358706
repeater: elapsed time  15.884
repeater: iteration  2
repeater: new best  2.30718902535293857
repeater: elapsed time  22.354
repeater: iteration  3
repeater: new best  0.627585701120743822e-4
repeater: elapsed time  30.777
repeater: iteration  4
repeater: elapsed time  47.959
repeater: iteration  5
repeater: new best  0.627585700905294148e-4
repeater: elapsed time  55.221
repeater: iteration  6
repeater: elapsed time  60.009
 [0.0000627585700905294, [a = 2.61748237902808, b = 1.71949329097179, 

   c = 2.30924401405164, d = 1.50333106110324, e = 1.84597267458055], 4333]


# without userinfo messages printed
infolevel[repeater]:=0:
repeater(DirectSearch:-DataFit(F
                      , [a=0..10, b=-10..10, c=0..100, d=0..7, e=0..4]
                      , X, Y, x
                      , strategy=globalsearch
                      , evaluationlimit=30000
                              ));

 [0.0000627585701341043, [a = 2.61748226209478, b = 1.71949332125427, 

   c = 2.30924369227236, d = 1.50333090706676, e = 1.84597294290477], 6050]


# illustrating early timeout
infolevel[repeater]:=2:
repeater(DirectSearch:-DataFit(F
                      , [a=0..10, b=-10..10, c=0..100, d=0..7, e=0..4]
                      , X, Y, x
                      , strategy=globalsearch
                      , evaluationlimit=30000
                              ),
         maxtime=2);

repeater: iteration  1
repeater: elapsed time  2.002
Error, (in repeater) time limit exceeded during first attempt

# illustrating iteration limit cutoff
infolevel[repeater]:=2:
repeater(DirectSearch:-DataFit(F
                      , [a=0..10, b=-10..10, c=0..100, d=0..7, e=0..4]
                      , X, Y, x
                      , strategy=globalsearch
                      , evaluationlimit=30000
                              ),
         maxiter=1);

repeater: iteration  1
repeater: new best  5.68594272127419575
repeater: elapsed time  7.084
 [5.68594272127420, [a = 3.51723075672918, b = -1.48456068506828, 

   c = 1.60544055207338, d = 6.99999999983179, e = 3.72070034285212], 2793]


# giving it a large total time limit, with reduced userinfo messages
infolevel[repeater]:=1:
Digits:=15:
repeater(DirectSearch:-DataFit(F
                      , [a=0..10, b=-10..10, c=0..100, d=0..7, e=0..4]
                      , X, Y, x
                      , strategy=globalsearch
                      , evaluationlimit=30000
                              ),
         maxtime=2000, maxiter=1000);

repeater: new best  3.10971990123465947
repeater: new best  0.627585701270853103e-4
repeater: new best  0.627585700896181428e-4
repeater: new best  0.627585700896051324e-4
repeater: new best  0.627585700895833535e-4
repeater: new best  0.627585700895607885e-4
 [0.0000627585700895608, [a = 2.61748239185387, b = -1.71949328487160, 

   c = 2.30924398692221, d = 1.50333104262348, e = 1.84597270535142], 6502]

I did not come across with a sorting algorithm animation that allows me to enter my own data, so I decided to write one in Maple.

In this worksheet, you can create an animation on sorting the integers that you have entered. If you let the worksheet to generate the data for you, you can specify the sortedness of the data. This feature allows you to visualize how some algorithms perform better or worse on data of a certain characteristic: The time complexity may not be...

NewtonBlackArea.mw

I have been working with Newton-Raphson fractals for some time.  Like others it was necessary to deal with the "black areas" many times, so I performed some additional analysis and present some of these results here.  This will allow others to stop coloring these areas black and allow visualization of the structure inside these areas.  It will also help demonstrate...

2012.zip

Ukraine. External independent evaluation (ZNO) in 2012. Trial in Maple 16

html 3-interactive in Ukrainian: zno.zip

One of my coworkers brought in G.L. Legendre's book "Pasta By Design" (amazon.ca/dp/0500515808).  It is full of photographs and parametric equations for 92 shapes of pasta.  Of course, we had to set about plotting his equations in Maple.  Orginally I was going to post about this before Maple 16 came out, but I was struck with how much better plots looked in the Maple 16 pre-release and so I decided to wait.   As one example, here are the parametric equations for Giglio Ondulato noodles plotted using the default 3D plot settings in Maple 16 and Maple 15.

 

I was introduced to the geometric interpretation of correlation and linear regression recently.


Orignially due to the famous statistician R.A.Fisher, the idea is that the correlation between 
two variables is the cosine of the angle between the 2 vectors in n-dimensional space.
This can be demonstrated in Maple as follows:

First, we represent each variable as a vector and transform it so that it is centred at its
mean and has a length equal...

Mechanics of Materials Toolbox Screencasts:

http://youtu.be/czz_uw0918E

1dim_roots.mw

All critics and new ideas would be appreciated.

N.B. The MaplePrimes site elided everything past the bullet list, which included more description and links for obtaining the package.  It appears as though bullet lists break MaplePrimes, so this is going to be a bit out-of-order.

This new debugger uses a client/server architecture.  Communication with Maple is via TCP. This permits remote debugging as well as concurrent debugging of multiple Maple processes.  That is useful for comparing different...

This post is continuing the theme of "animate implicitplot (a heart shape)". I tried to send a reply within this theme, but for some reason, my message is not loaded

prototypes.zip

HTML-Navigator of the tasks' prototypes  of the unified state examination in Russia 2012.
Supplied with the links to the Maple-solutions of 2011.

Online: http://webmath.exponenta.ru/beg/index.html

This is one of rank tests.
Non-parametric methods are widely used for studying populations that take on a ranked order (such as movie reviews receiving one to four stars).
The use of non-parametric methods may be necessary when data have a ranking but no clear numerical interpretation, such as when assessing preferences.
In terms of levels of measurement, non-parametric methods result in "ordinal" data.
After the introduction to the topic let's turn to an example.

I created a procedure over a year ago to collect earthquake data from the usgs and save it into a file. The procedure pulls data off the internet and saves it in a text file with a date stamp (minus the year - because I haven't been bothered to change it ) in the folder location f:/7 day earthquakes.  Feel free to modify it as you wish.  Here it is, pretty much still in it's original format when I created it.

earthquakedatasave := proc ()
  local a, b, c, d, e, i:

Dr. Gilbert Lai is a mentor for the FIRST Robotics team SWAT 771. He is helping an all girls team from grades 7-12 design a basketball-shooting robot for this year’s annual FIRST Robotics Competition. Dr. Lai is using MapleSim and Maple to help the team understand the principles involved and design their robot. This blog post is part of a series that chronicles the progress of the team.  Posts in the series include:

  • Part 1 - 

Ukaine-2012. External independent evaluation. A trial version in Maple, by Maple.
HTML, Java-Interactive:
http://webmath.exponenta.ru/zno_11/ranok/z.html
Maple:2012_ranok_ru_bez.mw

First 53 54 55 56 57 58 59 Last Page 55 of 71