Joe Riel

9530 Reputation

23 Badges

20 years, 28 days

MaplePrimes Activity


These are answers submitted by Joe Riel

To better see what is happening, do

plot([g,x->x], 0..1);

The intersection is a fixed point for g, that is, g(x0) = x0, with x0 = 0.9047882178730188...

You can solve for x0

fsolve(g(x) = x);
                                   0.9047882178730188

The reason that your loop is in a two-cycle is due to numerical approximation in the evaluation of g.  Try changing the setting of Digits and noting how the loop values change.

You also might want to run your corrected procedure (after applying Axel's suggestion) through ?mint:

Procedure gen( x0, a, b, M, n ) on lines 1 to 30
  These variables were used as the same loop variable for nested loops:  i
Global names used in this file:  gen
 

It should be clear what the issue is.  Whether it is a problem depends on what you are intending (I didn't examine the algorithm). To more clearly see this, I indented it for you:

gen:=proc(x0,a,b,M,n)
local i,x,r,d,j,f;
    r:= Vector(n);
    d:= Vector(n);
    f:= Vector(n);
    x:= x0;
    for i to n do
        x:=irem(a+x*b,M);
        r[i]:=2*evalf(x/M);
        f[i]:=evalf(x/M);
        if r[i]>1 then
            d[i]:=sqrt(2*evalf(Pi))*exp(-2*(r[i]-1))*2*exp(-(r[i]-2)^2/2);
        else
            d[i]:=2*sqrt(2/evalf(Pi))*exp(-r[i]^2/2);
        end if;
        for i to n do
            if d[i]<2/3 then
                for j to infinity
                while d[i]<=exp(-f[j] ^2/2) do
                    d[i]:=f[j]
                end do;
            else for j to infinity
                 while d[i]<=exp(-(exp(-2*(f[j]-1))*2-2)^2/2) do
                     d[i]:=f[j]
                 end do;
            end if;
        end do;
        return (d);
    end do;
end proc:

Before considering the recursive method, let's consider a weakness in the current procedure.  Try it with

(**) bisection(cos, 0, 3, 0.001);
Error, (in bisection) unable to evaluate sign

To find the cause of that error you'll need to do some debugging. For that, let's invoke Maple's debugger.  Because I'm using the command-line interface, the interactions will be slightly different than what you will see using the debugger interface in the Standard worksheet, however, you should be able to resolve that.

A convenient way to invoke the debugger is to use the ?stoperror function. 

(**) stoperror(""):           
(**) bisection(cos, 0, 3, 0.001);
Error, unable to evaluate sign
bisection:
   6     if sign(f(c)) = sign(f(a)) then
           ...
         else
           ...
         end if

The error message that the debugger is displaying is the error that will occur if the next statement is executed. At this point we can see what is being passed to the sign functions:

DBG> f(c)
cos(3/2)

The problem is that Maple is being asked to compute sign(cos(3/2)).  The sign function will not numerically evaluate the cosine, so the result is unknown.  To avoid that, you can call the ?evalf procedure to evaluate the cos to a floating point. However, rather than inserting evalf functions here, you could immediately convert the input arguments to floats, then the call to the cosine will be automatically evaluated as a float.  Here is the updated procedure

bisection2 := proc(f, A::realcons, B::realcons, delta::positive)
local a,b,c;
    a := evalf(A); # convert A and B to floats
    b := evalf(B);
    while abs(b-a)>delta do
        c := (a+b)/2:
        if sign(f(c))=sign(f(a)) then
            a := c;
        else
            b := c;
        end if;
    end do;
    (a+b)/2;  # return the value, don't just print it.
end proc:

Alas, what you ask for is currently not available in MapleSim.  You could approximate a time-delay element with one of the transmission lines in the Electrical menu.

Are these variables that you use at top-level in a worksheet, or are they local to procedures or modules?  The first doesn't make much sense because there is no method for declaring top-level variables. I rarely use document mode to write procedures, preferring the comfort of an external editor. There I may include an inline comment, say

someproc := proc()
local L  # list of random values
    , k  # counter
    ;
  ...
end proc:

I don't know of any tools for tracking these when written in the usual Maple format, however, I've occasionally used noweb, a literate-programming tool that will produce an index of procedures and variables (depending on what you mark up).  For a Maple example, see solving_a_sudoku_puzzle_with_maple.

An interesting possibility for top-level variables might be to use ?setattribute to tag them with documentation (that is, essentially, what Maple uses for the option string of procedures).

Your original premise is flawed.  That is, the additional assumption that cos(x*y) <> 0 implies sin(x*y) <> 1.  So the only solution to the equation is x=0.

Take a look at ?StringTools[FormatTime] and ?StringTools[ParseTime].

While not as efficient, here is a variation of Roman's suggestion (I doubt ?charfcn gets used much):

L := [1,2,3,2,4,6,5,0,9,0,3,4,4]:
f := charfcn[2..3];
                                       f := charfcn[2 .. 3]
add(f(x), x in L);
                                                 4


with(group):
mulperms([[1,2]],[[2,3]]);
                                            [[1, 3, 2]]
convert(%,permlist,3);
                                             [3, 1, 2]

Probably (i.e. you are missing something).  More exactly, we are missing something, the procedure.  From the snippet you gave, I'd code it as

f := proc(a::nonnegint,b::nonnegint)
option cache;
    if   a=0 or b=0 then 1;
    else
        f(a-1,b) + f(a,b-1)
    end if;
end proc:

You need to convert the voltages to signals.  Use either the voltage or potential sensor, in the Electrical/Analog/Sensors menu.

It depends on the system. For a continuous-time transfer function system it uses ?inttrans[invlaplace] to compute the inverse laplace and then plots that.

It would work if you also passed the argument to the procedure:

add((i->if i < 2 then 1 else 0 end if)(i), i in list);

A shorter and more efficient method is to use Maple's ?if operator:

add(`if`(i<2, 1, 0), i in list);


Correction: ?sort does work on Array.  I hadn't realized that. 

Note that sort doesn't work with an Array.  Here is a modification to your routine

genRequestDist := proc (N, t)
local requests, i, r, pts, d; 
    requests := Array(1 .. N
                      , sort(RandomTools:-Generate(list(integer(range=0..t),N)))
                     );
    pts := Array(0 .. t, 1 .. 2);
    for i from 0 to t do
        pts[i, 1] := i;
    end do;
    for i to N-1 do
        d := abs(requests[i+1]-requests[i]);
        pts[d, 1] := d;
        pts[d, 2] := pts[d, 2]+1
    end do;
    pts;
end proc:

Most of the values in the second column are 0.  You could eliminate those with

N := 10^5:
A := genRequestDist(N, 36*10^5);
for i from N to 1 by -1 while A[i,2]=0 do end do:
plots:-pointplot(A[1..i,..]);


This particular problem can be readily solved by calling solve:

eqs := {NULL
        , a+b+c=1
        , a+d+e=2
        , b+d+f=2
        , c+e+f=1
       }:
solve(eqs);
                 {a = f, b = e, c = 1 - e - f, d = 2 - f - e, e = e, f = f}

and then noting that there are only two possibilties, {e=0,f=1} and {e=1,f=0} that meet the requirements.

Here is a procedure that implements that technique.  It works on similar simple problems

SolveRestricted := proc(eqs :: set(equation)
                        , given :: set(name=set)
                       )
    
local sols, free, depends, freevars
    , freechoices, dependchoices, T
    , Sols, choice, sol;
    
    sols := solve(eqs);
    (free, depends) := selectremove(evalb, sols);
    
    freevars := map(lhs,free);
    
    (freechoices, dependchoices) := selectremove(has, given, freevars);
    freechoices := convert(freechoices,list);
    freevars := map(lhs, freechoices);
    
    dependchoices := map(eq -> lhs(eq) in rhs(eq), dependchoices);
    
    T := combinat:-cartprod(map(rhs, freechoices));
    
    Sols := table();
    
    while not T['finished'] do
        choice := zip(`=`,freevars,T['nextvalue']());
        try
            sol := eval(depends, choice);
            if andmap(evalb, eval(dependchoices, sol)) then
                Sols[sol union convert(choice,set)] := NULL;
            end if;
        catch:
        end try;
    end do;
    
    return {indices(Sols, 'nolist')};
end proc:


SolveRestricted(eqs, map(`=`,indets(eqs),{0,1}));
  {{a = 0, b = 1, c = 0, d = 1, e = 1, f = 0},
   {a = 1, b = 0, c = 0, d = 1, e = 0, f = 1}}
SolveRestricted(eqs, map(`=`,indets(eqs),{0,1,2}));
  {{a = 0, b = 0, c = 1, d = 2, e = 0, f = 0},
   {a = 0, b = 1, c = 0, d = 1, e = 1, f = 0},
   {a = 1, b = 0, c = 0, d = 1, e = 0, f = 1}}
 

 

First 75 76 77 78 79 80 81 Last Page 77 of 114