Maple 2019 Questions and Posts

These are Posts and Questions associated with the product, Maple 2019

Hello everyone

I have the solution of diffusion equation from Help of maple website. I put the code here

*****************************

restart: with(plots):
 

unprotect(D);
 

alias(c[0]=c0, c[1]=c1, c[2]=c2);
PDE:=diff(C(x,t),t)=D*diff(C(x,t),x,x);
IBC:={C(x,0)=cx0, C(0,t)=ct0, D[1](C)(10,t)=0};
ct0:=1;
cx0:=0;
D:=1;
pds:=pdsolve(PDE,IBC,numeric);
L1:=[0.01, 0.1, 1, 5, 10];
L2:=[red, green, yellow, blue, magenta, black];
for i from 1 to 5 do
 pn[i] := pds:-plot(t=L1[i], color=L2[i]):
end do:
display({seq(pn[i], i=1..5)}, title=`Numerical solution at t=0.01, 0.1, 1, 5, 10`);

****************************

 

the code is working perfectly. But, My question is how can I found the diffusion constant (D) if I have the solution ( C(x,t) ).  Probably it should be an algorithm which use least square method to find (D) based on the data C(x,t).

I am looking for a fast and efficient algorithm if there is any.

thank you so much for your kind attentions in advance

Sincerely yours,

Amir

This ODE turns out to be a simple separable ODE. With one solution, if the ODE is rewritten.

But in its original form, Maple dsolve gives 6 complicated looking solutions with complex numbers in some of them. Even though all 6 solutions are valid.

Any one knows why Maple did that and not give the one simple solution instead? 

I used isolate to solve for y' from the original ODE. Verfiied that only one solution exist.  The ODE then became y'(x)= 3*y(x)/(2*x). Which by uniqueness theorem, should have one unique solution in some region in the RHS or in some region in the LHS that does not inculde x=0 ?

Just wondering what is going on, and why Maple did not give same simpler solution, so I can learn something new. That is all.

restart;

Typesetting:-Settings(typesetprime=true):

ode:= 1/2*(2*x^(5/2)-3*y(x)^(5/3))/x^(5/2)/y(x)^(2/3)+1/3*(-2*x^(5/2)+3*y(x)^(5/3))*diff(y(x),x)/x^(3/2)/y(x)^(5/3) = 0;

(1/2)*(2*x^(5/2)-3*y(x)^(5/3))/(x^(5/2)*y(x)^(2/3))+(1/3)*(-2*x^(5/2)+3*y(x)^(5/3))*(diff(y(x), x))/(x^(3/2)*y(x)^(5/3)) = 0

DEtools:-odeadvisor(ode);

[[_1st_order, _with_linear_symmetries], _exact, _rational]

maple_sol:=dsolve(ode);

y(x) = (1/3)*2^(3/5)*3^(2/5)*(x^(5/2))^(3/5), y(x) = (1/3)*(-(1/4)*5^(1/2)-1/4-((1/4)*I)*2^(1/2)*(5-5^(1/2))^(1/2))^3*2^(3/5)*3^(2/5)*(x^(5/2))^(3/5), y(x) = (1/3)*(-(1/4)*5^(1/2)-1/4+((1/4)*I)*2^(1/2)*(5-5^(1/2))^(1/2))^3*2^(3/5)*3^(2/5)*(x^(5/2))^(3/5), y(x) = (1/3)*((1/4)*5^(1/2)-1/4-((1/4)*I)*2^(1/2)*(5+5^(1/2))^(1/2))^3*2^(3/5)*3^(2/5)*(x^(5/2))^(3/5), y(x) = (1/3)*((1/4)*5^(1/2)-1/4+((1/4)*I)*2^(1/2)*(5+5^(1/2))^(1/2))^3*2^(3/5)*3^(2/5)*(x^(5/2))^(3/5), x/y(x)^(2/3)+y(x)/x^(3/2)+_C1 = 0

map(x->odetest(x,ode),[maple_sol])

[0, 0, 0, 0, 0, 0]

solve(ode,diff(y(x),x),AllSolutions)

(3/2)*y(x)/x

ode2:=isolate(ode,diff(y(x),x)); #solve for y' first

diff(y(x), x) = -(3/2)*(2*x^(5/2)-3*y(x)^(5/3))*y(x)/(x*(-2*x^(5/2)+3*y(x)^(5/3)))

ode2:=simplify(ode2)

diff(y(x), x) = (3/2)*y(x)/x

DEtools:-odeadvisor(ode2);

[_separable]

sol:=dsolve(ode2)

y(x) = _C1*x^(3/2)

odetest(sol,ode2)

0

 

Download strange_ode_answer.mw

Maple 2019.1

Physics 395

Hi, I am trying to plot the phase potrait for this as follow:

s0 := 3*10^5;
d := 10^(-3);
delta := 10^4;
b := 5*10^6;
lamda := 4.16;

DEplot([diff(I(t), t) = s0 + I(t)*(-d - delta*Q(t)/(b + Q(t))), diff(Q(t), t) = -lamda*Q(t)], [I(t), Q(t)], t = 0 .. 10, I = 0 .. 100, 0, Q = 0 .. 100, 0, dirfield = 400, arrows = smalltwo, number = 2, [[0, 4, 0.1], [0, 0.2, 4.1], [0, 7, 0.2], [0, 0.2, 7]], color = red, linecolor = blue, numsteps = 100)

 

But, there is an error saying "Error, (in DEtools/DEplot) vars must be declared as a list, e.g. [x(t),y(t),...]". However, I did the same for other problem and worked well tho. I have no idea what the problem is for above.

When I apply the uses function with the Physics package in a procedure, the commands in this package are not restricted to the inside of the procedure, but are applied globally. See the example below:

gds := proc(LL, qi, t)

 local ta,i;  

uses Physics;

ta := sec(diff(diff(LL, diff(qi[i](t), t)), t), i = 1 .. nops(qi));

RETURN(ta) end:

sxy := diff(x(t), t)^2 + diff(y(t), t)^2:

gds(sxy, [x, y], t);

Error, (in Physics:-diff) name expected for external function
 

On the other hand, when I apply the uses function with the LinearAlgebra package in a procedure, the commands in this package are restricted to the inside of the procedure only.
dst:=proc(MM) 

local DA; 

uses LinearAlgebra;

DA:=Determinant(MM); 

RETURN(DA) end:

dst(<<1 | 2>, <3 | 4>>);

                  -2

Determinant(<<1 | 2>, <3 | 4>>);

                         Determinant(Matrix(2, 2, [[1, 2], [3, 4]]))

This could be a bug in Maple 2019?

The following differential equation:

sy := (dsolve({T*diff(y(x), x, x) + rho*omega^2*y(x) = 0, y(0) = 0, y(L) = 0}, y(x)) assuming (0 < T, 0 < omega, 0 < rho))

                                                                 sy:=y(x)=0

Maple only returns the trivial solution. Should return other expressions:
Thank you

step := t −> piecewise(t sum( (−1)∧n * step(t−n) , n=0..50);

Consider the following forced D.E. L d 2x dt2 + R dx dt + 1 C x = 200 ∗ f(t/3)

where R = 20 , C = 0.01 , L = 10 , x(0) = 10 , x 0 (0) = 0.

a. Graph the solution curve in the phase plane on Maple.

b. Graph x(t) over the interval 0 ≤ t ≤ 25 on Maple .

 

 

For the DE solution below, Maple returns only one option. I can't get the others. Can anyone help?

wW := unapply(piecewise(0 <= x and x < L/2, 0, L/2 <= x and x <= L, w[0]), x):

eq := k*diff(y(x), x$4) = wW(x):

dsolve({eq, y(0) = 0, y'(0) = 0, y''(L) = 0, y'''(L)=0}, y(x)) assuming 0 < L

          y(x) = -L*w[0]*x^3/(6*k) + L^2*w[0]*x^2/(4*k) + w[0]*x^4/(24*k)

Obrigado.
Oliveira

given an expression such as expr:=-1/2*x*(y^2-1) which in tree form will be

I can get -1/2 using op(1,expr).   I need command to return the "rest" of the right side of the tree, all of it, not matter how big.

I tried op(2..nops(expr),expr) and that returns x, y^2 - 1 

Is there a way to return directly x*(y^2-1)*etc....  so I do not have to play around with the above expression sequence?  Another option is to type

           expr/op(1,expr)

to get the right side of the tree. But this seems like a hack. Do not like to divide. worry about zero.

The same thing if the type was `+`, I want to get  the whole right side in one command.

Again, I can do  expr-op(1,expr) to get the whole right side. But this also seems like  a hack, altought not as bad as with  the case above

Any hints on how to best do these things?

Maple 2019.1

 

 

 

I never used applyrule before. Seems like a good command. I was trying to tell Maple to rewrite

as

I could not find one command to do it.  Collect does not work. The only way I figure to it, is by replacing 

by a new single unused symbol, and then use collect on the symbol, then use subs again to replace the symbol with orginal  expression. like this

restart;
expr:=(x^2 - 1)/x^2*y + (x^2 - 1)/x^2;
r:=(x^2 - 1)/x^2; #this is what I want to collect
applyrule(r=Z,expr);
collect(%,Z);
subs(Z=r,%)

In the above code, if I replace applyrule by algsubs, it does not work. Why?

restart;
expr:=(x^2 - 1)/x^2*y + (x^2 - 1)/x^2;
r:=(x^2 - 1)/x^2;
algsubs(r=Z,expr);

You see, it did not replace (x^2-1)/x by Z, like applyrule does:

restart;
expr:=(x^2 - 1)/x^2*y + (x^2 - 1)/x^2;
r:=(x^2 - 1)/x^2;
applyrule(r=Z,expr);

I will learn applyrule now more to do this. I was just wondering why algsubs failed, and if applyrule is the better tool to do this. Or may be there is a better way to do this whole thing?

Maple 2019.1

I made a typo below, I did not mean to put [...] around the solution. But why Maple throws an error on this only?

restart;
ode:=diff(y(x),x) = exp(x)*sin(x)+y(x)*cot(x);
my_sol:=[sin(x)*(exp(x)+_C1)];
odetest( y(x)=my_sol,ode) assuming x::real

          Error, (in type/algext) too many levels of recursion

But on other ODE's, it works

restart;
ode:=diff(y(x),x) = y(x);
my_sol:=[_C1*exp(x)];
odetest( y(x)=my_sol,ode) assuming x::real

                [0]

I'll correct my type and remove the []. But the question is, should Maple have thrown an error? And why only on this one? Would this be considered a bug?

Removing the [] from the first example above, the error is gone and 0 is returned.

Maple 2019.1, Physics V 395 on windows 10

I'm new to Maple.

My problem is that if I input the command sqrt(3.0), for example, I get this strange result:

1.81847767202745*10^(-58) + (7.53238114626421*10^(-59))*I

The results is the same, no matter the argument of sqrt.

Also, when using ln, I get this:

-265.745524189222 + 0.785398163397448*I

Again, no matter the argument of ln, the result is the same.

What is happening?

I thought I remembed how to do this once in Maple, or asking something like this here, but may be it was something similar. But I am not able to figure it now or remember.

Given an expression, I want to find all occuranes of a pattern in it.  Not just one.  So this is like using patmatch but over and over again untill all patterns found. I'll give an example to make it easy to explain.

Given

expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;

I want to find all patterns of form y^n  so the result should be 

{y^(3/2), y^7, y^2, 1/y, y}

This below is how it is done in Mathematica, but having hard time translating this code to Maple.

The last line below does the actual repeated pattern matching. That line was not written by me. It is something from Mathematica forum at stackexchange and I use it all the time and it works well.

ClearAll[x,y,n]
expr = y^2*Sin[1/y] + y^(3/2) + y + x*y^7;
pat = y^n_.;
Last@Reap[expr /. a : pat :> Sow[a], _, Sequence @@ #2 &]

I looked at hastype also. But hastype will only tell me if the pattern is there or not. May be I did not use it right.

restart;
expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;
hastype(expr,symbol^(anything));

Gives true

I tried patmatch, but again, it only find one:

restart;
expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;
if patmatch(expr,a::anything+y^(n::anything)+b::anything,'la') then
   assign(la);
   n;
fi;

And the above is not even robust for finding one. Since I have to change the pattern again to account for multiplication/addition cases between terms. 

Is it possible to do in Maple the same thing I show in Mathematica? I am sure it is possible, but can't now find the right function in Maple.

Maple 2019.1

I experienced a significant obstacle while trying to generate independent random samples with Statistics:-Sample on different nodes of a Grid multi-processing environment. After many hours of trial-and-error, I discovered an astonishing workaround, and I achieved excellent time and memory performance. Since this seems like a generally useful computation, I thought that it was worthy of a Post.

This Post is also worth reading to learn how to use Grid when you need to initialize a substantial environment on each node before using Grid:-Map or Grid:-Seq.

All remaining details are in the following worksheet.
 

How to use Statistics:-Sample in the `Grid` environment

Author: Carl Love <carl.j.love@gmail.com> 1 August 2019

 

I experienced a significant obstacle while trying to generate indenpendent random samples with Statistics:-Sample on the nodes of a multi-processor Grid (on a single computer). After several hours of trial-and-error, I discovered that two things are necessary to do this:

1. 

The random number generator needs to be seeded differently in each node. (The reason for this is easy to understand.)

2. 

The random variables generated by Statistics:-RandomVariable need to have different names in each node. This one is mind-boggling to me. Afterall, each node has its own kernel and so its own memory It's as if the names of random variables are stored in a disk file which all kernels access. And also the generator has been seeded differently in each node.

 

Once these things were done, the time and memory performance of the computation were excellent.

restart
:

Digits:= 15
:

#Specify the size of the computation:
(n1,n2,n3):= (100, 100, 1000):
# n1 = size of each random sample;
# n2 = number of samples in a batch;
# n3 = number of batches.

#
#Procedure to initialize needed globals on each node:
Init:= proc(n::posint)
local node:= Grid:-MyNode();
   #This is wrapped in parse so that it'll act globally. Otherwise, an environment
   #variable would be reset when this procedure ends.
   parse("Digits:= 15;", 'statement');

   randomize(randomize()+node); #Initialize independent RNG for this node.
   #If repeatability of results is desired, remove the inner randomize().

   (:-X,:-Y):= Array(1..n, 'datatype'= 'hfloat') $ 2;

   #Perhaps due to some oversight in the design of Statistics, it seems necessary that
   #r.v.s in different nodes **need different names** in order to be independent:
   N||node:= Statistics:-RandomVariable('Normal'(0,1));
   :-TRS:= (X::rtable)-> Statistics:-Sample(N||node, X);
   #To verify that different names are needed, change N||node to N in both lines.
   #Doing so, each node will generate identical samples!

   #Perform some computation. For the pedagogical purpose of this worksheet, all that
   #matters is that it's some numeric computation on some Arrays of random Samples.
   :-GG:= (X::Array, Y::Array)->
      evalhf(
         proc(X::Array, Y::Array, n::posint)
         local s, k, S:= 0, p:= 2*Pi;
            for k to n do
               s:= sin(p*X[k]);  
               S:= S + X[k]^2*cos(p*Y[k])/sqrt(2-sin(s)) + Y[k]^2*s
            od
         end proc
         (X, Y, n)
      )      
   ;
   #Perform a batch of the above computations, and somehow numerically consolidate the
   #results. Once again, pedagogically it doesn't matter how they're consolidated.  
   :-TRX1:= (n::posint)-> add(GG(TRS(X), TRS(Y)), 1..n);
   
   #It doesn't matter much what's returned. Returning `node` lets us verify that we're
   #actually running this on a grid.
   return node
end proc
:

The procedure Init above uses the :- syntax to set variables globally for each node. The variables set are X, Y, N||node, TRS, GG, and TRX1. Names constructed by concatenation, such as N||node, are always global, so :- isn't needed for those.

#
#Time the initialization:
st:= time[real]():
   #Send Init to each node, but don't run it yet:
   Grid:-Set(Init)
   ;
   #Run Init on each node:
   Nodes:= Grid:-Run(Init, [n1], 'wait');
time__init_Grid:= time[real]() - st;

Array(%id = 18446745861500764518)

1.109

The only purpose of array Nodes is that it lets us count the nodes, and it lets us verify that Grid:-MyNode() returned a different value on each node.

num_nodes:= numelems(Nodes);

8

#Time the actual execution:
st:= time[real]():
   R1:= [Grid:-Seq['tasksize'= iquo(n3, num_nodes)](TRX1(k), k= [n2 $ n3])]:
time__run_Grid:= time[real]() - st

4.440

#Just for comparison, run it sequentially:
st:= time[real]():
   Init(n1):
time__init_noGrid:= time[real]() - st;

st:= time[real]():
   R2:= [seq(TRX1(k), k= [n2 $ n3])]:
time__run_noGrid:= time[real]() - st;

0.16e-1

24.483

R1 and R2 will be different because different random numbers were used, but they should have similar histograms.

plots:-display(
   Statistics:-Histogram~(
      <R1 | R2>, #side-by-side plots
      'title'=~ <<"With Grid\n"> | <"Without Grid\n">>,
      'gridlines'= false
   )
);

(Plot output deleted because MaplePrimes cannot handle side-by-side plots!)

They look similar enough to me!

 

Let's try to quantify the benefit of using Grid:

speedup_factor:= time__run_noGrid / time__run_Grid;

5.36319824753560

Express that as a fraction of the theoretical maximum speedup:

efficiency:= speedup_factor / num_nodes;

.670399780941950

I think that that's really good!

 

The memory usage of this code is insignificant, which can be verified from an external memory monitor such as Winodws Task Manager. It's just a little bit more than that needed to start a kernel on each node. It's also possible to measure the memory usage programmatically. Doing so for a Grid:-Seq computation is a little bit beyond the scope of this worksheet.

 


 

Download GridRandSample.mw

Here are the histograms:

I do not understand why the following is not working. I have a package which has a module inside it. The module is exported by the package.

Inside that module, there is a proc, which is also exported by the module.

So why can't one call the proc from outside the package? What Am I doing wrong? and how to correct it? I'd like to be able to call the proc directly. 

A:=module()
  option package;
  export B;
  B := module()
      export foo;

      foo:=proc()
           print("in foo");
      end proc;

  end module;
end module;   

Now when typing (A::B):-foo() Maple is not happy and says Error, module does not export `foo`

I tried different syntax from the above, but can't get it to work. For example A::B:-foo() gives Error, `B` does not evaluate to a module

 

Maple 2019.1

2 3 4 5 6 7 8 Last Page 4 of 12