Joe Riel

7545 Reputation

20 Badges

13 years, 178 days

MaplePrimes Activity

These are answers submitted by Joe Riel

I'd generally encode that as follows. Note the use of the remember option; that makes the operation much more efficient as previous results are reused.

v := proc(a)
option remember;
    v(a-1) + 6*v(a-2);
end proc:

# Assign the initial values (this writes to v's remember table)
v(1) := 1:
v(2) := 2:

for k to 20 do
    printf("v(%d) = %d\n", k, v(k) );
end do:

There is no formal parameter for the second positional argument. The dsolve routine does its own parameter processing and uses _rest, etc., to access the undeclared parameters. This might be because dsolve has been around for a long time and existed in some form before a lot of improvements to Maple's parameter processing were implemented. In this case, though, I suspect it has more to do with the second argument being optional.  An alternative would be to declare the second argument and give it a special default value then check for that, however, that isn't any better and the older code did the equivalent in a slightly different manner.


I'd change this to an answer, rather than a reply, if I knew how (thanks whoever did so).  The real confusion you are seeing is that you are using _nparams, rather than nargs or _npassed (a synonym). The _nparams value is based on the number of declared (formal) parameters, not the number of passed arguments.

I don't know what you want to plot; could you explain. That aside, the code can be slightly restructured to improve it.  Also, you might want to make H, etc. Vectors and Matrices rather than implicitly declared tables.

alpha := 0:
beta := 2:
Imax := 11:
Jmax := 11:
L := 10:
`Δζ` := L/(Imax-1):

`Δx` := `Δζ`:
`Δη` := 1/(Jmax-1):

x := Vector(Imax):
H := Vector(Imax):
H1 := Vector(Imax):
H2 := Vector(Imax):
y := Matrix(Imax,Jmax):
Y := Matrix(Imax,Jmax):

printlevel := 2:
for i to Imax do
    x[i] := `Δζ`*(i-1);
    H[i] := 3+2*tanh(x[i]-6);
    H1[i] := 0;
    H2[i] := -H[i];
    for j to Jmax do
        eta[j] := `Δη`*(j-1);
        z := (eta[j]-alpha)*ln((beta+1)/(beta-1))/(1-alpha);
        y[i, j] := -H[i]*(2*beta/(exp(z)+1)-beta-2*alpha)/(2*alpha+1);
        Y[i, j] := (H1[i]+(-H1[i]*x[i]+H2[i])/L)*eta[j]
    end do
end do:

Here's a different approach that doesn't allocate extra memory (doesn't generate new sets).

fnd := false:
for v in L2 do
    for e in L1 do 
        fnd := member(v,e); 
    until fnd; 
until fnd:

On exit, the variable fnd is assigned true if a vertex was found.  If true, e and v will be assigned the first edge and vertex found with v in e, which might be useful.

The problem is that the set S contains y[14], which is a procedure.  Fix the assignment to x[2], the reference to y[14] does not include the parentheses and argument.

The best approach depends on what you want to do with the result. 

Let nx be a piecewise whose condition is some arbitrary relation.

(**) nx := piecewise(x > 0, +1, -1);
(**) y := nx^n:
(**) simplify(y) assuming n :: even;                                                                                             
(**) simplify(y) assuming n :: odd;                                                   
                                  { -1        x <= 0
                                  { 1         0 < x

Another approach is to use a set to represent the possible values.

(**) nx := {+1,-1};                                                                   
                                    nx := {-1, 1}

(**) y := nx^~n;                                                                      
                                   y := {1, (-1) }

(**) simplify(y) assuming n :: even;                                                  

(**) simplify(y) assuming n :: odd;                                                   
                                       {-1, 1}

While I suspect that neither of these is what you're looking for, note that you can do

(**) nx := piecewise(x<0,-1,1):
(**) hx := convert(nx, Heaviside);
                             hx := 2 Heaviside(x) - 1

(**) y := hx^n;
                            y := (2 Heaviside(x) - 1)

(**) simplify(y) assuming n :: even;

(**) simplify(y) assuming n :: odd;
                                2 Heaviside(x) - 1

To generate the probability correctly, you need to generate all permutations of the 9 digits.

F := Permute(1..9):
picks := [seq(add(add(10^(i-1)*f[i+j],i=1..3),j=0..8,3),f = F)]:
rng := (min..max)(picks);
                        rng := 774 .. 2556
# Find unique values
vals := convert(picks,set):
num := numelems(vals);
                           num := 199
# Determine entry that occurs the most
A := Array(rng):
for val in picks do A[val] := A[val]+1; end do:
imax := max[index](A);
                          imax := 1764

Further While it is mostly pointless here, a better technique would be to avoid generating all the picks as a list.

T := table(sparse):
for f in F do
   val := add(add(10^(i-1)*f[i+j],i=1..3),j=0..8,3);
   T[val] := T[val] + 1;
end do:

A downside is that it is a little less convenient to find the max entry and associated value, however, because there are multiple indices with the same entry, we need to do this, regardless.

vmax := max(entries(T,'nolist'));
                                  vmax := 3888
for i in [indices(T,'nolist')] do 
    if T[i] = vmax then print(i) end if 
end do:

Those agree with the values you listed.

The Statistics package could be used here

X := RandomVariable(EmpiricalDistribution(picks)):
S := Sample(X,1000):

To remove identities I usually go with


I don't get that error, but rather 

cannot determine if this expression is true or false: .7444168734e-1*p < -.5300353358*p+1.578268991

That comes from using ai in the condition ai>aineg (and elsewhere), with p unassigned. I suspect that at some point you assigned NULL to p.

Is this what you want to do

local ex, exnum, xnum, ynum; 
uses DocumentTools;
    xnum := parse(GetProperty("TextArea0", "value"));
    ynum := parse(GetProperty("TextArea1", "value"));
    ex := GetProperty("MathContainer0", "expression");
    exnum := eval(ex, ['x'=xnum, 'y'=ynum]);
    SetProperty("TextArea2", "value", exnum);

You can use the second equation to eliminate M__Revolute(t) from the first. Open a Worksheet template and do

A := MapleSim:-LinkModel():
A:-SetSubstitutions([`Main.SpringDamper.c` = c__SpriD, `Main.SpringDamper.d` = d__SpriD, `Main.SpringDamper.phi_rel0` = PhiR0__SpriD, `Main.Revolute.M`(t) = M__Revolute(t), `Main.Revolute.theta`(t) = theta__Revolute(t)], 'nocheck'):
eqs := A:-GetEquations('output' = 'all', 'inputs' = {}, 'filter' = [], 'simplify' = true, 'params' = {`Main.SpringDamper.c`, `Main.SpringDamper.d`}):
dae := eqs[1];
subs(isolate(dae[2],M__Revolute(t)), dae[1]);

UseHardwareFloats is a Maple environment variable.  As such, it gets reset to its previous value when the procedure that assigned it (ModuleLoad) exits.. 

Use the fouriercos and fouriersin procedures in the inttrans package.  Use piecewise to create the expression you want to transform. For example

with(inttrans): # load the inttrans package
y := piecewise(t < 1, exp(t), 1);
fouriercos(y, t, s);

Replace the curly braces with regular parentheses, then use solve.

eq := alpha+(6*RK[1]*alpha+2+(96/5)*R^2*K^2*alpha^2-(1/6)*R*alpha+64*R^3*K^3*alpha^3)*beta+((24/5)*RK+44*R^2*K^2*alpha^3)*beta^2=0:
solve(eq, alpha);
solve(eq, beta);

Using print, as shown by acer, is the recommended way to do this. Here is an alternative.

t := 0:
for i to 11 do
    if 1=1 then
        j := i + i^2;
        k := j + t;
    end if;
    t := j/k;
end do;

That works because Maple uses the value of the global variable printlevel to determine what to print when evaluating substatements in a statement terminated with a semicolon. The default value of printlevel is 1, which prints the result of substatements at level 1 or lower. Note that if true is used rather than 1=1 as the condition, Maple's simplifier will fully evaluate the conditional statement so the nested assignments will appear at level 1 and so be printed.

Further Rather than adding dummy levels, you could modify printlevel directly. For example

t := 0:
for i to 11 do
    printlevel := 0;
    j := i + i^2;
    k := j + t;
    t := j/k;
end do;

Note the use of the assign function, which returns NULL (so prints nothing) when resetting printlevel to 1. If a regular assignment statement was used then that assignment would be printed.

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