Carl Love

Carl Love

25324 Reputation

25 Badges

10 years, 240 days
Natick, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity

These are answers submitted by Carl Love

The DirectSearch package, available for free download from the Maple Applications Center, has numerous derivative-free minimizing routines including Brent's. All work in multiple dimensions and with constraints. Input can be algebraic expressions or numeric procedures.

The error message indicates that in one or more of your equations you used uppercase Zeta where you meant lowercase zeta. I can't tell from your screen shot where exactly you made that typo, but if you print out the equations after assigning the parameter values, you should see it.

It's much easier for us if instead of posting screenshots, you upload your worksheet by using the green up arrow on the toolbar of the MaplePrimes editor.

Here is a naive method, akin to just subdividing them by hand. It's likely less efficient than @dharr 's method. But I think it's relatively easy to understand.

SplitCycle:= (C::And(list, Not({0,1,2,3}) &under nops))->
local n:= nops(C), i, j;
    seq(seq([C[i..j], C[[j..-1, 1..i]]], j= i+2..`if`(i=1, n-1, n)), i= 1..n-2) 
Triangulate:= (Gs::set(set(list({integer, symbol, string}))))->
local r:= Gs, pc, pg, cs, g, c;
    while membertype(satisfies(g-> membertype(Not(3) &under nops, g, 'pc')), r, 'pg') do
        g:= r[pg];  c:= g[pc];
        r:= subsop(pg= seq(subsop(pc= cs[], g), cs= SplitCycle(c)), r)
TrianglesToEdges:= (Ts::set(And(list, 3 &under nops)))->
    subsindets(Ts, list, T-> combinat:-choose({T[]}, 2)[])
PlotGs:= (GS::set(set(list)))->
local Gs:= GraphTheory:-Graph~(TrianglesToEdges~(GS));
    GraphTheory:-DrawGraph(Gs, 'width'= isqrt(nops(Gs)))       


Change the solve command in the loop to

(R[i], X[i]):= eval([Rm, Xm], solve({gl1, gl2}, {Rm, Xm}))[];

Then the values that you want will be stored as R[1], ..., R[4] and X[1], ..., X[4].

Let me know if this does what you need.

Here is a detailed example of solving your system for the parameters a1a2, and a3. Since we want to obtain 3 values, we need to provide 3 additional boundary conditions.

eq1:= diff(f(x),x$3) + (cos(alpha)*x + sin(alpha)*f(x))/2*diff(f(x),x$2):
    diff(g(x),x$2) + diff(g(x),x)*(1 + diff(h(x),x) + cos(alpha)*x)
    + sin(alpha)*f(x)*g(x):
eq3:= diff(g(x),x$2) + diff(h(x),x$2) + (cos(alpha)*x+sin(alpha)*f(x))/2
    f(0) = 0, D(f)(0) = 1, (D@@2)(f)(0) = a1,
    g(0) = 1, D(g)(0) = a2,
    h(0) = 1, D(h)(0) = a3
To solve for a1, a2, a3, we pick a value of x>0 (called ub here) and give any 3
algebraic equations involving some of {f, f', f'', g, g', h, h'} evaluated
at x= ub.  These equations cannot use symbolic x, only x=ub. The equations
do not need to use a1, a2, or a3, nor do they need to use all of f, g, and h.

ub:= 1:  bcs:= f(ub) = 1, g(ub) = 1, h(ub) = 4
alpha:= Pi/4
sol:= dsolve({eq||(1..3), ics, bcs}, numeric, range= 0..ub)

# Get the values of a1, a2, a3:
a||(1..3) =~ eval([a||(1..3)], sol(0));

[a1 = HFloat(-3.1095161290988124e-16), a2 = HFloat(0.22323816712844022), a3 = HFloat(2.894612963069318)]

    sol, `[]`~(x, [f,g,h](x)), 0..ub, color=[red, blue, green], legend= [f,g,h]

The current numeric solution functions returned by dsolve are limited to
0 <= x <= ub. However, if you need, dsolve can be used again to obtain new solution
functions that will work outside that range.

Let me know if you need this.




While in Maple, use Export from the File menu to save your document or worksheet as a PDF file. Then access that PDF from your phone. There are many free apps for displaying a PDF on your phone.

Here's a procedure for it. If you give a writable filename as the second argument, then it will write text to that file. Otherwise, it'll display it on your screen.

GraphsToRGraphs:= proc(
    Gs::list(set(And(set(posint), 2 &under nops))), 
    file::{string, symbol}:= 'terminal'
local k, G, E, n, fp, pf:= curry(fprintf, fp);
    try fp:= `if`(file = 'terminal', file, fopen(file, WRITE))
    catch: fp:= 'terminal'
    end try; 
    for k,G in Gs do
        E:= [G[]];
        n:= nops(E)-1;
        if k>1 then pf(",\n\n") fi;
        pf(cat("    from <- c(", "%d,"$n, "%d),\n"), op~(1,E)[]);
        pf(cat("    to <- c(", "%d,"$n, "%d),\n"), op~(2,E)[]);
        pf("    ft <- cbind(from, to),\n");
        pf("    Graph%d <- ftM2graphNEL(ft)", k)
    if file <> 'terminal' then fclose(fp) fi;
end proc
        {{1, 2}, {1, 7}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}}
    from <- c(1,2,3,4),
    to <- c(2,3,4,5),
    ft <- cbind(from, to),
    Graph1 <- ftM2graphNEL(ft),

    from <- c(1,1,2,3,4,5,6),
    to <- c(2,7,3,4,5,6,7),
    ft <- cbind(from, to),
    Graph2 <- ftM2graphNEL(ft)


The keyword static only makes sense when declaring a local or export of an object. The line in question is a reassignment, not a declaration, as we discussed yesterday. I'm amazed that the basic syntax checker (i.e., without using assertlevel= 2) allowed it at all. 

However, Maple syntax does allow you to do a lot of crazy things (which will usually cause you trouble later). For example, you could create a type named static. By no means am I saying that you should do it; just that Maple would allow you to if you were crazy enough to do it.

Although &+- does mean something typographically to Maple, it doesn't know it as a mathematical function. In particular, it doesn't know its derivative. So, it applies the chain rule and expresses the result in terms of that unknown derivative, just like it would do for diff(f(x^2), x). You can easily teach Maple the derivative:

`diff/&+-`:= (y,x)-> diff(y,x);


diff(&+-(x^2), x);

will show what you asked for.

The hot-key for that (at least in Windows) is F3.

Infinite sets (whether discrete or not) can be represented in Maple by using the inert SetOf constructor with a type-based membership criterion. Examples:

  1. Since prime is a predefined type, the set of prime numbers can be represented as SetOf(prime).
  2. There's no limitation to predefined types: odd is also a predefined type, so the set of odd primes can be represented via a constructed type as SetOf(And(odd, prime)).
  3. #2 is mathematically the same as SetOf(odd) intersect SetOf(prime). Since Maple's type system is far more sophisticated than its property system (which SetOf is a part of), I prefer #2 because it puts most of the computation within the type system.

Your example starting sets are (where N is "natural numbers")

  • A: natural numbers at least 20;
  • B: {3*k - 1  |  k in N};
  • C: {2*k + 1  |  k in N}.

These can be defined by (there are many alternative ways to construct the required types; I'm simply showing my favorites):

A:= SetOf(And(posint, Not(integer[1..20])));
B:= SetOf(And(posint, (-1) &under rcurry(mods, 3)));
C:= SetOf(And(odd, Not(1)));

The derived sets that you want are

  • A intersect B intersect C;
  • B minus C;
  • A intersect (B minus C),

which can be constructed via

ABC:= A intersect B intersect C:  #or `intersect`(A, B, C)
`B\\C`:= B minus C:
`A(B\\C)`:= A intersect `B-C`:

They're still infinite sets of course, but they can be used for membership tests via the `in` infix operator, for example,

is(23 in ABC);

is(23 in `B\\C`);

Explicitly listed finite intersections can be made with select. For example the elements of the above 3 sets at most 100 can be listed via

(op@print~@map[3])(select, `in`, {$1..100}, [ABC, `B\\C`, `A(B\\C)`]):
                   {23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83, 89, 95}
             {2, 8, 14, 20, 26, 32, 38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98}
                   {26, 32, 38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98}

A child class may reassign its parent's members, but it can't redeclare them (as localexport, static, etc.).  So, here is that principle applied to your example:


module ode_class()
option object;
local ode::`=`, x::symbol, y::symbol;

    ModuleCopy::static:= proc(_self::ode_class, proto::ode_class, ode, func, $)
        (_self:-ode, _self:-y, _self:-x):=
            if nargs=2 then (proto:-ode, proto:-y, proto:-x)
            else (ode, op(0..1, func))
   end proc,

   get_ode::static:= proc(_self, $) ode end proc,

   get_x::static:= proc(_self, $) x end proc,

   get_y::static:= proc(_self, $) y end proc
end module

module first_order_ode_class()
option object(ode_class);
local is_linear_ode::truefalse;

export is_linear::static:= proc(_self, $) is_linear_ode end proc;

    ModuleCopy:= proc(
        _self::first_order_ode_class, proto::first_order_ode_class, ode, func, $
        (_self:-ode, _self:-y, _self:-x, _self:-is_linear_ode):=
            if nargs=2 then (proto:-ode, proto:-y, proto:-x, proto:-is_linear_ode)
            else (ode, op(0..1, func), false)
   end proc
end module

ode:=Object(first_order_ode_class, diff(y(x),x)=sin(x), y(x));

module first_order_ode_class () local ode::`=`, x::symbol, y::symbol, is_linear_ode::truefalse; option object(ode_class); end module


diff(y(x), x) = sin(x)


type(ode, ode_class);


type(ode, first_order_ode_class);




The differences, which are fairly minor, are described on the help page ?module,named.

You need to declare your coordinate system when creating the vector field. Once that's done, DivergenceCurl, and other differential operators are simple commands (with no options needed):

n:= VectorField(<cos(theta(x,y)), sin(theta(x,y)), 0>, cartesian[x,y,z]);

The coordinate system in this case is cartesian[x,y,z].

You can probably do what you want by making collect's second argument a list of variable names such as [x,y] rather than a single variable such as x. I can't say for sure without seeing your example.

Nonetheless, it's good to learn a few fundamental commands that let you build what you want on the fly, and thus not need to remember all the idiosyncratic options (and their orders) of a vast number of not-so-fundamental commands. Two of those fundamental commands are curry and rcurry. They let you create a new function by pre-passing some (not all) arguments to an existing function (or command or procedure). The only difference between them is that curry pre-passes arguments starting with the 1st position, and rcurry pre-passes them ending with the last position (the r stands for "right"). In your case you could do:

a:= randpoly([x,y,z], dense);  #my example polynomial
collect(a, x, rcurry(collect, y));

You may include a "form" argument with either or both of those, and you may include a "func" argument with the inner collect by making it the last argument of the rcurry.

I use curry and rcurry so much that I have abbreviations for them defined in my initialization file:

`&<`:= curry;  `&>`:= rcurry;

Using those, the command becomes

collect(a, x, collect &> y);

1 2 3 4 5 6 7 Last Page 3 of 369