acer

32485 Reputation

29 Badges

20 years, 7 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

> expr := (1+1/(n+1))^(n+1)-(1+1/n)^n:

> fsol:=fsolve(abs(expr)=10^(-5),n=0..1000);
                              fsol := 367.2490976
 
> isol:=trunc(fsol);
                                  isol := 367
 
> is( eval(abs(expr)<10^(-5),n=isol) );
                                     false
 
> isol:=trunc(fsol)+1;
                                  isol := 368
 
> is( eval(abs(expr)<10^(-5),n=isol) );
                                     true
 
> fsol:=fsolve(abs(expr)=10^(-5),n=-1000..0);
                             fsol := -370.0824305
 
> isol:=trunc(fsol);
                                 isol := -370
 
> is( eval(abs(expr)<10^(-5),n=isol) );
                                     false
 
> isol:=trunc(fsol)-1;
                                 isol := -371
 
> is( eval(abs(expr)<10^(-5),n=isol) );
                                     true

acer

intVec := [1,2,3,4,5]:

myGraphs := proc(intvec, fname)
  local myplot;
  myplot[1] := Statistics:-PieChart(intvec,'color'='red'..'yellow');
  myplot[2] := plots:-textplot([1,2,"some stuff"]);
  plotsetup('postscript','plotoutput'= cat(fname,".ps"),
            'plotoptions'="colour=cmyk,noborder");
  print(plots:-display(myplot[1],myplot[2]));
  plotsetup('default');
  return NULL;
end proc:

myGraphs(intVec, foo);

plot(sin(x));

acer

The concept "package" may mean different things to different people. But for now I will suppose that you mean that you have a procedure or several procedures, possibly with or without accompanying help-pages and example worksheets, which you wish to save and re-use. By saving them, you hope to either re-use them yourself in other Maple sessions, or to allow colleagues to use them in Maple sessions on their own machines.

Maple reads procedures from archives with the .mla filename suffix. Maple searches for .mla archives by looking in directories (OS folders) in the order that they appear in the Maple libname variable's value. See the ?libname help-page for more.

You can create such archives, and save to them, using the routines in the LibraryTools package. See the ?LibraryTools help-page for details.

One you have saved a procedure inside a .mla archive, then in any new Maple session you can access (use) that procedure as long as the .mla is in a directory or folder in libname. But in any Maple session, you can append to libname. So, rather than having to place the .mla in a certain specified location, you could also simply change your libname to include whatever location the .mla file has.

Here is a very simple example, which creates foo.mla in whatever folder is returned in Maple by command kernelopts(homedir). You don't have to use that folder, of course.

> restart:

> libname := libname,kernelopts(homedir):

> LibraryTools:-Create("foo.mla");

> p := proc(x) sin(x) end proc:

> LibraryTools:-Save(p,foo);

> restart: # a new session!

> p(17); # p is not found in default libname path
                                     p(17)
 
> libname := libname,kernelopts(homedir):

> p(17);
                                    sin(17)

You can either save all your procedures individually to the same .mla archive, or bundle them up first as a Maple module (with option package). See the ?module help-page. This is not a necessary aspect of redistributing or re-using your materials, but it can make things look more organized if you intend to redistrubute your work. There are some other advantages to modules, but those are more advanced.

There is a similar set up for customized help-files. You can write a .mw worksheet which shows how to use a procedure. You can then save that to a .hdb help database archive using the INTERFACE_HELP command. See the ?INTERFACE_HELP help-page for more details. Maple finds and searches .hdb archives in the same way as .mla archives, using the directories in libname. So you can simply place any .hdb file alongside your .mla file so that they get found and used together.

There is also a way to pack up .mla, .hdb, example worksheets, code source text files, and any other file, into an automatically self-unpacking object which Maple can open. There are also some special locations which Maple will search (under your own home directory) even without libname having to be set. I should probably wait until I can write that all out clearly.

It would be better if I rewrote all these note and placed them in the mapleprimes books section, as I've been meaning to do for some time now.

acer

Frequency can mean cycles/unit-of-time, or revolutions/unit-of-time, or 1/unit-of-tme. The second is a common unit of time.

So, using 2*Pi radians as one cycle or revolution

f1,f2 := 1, 1/3: # frequencies, as cycles/second

plot([sin(f1*t*2*Pi),sin(f2*t*2*Pi)],t=0..3);

The above shows two sin plots. One does 1 cycle (2*Pi radians) for every whole unit of t, and the other does 1/3 cycles. You can interpret a unit of t as being a second, for example. The plots are thus done over 3 seconds. One curve does three full cycles and the other shows just one cycle.

You can use the legend or labels options of plot to illustrate it. For example,

f1,f2 := 1, 1/3: # frequencies, as cycles/second

plot([sin(f1*t*2*Pi),sin(f2*t*2*Pi)],t=0..3,
     labels=[typeset(Unit(s)),cycle],
     legend=[typeset(f1*Unit(Hz)),typeset(f2*Unit(Hz))]);

acer

Remain calm. Please put your seat-back into its upright position. Please extinguish all cigarettes.

Seriously, we may be able to give some help. But you are right, posting your code would help. You can use the green up-arrow in the toolbar at the top of this forum's editor (to upload files and worksheets). Or you can put the code inlined right into a post/response here (toggle to Source, and use the <pre> and </pre> tags say).

For the most part in straight Matlab computations will be done in hardware double- precision. There are ways to get around that in (most of) Maple, but it is not the default. There are also ways to use even higher precision in Maple (by setting its Digits environment variable for example), which is generally not possible in Matlab (without having to recourse to its symbolic toolbox).

Let us know what commands you are using, or post or upload the code itself, and it may be possible to offer more precise suggestions.

You may wish to read the ?Digits, ?evalhf, and ?Matrix help-pages.

acer

I can't tell exactly what you want.

> eqn := a*b*c = sqrt(a+2)*d*e:

> sol := isolate(eqn,a);
                                      2  2      2  2 1/2
                             (d e + (d  e  + 8 b  c )   ) d e
                  sol := a = --------------------------------
                                            2  2
                                         2 b  c

> printf("%a == %a\n",lhs(sol),rhs(sol));
a == 1/2/b^2/c^2*(d*e+(d^2*e^2+8*b^2*c^2)^(1/2))*d*e

> solve(eqn,{a});
              2  2      2  2 1/2
     (d e + (d  e  + 8 b  c )   ) d e
{a = --------------------------------},
                    2  2
                 2 b  c

                     2  2      2  2 1/2
           (-d e + (d  e  + 8 b  c )   ) d e
    {a = - ---------------------------------}
                           2  2
                        2 b  c

acer

In Maple, the quotation mark " is used to delimit strings. The single-left-quote is used to delimit names. (One can convert from one to the other, as shown below.) It's not entirely clear to me which you want.

> N := `i love going to the movies`;
                        N := i love going to the movies

> S1 := convert(N,string);
                      S1 := "i love going to the movies"

> S2 := "i love going to the movies"; # same as S1
                      S2 := "i love going to the movies"

> StringTools:-LengthSplit(S2,2);
 "i ", "lo", "ve", " g", "oi", "ng", " t", "o ", "th", "e ", "mo", "vi", "es"

> map(`[]`@parse,[%]);
   [[i], [lo], [ve], [g], [oi], [ng], [t], [o], [th], [e], [mo], [vi], [es]]

note. LengthSplit also works directly on the name N,

> StringTools:-LengthSplit(N,2);
 "i ", "lo", "ve", " g", "oi", "ng", " t", "o ", "th", "e ", "mo", "vi", "es"

map(`[]`,[StringTools:-LengthSplit(N, 2)]);
[["i "], ["lo"], ["ve"], [" g"], ["oi"], ["ng"], [" t"], ["o "], ["th"],

    ["e "], ["mo"], ["vi"], ["es"]]

Notice this difference in converting from strings to names.

> parse(" g");
                                       g

> lprint(%);
g
> convert(" g",name);
                                       g

> lprint(%);
` g`

> map(t->[convert(t,name)],[StringTools:-LengthSplit(N, 2)]);
[[i ], [lo], [ve], [ g], [oi], [ng], [ t], [o ], [th], [e ], [mo], [vi], [es]]

> lprint(%[4]);
[` g`]

acer

The Optimization routines do floating-point calculation, ie. they are not exact. So the issue is whether the result is "close enough" to zero at the current working precision (or with the tolerance used by the routine in question).

The assume=nonnegative option is a convenient way to supply additional constraints on all the variables. It's equivalent to passing such additional constraints as x[1]>=0, x[2]>=0, etc. So, what you are seeing is a constraint violation.

By default,  Optimization routines work at floating-point double precision. Notice that value for y[2] is close to zero (effectively zero) in the double-precision sense.

> evalhf(DBL_EPSILON);
                                                 -15
                          0.222044604925031308 10

Here are two ways to deal with this issue. The first is to apply `fnormal` to the result. See the ?fnormal help-page for an explanation.

> sol := Optimization[NLPSolve](F1, {constraint1, constraint2 },
>   x[2]=x[1]+0.0001..xmax, y[2]=ymin..y[1]-0.0001,
>   assume=nonnegative, maximize);
sol := [0.938888888888889328,
                                                                -15
    [x[2] = 6.33333333333333393, y[2] = -0.165123990869542325 10   ]]

> fnormal(sol);
               [0.9388888889, [x[2] = 6.333333333, y[2] = -0.]]

> fnormal(sol,14);
           [0.93888888888889, [x[2] = 6.3333333333333, y[2] = -0.]]

> fnormal(sol,15,1e-15);
          [0.938888888888889, [x[2] = 6.33333333333333, y[2] = -0.]]

Alternatively, you can obtain results with an even tighter bound on any violation of the constraints.

> Digits:=17:

> sol := Optimization[NLPSolve](F1, {constraint1, constraint2 },
>   x[2]=x[1]+0.0001..xmax, y[2]=ymin..y[1]-0.0001,
>   assume=nonnegative, maximize, feasibilitytolerance=1e-16);
     sol := [0.93888888888888898, [x[2] = 6.3333333333333334, y[2] = 0.]]

See the ?Optimization,options help-page for more explanation of the feasibilitytolerance option.

The waters are a little muddy here, because the Optimization routines respect both the Digits and UseHardwareFloats environment flags. But hopefully the above is enough.

acer

I'm not sure that I understand what you mean by simultaneously, here.

Can you phrase the multi-objective optimization goal in more explicit terms? Is it the case that you have no satisfactory (single) objective function that unifies both U and u (with  some penalty function applied to each)?

For example, can you state the goal in terms of Pareto optimality? If not, then how should the u and U be compared? (Ie. how does a penalty for one relate to a penatly to the other, quantitatively?)

acer

What's the objection to using piecewise?

> f := proc(x) piecewise(x>=0 and x<1,1,0); end proc:
> int(f(x),x=0..2);
                                       1
 
> int(f,0..2);
                                       1
 
> f := piecewise(x>=0 and x<1,1,0):
> int(f,x=0..2);
                                       1

Consider your original,

> f:=proc(x) if x>=0 and x<1 then 1 else 0 fi; end proc:
> f(x);
Error, (in f) cannot determine if this expression is true or false: 0 <= x and
x < 1

Note that int(f(x),...) or plot(f(x),...) will try to evaluate f(x) right away. That's why you got an error. Because at that moment, x is not assigned and so the comparison produces that error. You could also use the operator form of calling int (or plot), even with your original definition of f.

> int(f,0..2);
                                       1

acer

Actually, you set up an equation Determinant(PL) = 0, and started working with that. So, either deal with lhs() of that equation, or don't set it up as an equation.

EqPl:=simplify(Determinant(PL)=0):
coeffs(lhs(EqPl), [x, y, z]);

altern := simplify(Determinant(PL)):
coeffs(altern, [x, y, z]);

BTW, that subs() call didn't assign to anything. Did you intend it to do so?

acer

The sort of membership that you are considering involves checking whether your candidate Matrix is identical to any member of the set A.

But Matrices are mutable data structures (their entries can change). Two mutable structures are not considered identical just because they happen to have the same entries at one given moment. That's because changing the entry of one of them wouldn't affect the other.

The same happens for direct comparison of Matrices. Consider,

> evalb( Matrix(2,2,[[0,0],[0,0]]) = Matrix(2,2,[[0,0],[0,0]]) );
                                     false
 
> X := Matrix(2,2,[[0,0],[0,0]]):
> Y := Matrix(2,2,[[0,0],[0,0]]): # not same object as X
> evalb( X = Y );
                                     false
 
> X[1,1]:=17:
> Y[1,1];
                                       0
 
> Y := X: # same object as X
> evalb( X = Y );
                                      true

> X[1,1]:=13:
> Y[1,1];
                                       13

For your set A, you could instead compare entries something like this,

> ormap(t->EqualEntries(t,Matrix(2,2,[[0,0],[0,0]])),A);
                                      true
 
> ormap(t->EqualEntries(t,Matrix(2,2,[[1,1],[1,1]])),A);
                                      true

You can also use more sophisticated comparisons. Eg, here is a floating point comparison (allowing for  only 1 ulp difference  when compared using 3 digits)

> ormap(t->verify(t,Matrix(2,2,[[1,1],[1,0.999]]),
>                 'Matrix'('float'(1,digits=3))),A);
                                      true
 
> ormap(t->verify(t,Matrix(2,2,[[1,1],[1,0.998]]),
>                 'Matrix'('float'(1,digits=3))),A);
                                     false

Having introduced verify, one could do your example like this,

> verify(Matrix([[1,1],[1,1]]),A,'member'('Matrix'));
                                     true
 
> verify(Matrix([[1,1],[2,2]]),A,'member'('Matrix'));
                                     false

acer

> L := 0.5*m*diff(x(t),t)^2 - m*g*x(t):

> diff(frontend(diff,[L,diff(x(t),t)]),t) = frontend(diff,[L,x(t)]);
                                  / 2      \
                                  |d       |
                            1.0 m |--- x(t)| = -m g
                                  |  2     |
                                  \dt      /

acer

> T:=convert(x^n*exp(x),FormalPowerSeries,x);
                                 infinity
                                  -----    (k + n)
                                   \      x
                            T :=    )     --------
                                   /         k!
                                  -----
                                  k = 0

 > Z:=value(subs(infinity=5,T));
                              (2 + n)    (3 + n)    (4 + n)    (5 + n)
              n    (1 + n)   x          x          x          x
        Z := x  + x        + -------- + -------- + -------- + --------
                                2          6          24        120
 
> S:=series(eval(Z,n=3),x,8);
                     3    4        5        6         7      8
               S := x  + x  + 1/2 x  + 1/6 x  + 1/24 x  + O(x )
 
> value(subs(n=3,T));
                                    3
                                   x  exp(x)
> series(%,x,8);
                   3    4        5        6         7      8
                  x  + x  + 1/2 x  + 1/6 x  + 1/24 x  + O(x )

> Z:=value(subs(infinity=20,T)):
> MultiSeries:-series(Z,x,5);
                          (2 + n)    (3 + n)    (4 + n)
          n    (1 + n)   x          x          x             (5 + n)
         x  + x        + -------- + -------- + -------- + O(x       )
                            2          6          24

acer

I suspect that this may explain how djc obtained this form,

   evalm((`&*`(`&*`(A, B), 2))*B-`&*`(B, Id));

If the ?evalm help-page, being viewed, has its Examples toggled to 2D Math (using the small button symbol at right-end of iconic menubar), then pasting the relevant line to a Worksheet as 1D Maple input produces the following,

   evalm((`&*`(`&*`(A, B), 2))*B-`&*`(B, Id))

But it the Examples are toggled to 1D Maple input, then it gets copied and pasted into a Worksheet as the following 1D Maple input,

   evalm((A &* B) &* (2*B)-B &* Id);

I also tried pasting as 2D Math, with both levels of Typesetting (Extended and Standard). But who can tell, what is really lying "underneath" displayed 2D Math? I don't like the idea that 2D Math might be WYSINWYH (what you see is not what you have).

acer

First 301 302 303 304 305 306 307 Last Page 303 of 337