MaplePrimes Questions

I am generating splines using CurveFitting[Spline] command, which produces piecewise functions. I would like to extract the different "pieces" of the spline for use elsewhere. How do I do that? In the attached worksheet, for example, I would like to extract (59*x^2)/756, -1/84*x^2 + 17/63*x - 17/84, and -19/504*x^2 + 19/36*x - 61/72. There has to be a way to do this, but I'm coming up short.

spline.mw
 

with(CurveFitting)

f := Spline([[0, 0], [3, 1/2], [7, 1]], x, degree = 2)

piecewise(x < 3/2, (59/756)*x^2, x < 5, -(1/84)*x^2+(17/63)*x-17/84, -(19/504)*x^2+(19/36)*x-61/72)

(1)

``


 

Download spline.mw

 

I want to plot some oscillating function (function involve sin and cos) as a function to time (t). And when I try a large interval like t=0..1000, it is going to take forever to plot the graph and the system becomes a little laggy. Is there any way I can do to make it compute quickly?

Thank you all

11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

Hello

I am trying to use Threads in my procedures as much as possible.  However, one of them returned different results when compared to map or Grid:-Map (I have checked if the used functions are threadsafe (perhaps I miss something)).  Here is the procedure (optimization of the code is most welcome).

searchMonomialsEqns:=proc(conds::set,Eqns::list,Vars::list,poolofeqns::list(list))
description "Find if a set of monomials in an equation can be found in a pool of monomials and returns the condition when it is true":
local A:=Array(1..0),
      C,
      i,
      res,
      n:=numelems(Eqns):
#  Find the monomials of Eqns
res:=subs(conds,Eqns):
for i from 1 to n do
    C:= coeffs(expand(lhs(res[i])-rhs(res[i])),Vars, 'M'):
    A,={M}:
end do:
ifelse(member([seq(A)],poolofeqns)=false,NULL,conds):
end proc:

1) Threads:-Map

ans1:=CodeTools:-Usage(Threads:-Map(w->searchMonomialsEqns(w,eqns[1..2],vars[2..3],validYZeqnMon),conds5)):nops(ans1);

returns

memory used=0.58GiB, alloc change=139.56MiB, cpu time=40.23s, real time=11.12s, gc time=1.01s

                              8613

 

2) map

ans2:=CodeTools:-Usage(map(w->searchMonomialsEqns(w,eqns[1..2],vars[2..3],validYZeqnMon),conds5)):nops(ans2);

returns

memory used=0.57GiB, alloc change=-4.00MiB, cpu time=22.48s, real time=21.55s, gc time=1.70s

                              8637

3) Grid:-Map

ans3:=CodeTools:-Usage(Grid:-Map(w->searchMonomialsEqns(w,eqns[1..2],vars[2..3],validYZeqnMon),conds5)):nops(ans3);

return

memory used=23.29MiB, alloc change=21.88MiB, cpu time=3.77s, real time=2.25s, gc time=3.14s

                              8637

Although the number of elements is the same, Grid:-Map returns the result with set function mentioned in my previous post. (I am aware that CodeTools:-Usage is pointless here).

4) Threads:-Seq

ans4:=CodeTools:-Usage([Threads:-Seq](searchMonomialsEqns(conds5[i],eqns[1..2],vars[2..3],validYZeqnMon),i=1..nops(conds5))):nops(ans4);

returns

memory used=0.58GiB, alloc change=0 bytes, cpu time=33.99s, real time=8.68s, gc time=644.74ms

                              8622

What am I missing?   

Many thanks

Ed

 

"What's new in Maple 2020" explain an exciting new option for DrawGraph: layout=interactive, that should allow you to move vertices in the plot. I tried this in the help page opened in a worksheet window and it works.

Unfortunately when I write in a new worksheet the command

with(GraphTheory): with(RandomGraph): G:=RandomGraph(10,25); DrawGraph(G,layout=interactive)

I get the error

"Error, invalid input: GraphTheory:-DrawGraph expects value for keyword parameter [style, layout] to be of type identical(none, bipartite, circle, default, fixed, grid, interactive, network, spectral, spring, spring[constant], tree, user, planar, random), but received plots:-interactive"

Could you help me? Thanks

This is really a math question. But I can't figure how Maple did it.

Maple solves the ODE with 2 initial conditions correctly.

But if I use the general solution, and setup the set 2 equations, and tell Maple to solve _C1 and _C2, it says there is no solutions. Which is correct.

I would really like to know how Maple then managed to solve for these initial conditions.  This is problem from text book

I know how to solve it by hand to obtain general solution. But do not know how find solution that satisfies the IC's. when pluggin the initial conditions, the resulting 2 equation have no solution.

restart;
ode:=2*diff(y(x),x$2)=exp(y(x));
maple_general_sol:=dsolve(ode);
odetest(maple_general_sol,ode);
IC:=y(0)=0,D(y)(0)=1;
maple_sol_with_IC:=dsolve([ode,IC]);
odetest(maple_sol_with_IC,[ode,IC]);

The goal is to now take the general solution found by Maple above, and manually solve for _C1 and _C2:

eq1:=0=subs(x=0,rhs(maple_general_sol));
the_derivative:= diff(rhs(maple_general_sol),x):
eq2:=1=subs(x=0,the_derivative);

Two equations, two unknown. But using solve or PDEtools:-Solve produce no solution.

sol1:=solve([eq1,eq2],[_C1,_C2]);
sol2:=PDEtools:-Solve([eq1,eq2],[_C1,_C2]);

Looking at equation (1) above, we see that it is the same as 

eq1:=1=(tan(_C2/(2*_C1))^2 + 1)/_C1^2;

Because 0=ln(Z) means Z=1. Using the above simpler equation now gives

eq1:=1=(tan(_C2/(2*_C1))^2 + 1)/_C1^2;
the_derivative:= diff(rhs(maple_general_sol),x):
eq2:=1=subs(x=0,the_derivative);
sol1:=solve([eq1,eq2],[_C1,_C2]);

We see that there is indeed no solution. Second equation above says _C1= tan(.). Let tan(.)=Z. Plugging this in first equation gives Z^2= (Z^1+1) which has no solution for Z since this says 0=1 

So direct appliction of the initial conditions produces no solution. So how did Maple find the solution it obtained? I also tried using limits. But no luck. Book does not show how to obtain solution either. 

Any ideas?

 

When solving this by hand   sqrt(y)=tanh(x), I would square both sides to obtain  y=tanh(x)^2 as solution.  

Maple does this when RHS is tan,cos,sin, but not when RHS is hyperbolic.

Why is that, and how to best tell Maple to give  the simple solution? Here is an example

restart;
eq:=sqrt(y)=tanh(x);
sol:=solve(eq,y);

Compare to Mathematica:

I can post-process Maple solution like this

restart;
eq:=sqrt(y)=tanh(x);
sol:=solve(eq,y);
sol:=convert(sol,trigh);
sol:=simplify(sol)

Which is tanh(x)^2.  

But this is all too much work, since this is done in a program, and I do not know what specific convert to use at the time to try.

My question is, why does not maple simply solves   sqrt(y) = f(x)  giving     y=f(x)^2?  it works for generic function

restart;
eq:=sqrt(y)=f(x);
sol:=solve(eq,y);

it works for non-hyper trig functions also

restart;
eq:=sqrt(y)=tan(x);
sol:=solve(eq,y);

Is there a trick to make it work for hyperbolic functions also automatically?

ps. for now, to avoid all these issues, I am doing this

restart;
the_rhs:=tanh(x);
eq:=sqrt(y)=f;
sol:=solve(eq,y);
sol:=subs(f=the_rhs,sol);

Since I know that the RHS contains no y before hand. The above bypasses any issues Maple has with hyper trig functions.

Hello

Without any success, I've been trying to hide the input/code, such that only the output (moment and shear) diagram is shown. I've tried guides with Document Blocks method but all seem outdated, since they're referring to commands which don't seem to exist i.e. "collapse document block". I can't understand that there isn't some neat and user-friendly way to something as simple as show/hide the input, to make it more document-reader friendly? It seems the only way is to just hightlight the input and color it white, which isn't too viable long term. 

I hope you can help, it would be alot of help to me and my classmates. 
Kind regards and thanks for your time.

 

PS. Here's a picture of the input and diagram.

restart, with(plots);
startingTime := 1800;
interval := 10;
P(0) := 5.308;
P(t) = 5.308*M/(5.308 + (M - 5.308)*exp(-kMt));
                                5.308 M           
              P(t) = -----------------------------
                     5.308 + (M - 5.308) exp(-kMt)


diff(P(t), t) = kP(t)*(M - P(t));
                   d                         
                  --- P(t) = kP(t) (M - P(t))
                   dt                        

Explore(plot([P(t), [[0, 5.308], () .. (), [140, 131.7]]], t = 0 .. 0.140, P = 0 .. 140, style = [line, point], gridlines = true), parameters = [[M = M_0 .. M_1], [k = k_0 .. k_1]]);
Error, (in Explore) incorrect first argument [P(t), [[0, 5.308], () .. (), [140, 131.7]]]

I am currently trying to solve the following ODE using numerical methods:

diff(U(x), x$2) + [(z+ I*y)2/k12 -k22 + ((z+I*y)/k3)*sech(x)^2U(x)

Where the complex value (in this case, omega, has been written as z+Iy). I believe dsolve has capabilities for solving this as an initial value problem with complex values and thus to solve this as a boundary value problem I aim to use fsolve to find a zero other a function which is the (IVP solution) - (the non-initial boundary). This has worked very well for the case where y=0 however does not work for values of y>0, and it seems the problem is with fsolve. Any advice on how to deal with this problem, perhaps alternatives to fsolve? 

Is there any possibility to modify column width within tables, so that they become equal?

Dear Users! 

I want to evaluate following integral (f[i]'s) for different values of rho as shown. But find some problem and can't find f[3], f[4], f[5] and f[6]. If the integral from direct way is hard to calculate please guide me how I can evaluate it using numerical integration?

restart; HAM := [1, 2, 3, 4, 5, 6]; U := y^6*sin(x); alpha := 1/3;

for i while i <= nops(HAM) do

rho := op(i, HAM);

f[i] := int(int(U/((x^rho-p^rho)^alpha*(y^rho-q^rho)^alpha), q = 0 .. y), p = 0 .. x)

end do

Thanks in advance!

Special request to @acer @Carl Love @Kitonum @Preben Alsholm

Hi, 

I want to simulate a decision rule (let's say a production control strategy to fix the ideas) based on the outcomes of a binomial random variable.

One stage of this simulation involves operations which correspond to the NaiveProc described below. This procedure is a straightforward and rather naive translation in Maple of these operations.
It is given here to help you to understand what is to be done.

This coding being extremely slow, I implemented the required operations differently.
Given the fact that I'm only concerned by binomial samplings with outcomes 0 and 1, I basically replaced these samplings by three operations  U:=... , Got01:=... , Got0:= ... .
Each new implementation is about 200 times faster than the naive one.

My question is: Can we do still better?
For information the same simulation realized with R runs in less than 15s when REP = 10^8 (more than 100 times faster than what I'm capable to do with Maple)

Thanks for your help.


 

restart:

with(Statistics):

# This is the naive coding of the problem
#
# It has the advantage to explain clearly (at least I hope so) what is to be done.

NaiveProc := proc(REP, a, b, N)
  local roll, rep, p, K, N0, N1:
  roll := rand(a .. b);
  N0   := 0:
  N1   := 0:
  for rep from 1 to REP do
    p := roll();
    K := Sample(Binomial(N, p), 1)[1]:
    if K=0 then
      N0 := N0+1:
    elif K=1 then
      N1 := N1+1:
    end if:
  end do:
  N0, N1:
end proc:
    
CodeTools:-Usage(NaiveProc(10^3, 0, 5e-4, 25000));

memory used=78.92MiB, alloc change=68.00MiB, cpu time=1.21s, real time=6.30s, gc time=24.80ms

 

93, 80

(1)

# As the previous code is very slow I implemented several variants based on the fact
# that I care only on values of K equal to 0 and 1 and that it is then useless to sample
# a Binomial random variable to do this.
#
# Each of them is much faster than the naive coding... but can we even faster?
# For comparison, the same simulation realized in R takes less than 15s to run with REP=10^8

pi := (n, p, k) -> binomial(n, k) * (1-p)^(n-k) * p^k;


f1 := proc(REP, a, b, N)
  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
  Q     := Sample(Uniform(a, b), REP)^+:
  P0    := pi~(N, Q, 0):
  P1    := pi~(N, Q, 1):
  P01   := P0 + P1:
  U     := Sample(Uniform(0, 1), REP)^+:
  Got01 := Select(t -> is(t <= 0), U-P01):
  Got0  := Select(t -> is(t <= 0), U-P0 );
  N01   := numelems(Got01):
  N0    := numelems(Got0):
  N1    := N01-N0:
  N0, N1;
end proc:

CodeTools:-Usage(f1(10^5, 0, 5e-4, 25000));

print():

f2 := proc(REP, a, b, N)
  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
  Q     := Sample(Uniform(a, b), REP)^+:
  P0    := pi~(N, Q, 0):
  P1    := pi~(N, Q, 1):
  P01   := P0 + P1:
  U     := Sample(Uniform(0, 1), REP)^+:
  Got01 := select[flatten](`<=`, U-P01, 0):
  Got0  := select[flatten](`<=`, U-P0 , 0):
  N01   := numelems(Got01):
  N0    := numelems(Got0):
  N1    := N01-N0:
  N0, N1;
end proc:

CodeTools:-Usage(f2(10^5, 0, 5e-4, 25000));

print():

f3 := proc(REP, a, b, N)
  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
  Q     := Sample(Uniform(a, b), REP)^+:
  P0    := pi~(N, Q, 0):
  P1    := pi~(N, Q, 1):
  P01   := P0 + P1:
  U     := Sample(Uniform(0, 1), REP)^+:
  Got01 := Vector(REP, i -> `if`(U[i] <= P01[i], 1, 0)):
  Got0  := Vector(REP, i -> `if`(U[i] <= P0 [i], 1, 0)):
  N01   := add(Got01):
  N0    := add(Got0):
  N1    := N01-N0:
  N0, N1;
end proc:

CodeTools:-Usage(f3(10^5, 0, 5e-4, 25000));

proc (n, p, k) options operator, arrow; binomial(n, k)*(1-p)^(n-k)*p^k end proc

 

memory used=376.83MiB, alloc change=296.77MiB, cpu time=4.12s, real time=4.03s, gc time=430.38ms

 

8118, 7979

 

 

memory used=173.26MiB, alloc change=0 bytes, cpu time=2.30s, real time=1.98s, gc time=461.22ms

 

7993, 7793

 

 

memory used=154.19MiB, alloc change=0 bytes, cpu time=2.28s, real time=2.01s, gc time=432.40ms

 

7989, 7869

(2)



 

 


 

Download MakeItFaster.mw

When Maple converts a matrix or a vector to Latex it uses latex array, which  is fine, but what it does is insert an exatra \\ at the end of the last row, just before the \end{array} 

This causes problem for some software which reads the latex file. It causes the software to take this as empty row and inserts a blank row at the end.

Here is a small example

A:=Matrix([[1,2],[3,4]]);
Physics:-Latex(A);

              \left[\begin{array}{cc}1 & 2 \\3 & 4 \\\end{array}\right]

Notice the extra \\  just before the \end{array} In Latex, this tells latex compiler to insert newline basically.

When opening the latex file using external software, which reads Latex and converts it to GUI, it show this

And now I have to go edit the latex by hand removing the extra \\ (inside the editor I can do global search and replace)

A better outout from Maple's Latex command would be the following

\left[\begin{array}{cc}1 & 2 \\3 & 4 \end{array}\right]

Even though both are valid Latex, the above is better.  I was wondering how hard it is to do this change.

All what the conversion needs to do is just check if the row in the matrix (or Vector) is the last one, and omit the extra \\ at the end of the last row. That is all. 

I could ofcourse write code to try to parse this each time I call Latex and to remove \\ myself in the program, but it will be much easier if Maple automatically did this.

 

ps. if the Latex conversion source code could be made public, others could help make improvement to it for free, benefiting Maple and everyone using it. Maplesoft would still approve the changes before checkin, and retain the copy right of the source code ofcourse. 

 

Maple 2020.1

 

First 441 442 443 444 445 446 447 Last Page 443 of 2385