Joe Riel

9660 Reputation

23 Badges

20 years, 4 days

MaplePrimes Activity


These are answers submitted by Joe Riel

It took a little doing to reproduce that error.  To do so, I had to use the (Maple 15) GUI, enter the statement (f(eta) := ...) in a 2D input, and select the "remember table assignment" option when the 2D parser opened a dialog. You probably don't want to do that; either make f a procedure

f := eta -> ...

or chose a name for the lhs of the assignment. Also, rather than sum, you should be using ?add.

Use ?dsolve,numeric. Check the output option. Here is a simple example

deq := diff(x(t),t) + x(t) = sin(t):
dsol := dsolve({deq,x(0)=0}, numeric, 'output=listprocedure'):
xt := eval(x(t), dsol):
plot(xt, 0..1);

Try ?fprintf, with the %q format if you want to include the commas

for j to 5 do
   file := sprintf("P%d.dat", j);
   fprintf(file, "%q\n", op(P[j]));
   close(file);
end do:

One way to do this is to generate the partitions in reverse-lexicographic order.  For example, the partitions of 10 are then

7,3
7,1,1,1
5,5
5,3,1,1
5,1,1,1,1,1
3,3,3,1
3,3,1,1,1,1
3,1,1,1,1,1,1,1

The following does that, but the code is rather ugly and specific to this particular problem. I threw this together without a lot of thought (it shows).
A better approach is to create an iterator that uses an Array as the data structure and each call updates it with the next partition.

Partition := proc( n :: posint)
local A,P,mx,p,psum;
    P := [Part(n,7)];
    for i do
        if member(1,P,'p') then
            p := p-1;
        else
            p := numelems(P);
        end if;
        mx := P[p]-2;
        P := (op(1..p-1,P),mx);
        psum := `+`(P);
        P := [P, Part(n-psum, mx)];
        A[i] := P;
        if P :: list(1) then break; end if;
    end do;
    return [seq(A[i],i=1..i)];
end proc:


Part := proc(n :: posint, mx :: posint)
local psum,i,A,val;
    psum := n;
    for i while psum > 0 do
        for val from mx to 1 by -2 while val > psum do end do;
        psum := psum - val;
        A[i] := val;
    end do;
    return seq(A[i], i=1..i-1);
end proc:
Partition(12);
[[7, 3, 1, 1], [7, 1, 1, 1, 1, 1], [5, 5, 1, 1], [5, 3, 3, 1],
    [5, 3, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1], [3, 3, 3, 3],
    [3, 3, 3, 1, 1, 1], [3, 3, 1, 1, 1, 1, 1, 1],
    [3, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]


CodeTools:-Usage(Partition(100)):
memory used=15.23MiB, alloc change=0 bytes, cpu time=120.00ms, real time=119.00ms
CodeTools:-Usage(Partition(200)):
memory used=178.39MiB, alloc change=41.49MiB, cpu time=2.70s, real time=2.71s

While not efficient in that it first generates all partitions, you could do

select(type, combinat:-partition(n), list({1,3,5,7}));

To workaround a deficiency in plots:-dualaxisplot, for which I'll submit an SCR, call Plot:-DualAxisPlot instead. For example,

plotsetup('ps'):
Plot:-DualAxisPlot(sin(x),cos(x),x=0..2*Pi);

will generate a postscript file with a dual axis plot.

 

Here is the fixed worksheet, Projektet-fixed.mw.  A few months ago I posted a Maple procedure to remove bad characters from a worksheet. Here it is again, with a small correction (added call to ?fclose to close the file after writing). It was used to fix the worksheet.

DeleteBadCharacters := proc(file :: string)
local base, badchar, char, cnt, msg, outfile, str, unicode;
    str := FileTools:-Text:-ReadFile(file);
    for cnt from 0 do
        try
            XMLTools:-ParseString(str);
            break;
        catch "An invalid XML character":
            msg := lastexception[2];
            if not StringTools:-RegMatch("Unicode: 0x([^)]+)", msg, 'all', 'unicode') then
                error;
            end if;
            unicode := sscanf(unicode,"%x");
            char := convert(unicode,'bytes');
            badchar[cnt+1] := char;
            str := StringTools:-SubstituteAll(str, char, "");
        end try;
    end do;

    if cnt=0 then
        printf("no errors in file\n");
    else
        if not StringTools:-RegMatch("^(.*)\\.mw$", file, 'all', 'base') then
            error "problem extracting basename";
        end if;
        printf("deleted bad characters: %A\n", {seq(badchar[cnt],cnt=1..cnt)});
        outfile := sprintf("%s-fixed.mw", base);
        FileTools:-Text:-WriteString(outfile, str);
        fclose(outfile);
        printf("wrote updated file to %s\n", outfile);
    end if;
    return NULL;
end proc:

Update

Modified routine to print the unique bad characters deleted (rather than just the number).  With the given worksheet, the bad characters were ^U and ^V.

Consider

piecewise(l<1, c(0), c(0)+sum(c(j)^2,j=1..l-1));

Are you entering text in a math region?  Look at the context bar, the "Text" field should be selected.  If "Math" is selected then you are entering text in a math region.

To match a parenthesis, you need to escape it by preceding it with a backslash, otherwise the parenthesis starts a group.  Doubling the parentheses, as you did, just creates two groups, both of which match exactly the same text. In fact, you actually need to precede the parenthesis with two backslashes because Maple backslashes must be doubled (escaped) to be entered into strings.

The way to handle this substitution is to use \# fields in the replacement string.  A \# string is a backslash (doubled) followed by an integer; the field is replaced with the matching group.  See ?RegSubs for details. I would create a regular expression with two groups, the first matches upto the first insertion point, the second matches to the end.  In the replacement text, insert these two groups, using \\1 and \\2, interspersed with the added text.

To match the text surrounded by parentheses I'd use the trick of matching the opening parenthesis, followed by anything that is not a closing parenthesis, followed by the closing parenthesis. The partial regular expression for that is "\\([^)]+\\)", where [^)] matches any character that is not a closing parenthese.  You might want to use * rather than + to permit matching no characters, but that is a detail. 

Thus,

str := ( "(4) dup 0 stringbbox 452 exch sub exch pop 2582 exch m show\n"
         "(x) dup 0 stringbbox 908 exch sub exch pop 3800 exch m show\n"
       ):

StringTools:-RegSubs("(exch m show\n\\([^)]+\\) )(dup 0 stringbbox [0-9]+ exch sub exch pop [0-9]+ exch m show\n)"
                     = "\\1TickLabelFont\\2AxisLabelFont"
                     , str);

To handle the ORIENTATION, do

str := "ORIENTATION(45.,45.,45.)":
StringTools:-RegSubs("(ORIENTATION\\([0-9.+-]+,[0-9.+-]+),[0-9.+-]+\\)"="\\1)",str);
                                                        "ORIENTATION(45.,45.)"

To match square-brackets, and other special symbols, escape them with a backslash, doubled as before. See ?regular_expressions for details. Or just ask here.


u := a+b*x:
eqs := { eval(u,x=h) = 4, eval(diff(u,x),x=6)=4 };
                          eqs := {b = 4, a + b h = 4}

solve(eqs, {a,b});
                             {a = -4 h + 4, b = 4}

Frequently it is possible to use ?frontend with ?diff to compute the derivative you want. For example, here is a simple procedure to compute the Euler-Lagrange equations from the Lagrangian:

T := 1/2*m*diff(x(t),t)^2:
V := m*g*x(t):
L := T - V;
                                  /d      \2
                       L := 1/2 m |-- x(t)|  - m g x(t)
                                  \dt     /


EulerLagrange := proc(L,X::set(name),t)
local x;
    { seq(diff(frontend(diff, [L, diff(x(t),t)]),t)
          - frontend(diff, [L,x(t)]) = 0
          , x = X) };
end proc:

deqs := EulerLagrange( L, {x}, t);
                                  / 2      \
                                  |d       |
                       deqs := {m |--- x(t)| + m g = 0}
                                  |  2     |
                                  \dt      /

dsolve(deqs);
                                      2
                                   g t
                         {x(t) = - ---- + _C1 t + _C2}
                                    2

 

 

The differential equations are badly formatted.  For example, unless K and y1 are assigned something else, diff(K,y1) evaluates to 0.  Also, it is quite unlikely that both diff(Y1(t),t) and diff(K,Y1) should both appear in the set, as they do.

You can do that with the ?VectorCalculus package. However, the expression given is for a 2D curve. The binormal and torsion apply to 3D curves. Maybe the z-component is t?

Eliminating the expression may be difficult, but converting the equations to the form

diff(u(x),x) = F1( u(x), delta(x) );
diff(delta(x),x) = F2( u(x), delta(x) );

is easy enough.  Just do

convert(solve(convert({eq1,eq2},D), D({u,delta})(x)), diff);
First 45 46 47 48 49 50 51 Last Page 47 of 114