epostma

1579 Reputation

19 Badges

17 years, 49 days
Maplesoft

Social Networks and Content at Maplesoft.com

Maple Application Center
I am the manager of the Mathematical Software Group, working mostly on the Maple library. I have been working at Maplesoft since 2007, mostly on the Statistics and Units packages and on contract work with industry users. My background is in abstract algebra, in which I completed a PhD at Eindhoven University of Technology. During my studies I was always searching for interesting questions at the crossroads of math and computer science. When I came to Canada in 2007, I had nothing but a work permit, some contacts at Maplesoft (whom I had met at a computer algebra conference a year earlier), and a plan to travel around beautiful Canada for a few months. Since Maplesoft is a company that solves more interesting math and computer science related questions than just about anywhere else, I was quite eager to join them, and after about three months, I could start.

MaplePrimes Activity


These are answers submitted by epostma

Hi Vitro,

Thank you for reporting this. As a first point, let me say that I'm afraid you've run into an inaccuracy in the documentation -- or at least a spot where it's rather unclear. The issue is with the frequencyscale option that you already discussed. The help page for Statistics[Histogram] states:

frequencyscale=relative or absolute
This option controls whether the absolute or relative data frequencies should be plotted. If frequencyscale is set to relative then the histogram will be rescaled so that the total area covered by the bars is equal to 1.

This seems to imply that the default is absolute. That's not true; the default is relative. We will try to make this clearer in the next version of Maple.

 

Now for the actual issue you were discussing. I tried running the commands you posted:

  • If I add the option frequencyscale = absolute, I get bars running up to integer values; that seems to be correct.
  • If I don't, as you say, the first bar is about 0.8 units high. According to the help page, the total area of the bars added together should be one; we could verify that this is true. First, we need to assign the plot data structure to a variable.

    dt := [2.5, 3.9, 2.9, 2.4, 2.9, .8, 9.1, .8, .7, .6, 7.9, 1.5, 1.8, 1.9, .8, 6.5, 1.6, 5.8, 1.3, 1.2, 2.7];
    histogram := Statistics[Histogram](dt);


    Now we need to get at the list of coordinates of the bars.

    bars := select(type, convert(op(histogram), list), listlist);


    We now compute the area of these bars.

    barArea := proc(polygon :: listlist, $) :: nonnegative;
    local xCoordinates, yCoordinates;
        xCoordinates := map(point -> point[1], polygon);
        yCoordinates := map(point -> point[2], polygon);
        return (max(xCoordinates) - min(xCoordinates)) * (max(yCoordinates) - min(yCoordinates));
    end proc;

    add(barArea(bar), bar in bars);

    This returns 1 for me (with a small rounding error); I tried it in Maple 12. So this seems to also conform to the documentation.

A separate issue is whether any of the two options does what you are trying to achieve. Maybe you would like the heights, instead of the areas, of the bars to add up to one? I don't think that Statistics[Histogram] can do that directly, but we can use plottools[transform] to do it for us: we use the histogram with absolute values, and divide the height of the bars by the number of points. First let's get the histogram.

dt := [2.5, 3.9, 2.9, 2.4, 2.9, .8, 9.1, .8, .7, .6, 7.9, 1.5, 1.8, 1.9, .8, 6.5, 1.6, 5.8, 1.3, 1.2, 2.7];
histogram := Statistics[Histogram](dt, frequencyscale = absolute);


Now we use the transformation.

transformation := plottools[transform]( (x, y) -> [x, y / nops(dt)] ):
plots[display](transformation(histogram));


This gives a plot where the leftmost bar is approximately 0.24 units high.

I hope that this answers your question; if not, feel free to ask for more detail.

 

Erik Postma.

Hi Sebgo,

What you'll want to do is first make sure you get dsolve to find a numerical solution for you. In order to achieve that,  you'll need to pass it the differential equations and initial conditions. Note also that there was a small mistake in the initial conditions that you posted: you had

ics := {Seq(y(j)[0] = exp(-(j-mu)^2/(2*sigma^2))/(sigma*sqrt(2*Pi)), j = 1 .. imax)}

where you probably want

ics := {seq(y[j](0) = exp(-(j-mu)^2/(2*sigma^2))/(sigma*sqrt(2*Pi)), j = 1 .. imax)}:

(note the seq being lowercase and the brackets for y[j](0)). If you have des defined as in your original posting and ics as in the line above this one, and furthermore, beta[i,j] has a numerical value for all sensible i and j, then you can call

dsnproc := dsolve([des[], ics[]], numeric):
odeplots := seq(plots:-odeplot(dsnproc, [t, y[i](t)], color='COLOR'('HUE', i * (1/2 + 1/2*sqrt(5)))), i=1..10):
plots:-display(odeplots);

Hope this helps,

Erik.

This idea has actually come up before, and we're looking into it.

Thanks for the suggestion!

On the other hand, if you need to do this programmatically, you could do it like this:

b := numer(a)/combine(frontend(expand, [denom(a)]), exp);

Note that I apply these constructs specifically to the denominator; if you need it to apply to the numerator as well, you can adjust it accordingly.

Hi Thomas,

I'm not sure which version of Maple you are using, but Maple 12 has a very powerful new system replacing stop_cond, called events. It allows for expressing the type of thing that you are interested in very naturally:

dsn := dsolve([D(D(x))(t)=-x(t), x(0)=0, D(x)(0)=1], numeric, events=[[[x(t), t > 4], halt]]):
plots:-odeplot(dsn, numpoints=2000);

This will nicely plot sin(t) till 2 Pi and give you the following warning:

Warning, cannot evaluate the solution further right of 6.2831854, event #1 triggered a halt

Note that dsolve did a pretty good job of finding the value of 2 Pi, which is 6.283185308 according to evalf with the standard setting of Digits=10.

The events system also allows for much more complicated events: you can set the values of the functions you're integrating, set discrete variables used in other events et cetera. I'd recommend a look at ?dsolve,events if you have Maple 12.

This reply is probably obsolete for now for Wolfie7873, the original author of the post, who has already indicated that goto is good enough for now, but I thought I'd just add this for future reference.

If you would be looking to replicate the switch/case statement found in other languages as closely as possible, so you have some literal constants a1, a2, a3 that you are comparing to, and you would want to use overload, then the most useful class of types would be the "identical" types - see type,identical. An example mirroring the initial construct in the post:

overload([
  proc(x :: identical(a1))
  option overload;
    b1;
  end,
  proc(x :: identical(a2))
  option overload;
    b2;
  end,
  proc(x :: identical(a3))
  option overload;
    b3;
  end])(a);

Now if you have really many cases, and care about efficiency, there is usually some structure to the set of possible values for a: at least an ordering or so. Then this is certainly a bad way to do it: it would simply try whether a is equal to a1 first, then a2, et cetera. What you'd want to do is use the structure; in this case, use binary search for the value. That would require writing some custom code.

First 9 10 11 Page 11 of 11