Joe Riel

9660 Reputation

23 Badges

20 years, 11 days

MaplePrimes Activity


These are answers submitted by Joe Riel

If all you are interested in is the assignments, then it should be clear

(1) 2
(2) n+1 [i is assigned n time] 
(3) n [once each iteration, so n total]
(4) n [once each iteration, so n total]
(5) 0
(6) 0
So the total is 2*n+3.  The reason that (2) is n+1 and not n is that on the last iteration i is assigned to n+1, which terminates the loop.

The complexity of this algorithm, however, is not necessarily O(n), at least not in Maple.  It depends on what y is assigned.  Assume it is a not assigned.  To see what happens do


printlevel := 20:
n := 3:
a:=1: b:=1:
for i to n do
    a := (a*y)/i;
    b :=b+a;
end do;
                                    a := y

                                  b := 1 + y

                                          2
                                         y
                                   a := ----
                                         2

                                                2
                              b := 1 + y + 1/2 y

                                          3
                                         y
                                   a := ----
                                         6

                                           2        3
                         b := 1 + y + 1/2 y  + 1/6 y


Note that b increase in size with each loop.   To better see this you can use the length function as a crude measure of the complexity of intermediate expressions:

printlevel := 1:
data := table():
n := 1000:
a:=1: b:=1:
for i to n do
    a := (a*y)/i;
    b :=b+a;
    data[i] := [length(a), length(b)];
end do:

with(plots):
display(loglogplot([seq([i,data[i][1]], i=1..n)]),
        loglogplot([seq([i,data[i][2]], i=1..n)]));

If you plot this you will see that the length of a increases linearly with n, while the length of b increases as the square of n.  Because all intermediate values are saved in the Maple simplification table, the complexity of the entire algorithm could be O(n^3).  Now rerun this test, but assign y a numeric value, say 1.  You will then see that both a and b increase linearly.  Finally, rerun once more, but with y assigned a float, say 1.0.  Now the complexity (as measured by length) of a and b does not increase with n.


 

Robert's use of simplifying with a  side relation, see ?simplify/siderels, is elegant and practical.  If you didn't know about that, you could, at least for these simple problems, solve for x, pick one solution, and then substitute into the result:

sol := solve((x+1)^2 = a, {x}):
normal(subs(sol[1], (x^2+2*x+1)^3+2)); 
                                         3
                                    2 + a

Edited

That same method, but using expand, will work for your followup example (exp is a function in Maple, not a symbol):

y := exp(3*x+3)+2; 
sol := solve(exp(x+1)=a,{x});
expand(subs(sol,y));      
                                         3
                                    2 + a

The command display is part of the plots package.  Access it either with the long name, plots:-display, or execute with(plots) before calling display.

The assignment

y1 := x -> y1(x);

and the subsequent call to y1 is the culprit.  The easy way to figure this out is to do

debug(PS):
PS();
.... [output omitted]
                               y1 := x -> y1(x)

<-- ERROR in PS (now at top level) = "too many levels of recursion"}
Error, (in y1) too many levels of recursion

What you should do is

y1 := 'y1';

Similarly for y2, later in the procedure. Finally, using with(plots) inside a procedure does not work.  What you want to do is remove that statement and change display to

plots:-display(...);

With those changes I get a plot.

This might get you started,

q := sqrt(2*m*(V-x))/h;
z := ( exp(-I*q*b)*(cosh(q*b)))^2 - I*sinh(q*b) * exp(-4*I*q*b);
simplify(evalc(abs(z)^2)) assuming V < x, m>0, h>0;
u := c*(exp(x-4*t)+exp(4*t-x))^(-2):
eq := diff(u,t) + u*diff(u,x) + diff(u,x$3) = 0:
sol := solve(eq,[c]):
simplify(sol);

Use dsolve/numeric with the output=array option, where array is an Array specifying the values of the dependent variable.  See ?dsolve/numeric. For example:

deq := {diff(y(x),x,x)+y(x)=0, y(0)=1, D(y)(0)=1}: 
dsolve(deq,numeric,output=Array([seq(0..1, 0.1)]));  

You've got the right idea, however, there are a few problems.  First, as assigned, d is merely an expression, not a procedure.  I'd write the procedure as

d := m -> numtheory:-sigma(m)-m; 

Next, the assignment to Amicable has a syntax error; the call to print should look like print(m,n);  Also, you don't want to make m a formal parameter.  What you probably should do is let the max number of loops be the formal parameter:

Amicable := proc(nmax)
local m,n;
   for m from 2 to nmax do
       n := d(m);
       if d(n) = m then
           print(m,n);
       end if;
   end do;
end proc:
Amicable(10000);

You might also want to add a test for m <> n, otherwise m=n, which isn't really a pair, but is a perfect number.
 

Note that this procedure merely prints the number.  More useful is to return the pairs in a list.  There are many ways to do that, but the easiest might be

[seq(`if`(d(d(n))=n and d(n)<>n, [n,d(n)], NULL), n = 1..10\000)];

A useful trick is to return the pairs as sets rather than lists, and as one big set (of sets), rather than a list, so that duplicates are automatically eliminated.

The way to do that is to write two procedures that compute a(n) and (b) in terms of a(n-1) and b(n-1), then assign their initial values.  To speed things up the procedures are given the option cache and a call to evalf is used to evaluate the result as a floating number.  Here is an outline,

a := proc(n) option cache; 
     evalf(...a(n-1)...b(n-1)...); # fill in the ... accordingly
end proc:
b := proc(n) option cache;
     evalf(...a(n-1)...b(n-1)...);
end proc:
a(0) := 1:
b(0) := 1/surd(2,3):

You will want to set Digits := 20; (or higher).   Next call these two procedures in a loop as n is increased from 1, printing the results at each step (a call top printf is useful here).

FYI here's a significantly different approach.  It uses the StringTools to do most of the work.

yahtzee := proc()
uses StringTools;
local keep,roll,numdice,pos,len;
    keep := "";
    numdice := 5;
    to 3 while numdice <> 0 do
        roll := Sort(cat(keep,Random(numdice, "123456")));
        (pos,len) := MaximalPalindromicSubstring(roll);
        keep := SubString(roll, pos..pos+len-1);
        numdice := 5 - len;
    end do;
    evalb(numdice=0);
end proc:

StringTools:-Randomize():
# total number of yahtzee's in 1000 trials
Ntrials := 1000:
add(`if`(yahtzee(),1,0), i=1..Ntrials);

I didn't look closely, but those procedures don't appear to simulate Yahtzee.  The way the game is actually played is that a player throws 5 dice, then on subsequent throws rerolls some but not necessarily all the dice.  That way, if you got 4 3's on the first roll you would then reroll the single die that wasn't a 3 [if you are trying to achieve a "yahtzee"].

You can use the RandomTools package to simulate the dice throws:

die := integer(range=1..6):
roll := n -> RandomTools:-Generate(list(die,n)):
roll(5);
               [2, 3, 2, 6, 4]

Is this what you mean?

L1 := [cat(a,1..4)]:
L2 := [cat(b,1..4)]:

inner((L1-L2)$2);
                       2             2             2             2
             (-b1 + a1)  + (-b2 + a2)  + (-b3 + a3)  + (-b4 + a4)

This could also be achieved with

add(x^2, x in L1-L2);

It really depends on what you hope to accomplish.  That is, if the real purpose is to learn Maple programming (or maybe programming in general), then I'd suggest you work out a routine at a fairly low level.   Otherwise I'd probably use acer's suggestion and use the BinaryPlace procedure from the ListTools package.  That is

L := [seq(10*k^3, k=0..9)]:
ListTools:-BinaryPlace(L,5); --> 1
ListTools:-BinaryPlace(L,10); --> 1
ListTools:-BinaryPlace(L,11); --> 2

That's just the idea, it needs some work...

They are the same thing, just presented slightly differently.  That is, in the differential form df=(df/dx)dx + ..., the term dx represents the row-vector [1,0,0], dy the row-vector [0,1,0], etc.

First 87 88 89 90 91 92 93 Last Page 89 of 114