Applications, Examples and Libraries

Share your work here

 Obtain the tri-stimulus XYZ values from the CIE Color matching functions.

 Show the gamut of maximum chroma for the standard observer model with a D65 Illuminant.

 Approximate the white point of a Planckian source and compare to D65.

 Translate the maximum chroma gamut in xy to Lab (CIE L*a*b*) for perceived gamut (Violet and Magenta come together)

 Map the RGB color cube of fully saturated color into Lab and compare to perceivable colors.

10/6/15  Initial Document

•12/28/15 Improve RGB gamut with more data points: Procedures added for RGB to Lab: Wavlength Colors now based on CIEDE2000 model for Lab.                   

 

 Here is the latest version of this document, the MSL_data must be in a directory set in the mw file;

MSL_data.xlsx    Vision_RGB_Gamut.mw

In this paper we will demonstrate the many differences of implementation in the modeling of mechanical systems using embedded components through Maplesoft. The mechanical systems are used for different tasks and therefore have different structure in its design; as to the nature of the used functional elements placed on them, they vary greatly. This diversity is reflected in approaches and practices in modeling.

The following cases focus on mechanical components of the units manufacturing and processing machines. We can generate graphs for analysis using different dynamic pair ametros; all in real-time considerations in its manufacturing costs from the equations of conservation of energy.
Therefore modeling with Maplesoft ensures the smooth optimum performance in mechanical systems, highlighting the sustainability criteria for other areas of engineering.

 

XXXIII_Coloquio_SMP_2015.pdf

XXXIII_Coloquio_UNASAM_2015.mw

(in spanish)

L.AraujoC.

 

 

Apparently inconsistent behaviour of the BesselJ() function.

Examples: BesselJ(-3, 0)  ... gives 0 (correct)

but BesselJ(-3.0, 0), BesselJ(-3, 0.0)  and BesselJ(-3, 0.0) all give Float(infinity) (wrong! - should be 0.0)

The problem seems to occur for all negative integer values of the first argument (the order) when the second argument is 0 or 0.0.


One of the interesting things about the Physics package is that it was designed from scratch to extend the domain of operations of the Maple system from commutative variables to one that includes commutative, anticommutative and nonocommutative variables, as well as abstract vectors and related (nabla) differential operators. In this line we have, among others, the following Physics commands working with this extended domain: `*` , `.` , `^` , diff , Expand , Normal , Simplify , Gtaylor , and Coefficients .

 

More recently, Pascal Szriftgiser (from Laboratoire PhLAM, Université Lille 1, France), suggested a similar approach to factorize expressions involving noncommutative variables. This is a pretty complicated problem though. Pascal's suggestion, however, spinned around an idea beautiful for its simplicity, similar to what is done in the experimental Physics command, PerformOnAnticommutativeSystem , that is, to transform the problem into one that can be treated with the command that works only with commutative variables and from there extract the result for noncommutative ones.The approach has limitations but it is surprising how far one can go using imaginative algebraic manipulations to extend these commands that otherwise only work with commutative variables.

 

In brief, we now have a new command, Physics:-Factor, with already powerful performance for factorizing algebraic expressions that involve commutative, noncommutative and anticommutative variables, making Maple's mathematical capabilities more advanced in very interesting directions. This command is in fact useful not just in advanced theoretical physics, but for instance also when working with noncommutative symbols representing abstract matrices (that can have dependency, and so they can be differentiated before saying anything about their components, multiplied, and be present int  expressions that in turn can be expanded, simplified and now also factorized), and also useful with expressions that include differential operators, now that within Physics you can compute with the the covariant and noncovariant derivatives D_  and d_ algebraically. For instance, how about solving differential equations using Physics:-Factor (reducing their order by means of factoring the involved differential operators) ? :)

 

What follows are some basic algebraic examples illustrating the novelty, and as usual to reproduce the results in this worksheet you need to update your Physics library with the one available in the Maplesoft R&D Physics webpage.

 

Physics:-Version()[2]

`2015, September 25, 7:48 hours`

(1)

with(Physics); -1; Setup(quantumoperators = {a, b, c, d, e}, mathematicalnotation = true)

[mathematicalnotation = true, quantumoperators = {a, b, c, d, e}]

(2)

First example, because of using mathematical notation, noncommutative variables are displayed in different color (olive)

Physics:-`*`(Physics:-`^`(alpha, 2), Physics:-`^`(a, 2))+Physics:-`*`(Physics:-`*`(Physics:-`*`(alpha, sqrt(2)), a), b)+Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(4, sqrt(2)), lambda), Physics:-`^`(b, 2)), c)+Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(4, lambda), alpha), b), c), a)+Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(4, lambda), sqrt(2)), b), c), b)+Physics:-`*`(Physics:-`*`(16, Physics:-`^`(lambda, 2)), Physics:-`^`(Physics:-`*`(b, c), 2))+Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(Physics:-`*`(4, alpha), lambda), a), b), c)+Physics:-`*`(Physics:-`*`(Physics:-`*`(sqrt(2), alpha), b), a)+Physics:-`*`(2, Physics:-`^`(b, 2))

alpha^2*Physics:-`^`(a, 2)+alpha*2^(1/2)*Physics:-`*`(a, b)+4*2^(1/2)*lambda*Physics:-`*`(Physics:-`^`(b, 2), c)+4*lambda*alpha*Physics:-`*`(b, c, a)+4*2^(1/2)*lambda*Physics:-`*`(b, c, b)+16*lambda^2*Physics:-`^`(Physics:-`*`(b, c), 2)+4*lambda*alpha*Physics:-`*`(a, b, c)+alpha*2^(1/2)*Physics:-`*`(b, a)+2*Physics:-`^`(b, 2)

(3)

Physics:-Factor(alpha^2*Physics:-`^`(a, 2)+alpha*2^(1/2)*Physics:-`*`(a, b)+4*2^(1/2)*lambda*Physics:-`*`(Physics:-`^`(b, 2), c)+4*lambda*alpha*Physics:-`*`(b, c, a)+4*2^(1/2)*lambda*Physics:-`*`(b, c, b)+16*lambda^2*Physics:-`^`(Physics:-`*`(b, c), 2)+4*lambda*alpha*Physics:-`*`(a, b, c)+alpha*2^(1/2)*Physics:-`*`(b, a)+2*Physics:-`^`(b, 2))

Physics:-`^`(4*lambda*Physics:-`*`(b, c)+a*alpha+2^(1/2)*b, 2)

(4)

A more involved example from a physics problem, illustrating that the factorization is also happening within function's arguments, as well as that we can also correctly expand mathematical expressions involving noncommutative variables

PDEtools:-declare((a, b, c, g)(x, y)):

a(x, y)*`will now be displayed as`*a

 

b(x, y)*`will now be displayed as`*b

 

c(x, y)*`will now be displayed as`*c

 

g(x, y)*`will now be displayed as`*g

(5)

Physics:-Intc(Physics:-`^`(Physics:-`*`(Physics:-`*`(Physics:-`*`(4, Physics:-Dagger(b(x, y))), c(x, y)), lambda)+Physics:-`*`(Physics:-`*`(Physics:-`*`(alpha, f(t)), a(x, y)), Physics:-Dagger(a(x, y)))+Physics:-`*`(Physics:-`*`(sqrt(2), g(x, y)), b(x, y)), 2), x, y)

Int(Int(Physics:-`^`(4*lambda*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y))+alpha*f(t)*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)))+2^(1/2)*g(x, y)*b(x, y), 2), x = -infinity .. infinity), y = -infinity .. infinity)

(6)

So first expand ...

expand(Int(Int(Physics:-`^`(4*lambda*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y))+alpha*f(t)*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)))+2^(1/2)*g(x, y)*b(x, y), 2), x = -infinity .. infinity), y = -infinity .. infinity))

Int(Int(16*lambda^2*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), Physics:-Dagger(b(x, y)), c(x, y))+4*lambda*alpha*f(t)*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), a(x, y), Physics:-Dagger(a(x, y)))+4*lambda*2^(1/2)*g(x, y)*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), b(x, y))+4*alpha*f(t)*lambda*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), Physics:-Dagger(b(x, y)), c(x, y))+alpha^2*f(t)^2*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), a(x, y), Physics:-Dagger(a(x, y)))+alpha*f(t)*2^(1/2)*g(x, y)*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), b(x, y))+4*2^(1/2)*g(x, y)*lambda*Physics:-`*`(b(x, y), Physics:-Dagger(b(x, y)), c(x, y))+2^(1/2)*g(x, y)*alpha*f(t)*Physics:-`*`(b(x, y), a(x, y), Physics:-Dagger(a(x, y)))+2*g(x, y)^2*Physics:-`^`(b(x, y), 2), x = -infinity .. infinity), y = -infinity .. infinity)

(7)

Now retrieve the original expression by recursing over the arguments and so factoring the integrand

Physics:-Factor(Int(Int(16*lambda^2*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), Physics:-Dagger(b(x, y)), c(x, y))+4*lambda*alpha*f(t)*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), a(x, y), Physics:-Dagger(a(x, y)))+4*lambda*2^(1/2)*g(x, y)*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y), b(x, y))+4*alpha*f(t)*lambda*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), Physics:-Dagger(b(x, y)), c(x, y))+alpha^2*f(t)^2*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), a(x, y), Physics:-Dagger(a(x, y)))+alpha*f(t)*2^(1/2)*g(x, y)*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)), b(x, y))+4*2^(1/2)*g(x, y)*lambda*Physics:-`*`(b(x, y), Physics:-Dagger(b(x, y)), c(x, y))+2^(1/2)*g(x, y)*alpha*f(t)*Physics:-`*`(b(x, y), a(x, y), Physics:-Dagger(a(x, y)))+2*g(x, y)^2*Physics:-`^`(b(x, y), 2), x = -infinity .. infinity), y = -infinity .. infinity))

Int(Int(Physics:-`^`(4*lambda*Physics:-`*`(Physics:-Dagger(b(x, y)), c(x, y))+alpha*f(t)*Physics:-`*`(a(x, y), Physics:-Dagger(a(x, y)))+2^(1/2)*g(x, y)*b(x, y), 2), x = -infinity .. infinity), y = -infinity .. infinity)

(8)

This following one looks simpler but it is actually more complicated:

Physics:-`*`(Physics:-Commutator(a, b), c)

Physics:-`*`(Physics:-Commutator(a, b), c)

(9)

expand(Physics:-`*`(Physics:-Commutator(a, b), c))

Physics:-`*`(a, b, c)-Physics:-`*`(b, a, c)

(10)

The complication consists of the fact that the standard factor  command, that assumes products are commutative, can never deal with factors like Physics:-Commutator(a, b) = a*b-a*b because if products were commutative these factors are equal to 0. Of course we not just us factor but include a number of algebraic manipulations before using it, so that the approach handles these cases nicely anyway

Physics:-Factor(Physics:-`*`(a, b, c)-Physics:-`*`(b, a, c))

Physics:-`*`(Physics:-`*`(a, b)-Physics:-`*`(b, a), c)

(11)

This other one is more complicated:

Physics:-`*`(Physics:-`*`(a, b)-Physics:-`*`(b, a), a+Physics:-`*`(beta, b)+Physics:-`^`(c, 2))

Physics:-`*`(Physics:-`*`(a, b)-Physics:-`*`(b, a), a+beta*b+Physics:-`^`(c, 2))

(12)

When you expand,

expand(Physics:-`*`(Physics:-`*`(a, b)-Physics:-`*`(b, a), a+beta*b+Physics:-`^`(c, 2)))

Physics:-`*`(a, b, a)+beta*Physics:-`*`(a, Physics:-`^`(b, 2))+Physics:-`*`(a, b, Physics:-`^`(c, 2))-Physics:-`*`(b, Physics:-`^`(a, 2))-beta*Physics:-`*`(b, a, b)-Physics:-`*`(b, a, Physics:-`^`(c, 2))

(13)

you see that there are various terms involving the same noncommutative operands, just multiplied in different order. Generally speaking the limitation (n this moment) of the approach is: "there cannot be more than 2 terms in the expanded form containing the same operands" . For instance in (13) the 1st and 4th terms have the same operands, that are actually also present in the 5th term but there you also have beta and for that reason (involving some additional manipulations) it can be handled:

Physics:-Factor(Physics:-`*`(a, b, a)+beta*Physics:-`*`(a, Physics:-`^`(b, 2))+Physics:-`*`(a, b, Physics:-`^`(c, 2))-Physics:-`*`(b, Physics:-`^`(a, 2))-beta*Physics:-`*`(b, a, b)-Physics:-`*`(b, a, Physics:-`^`(c, 2)))

Physics:-`*`(Physics:-`*`(a, b)-Physics:-`*`(b, a), a+beta*b+Physics:-`^`(c, 2))

(14)

Recalling, in all these examples, the task is actually accomplished by the standard factor  command, and the manipulations consist of ingeniously rewriting the given problem as one that involves only commutative variables, and from extract the correct result for non commutative variables.

 

To conclude, here is an example where the approach implemented does not work (yet) because of the limitation mentioned in the previous paragraph:

Physics:-`^`(Physics:-Commutator(a, b)+c, 2)

Physics:-`^`(Physics:-Commutator(a, b)+c, 2)

(15)

expand(Physics:-`^`(Physics:-Commutator(a, b)+c, 2))

Physics:-`*`(a, b, a, b)-Physics:-`*`(a, Physics:-`^`(b, 2), a)+Physics:-`*`(a, b, c)-Physics:-`*`(b, Physics:-`^`(a, 2), b)+Physics:-`*`(b, a, b, a)-Physics:-`*`(b, a, c)+Physics:-`*`(c, a, b)-Physics:-`*`(c, b, a)+Physics:-`^`(c, 2)

(16)

In this expression, the 1st, 2nd, 4th and 5th terms have the same operands a, b, a, b and then there are four terms containing the operands a, b, c. We do have an idea of how this could be done too ... :) To be there in one of the next Physics updates.

NULL

NULL


Download Physics[Factor].mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

Maple's dsolve numeric can solve delay ODEs and DAEs as of Maple 18. However, if I am not wrong, it cannot solve delay equations with a time dependent history. In this post I show two examples.

Example 1:

y1(t) and y2(t) with time dependent history. Use of piecewise helps this problem to be solved efficiently. Hopefully Maple will add history soon in its capability.

Example 2: 

This is a very a complicated stiff problem from immunology. As of now, I believe only Maple can solve this (other than RADAR5 from Prof. Hairer). Details and plots are posted in the attached code.

 

Let me know if any one has a delay problem that needs to be solved. I have tested many delay problems in Maple (they work fine). The attached examples required addtional tweaking, hence the post.

 

I want to take this opportunity to congratulate and thank Maple's dsolve numeric/delay solvers for their fantastic job. Maple is world leader not because of example1, but because of its ability to solve example 2.

 

 

restart;

 This code is written by Dayaram Sonawane and Venkat R. Subramnian, University of Washington. You will need Maple 18 or later for this. For those who are wanting to solve these problems in earlier versions, I can help them by offering a procedure based approach (less efficient).

Example1 The first example solved is a state dependent delay problem (http://www.mathworks.com/help/matlab/math/state-dependent-delay-problem.html).

 

eq1:= diff(y1(t),t)=y2(t);

eq1 := diff(y1(t), t) = y2(t)

(1)

eq2:=diff(y2(t),t)=-y2(exp(1-y2(t)))*y2(t)^2*exp(1-y2(t));

eq2 := diff(y2(t), t) = -y2(exp(1-y2(t)))*y2(t)^2*exp(1-y2(t))

(2)

 Both y1(t) and y2(t) have time dependent history (y1(t)=log(t) and y2(t)=1/t, t<-0.1). If I am not mistaken one cannot solve this directly using Maple's dsolve numeric command. However, a simple trick can be used to redefine the equations for y1(t) and y2(t) as below

eq3:=diff(y1(t),t)=piecewise(t<=0.1,1/t,y2(t));

eq3 := diff(y1(t), t) = piecewise(t <= .1, 1/t, y2(t))

(3)

eq4:=diff(y2(t),t)=piecewise(t<=0.1,-1/t^2,-y2(exp(1-y2(t)))*y2(t)^2*exp(1-y2(t)));

eq4 := diff(y2(t), t) = piecewise(t <= .1, -1/t^2, -y2(exp(1-y2(t)))*y2(t)^2*exp(1-y2(t)))

(4)

 The problem is solved from a small number close to t = 0 (1e-4) to make Maple's dsolve numeric remember the history till t = 0.1

epsilon:=1e-4;

epsilon := 0.1e-3

(5)

sol:=dsolve({eq3,eq4,y1(epsilon)=log(epsilon),y2(epsilon)=1/epsilon},type=numeric,delaymax=5):

with(plots):

odeplot(sol,[t,y1(t)],0.1..5,thickness=3,axes=boxed);

 

odeplot(sol,[t,y2(t)],0.1..5,thickness=3,axes=boxed);

 

sol(5.0);log(5.0);1/5.0;

[t = 5.0, y1(t) = 1.60942323180838, y2(t) = .199998786891688]

1.609437912

.2000000000

(6)

Tweaking the tolerances and epsilon will get the solution even more closer to the expected answers.

 

 

 Example 2

 The next problem discussed is very stiff, complicated and as of today, according Professor Hairer (one of the world's leading authority in numerical solutions of ODEs, DAEs), cannot be solved by any other code other than his RADAR (5th order implicit Runge Kutta modified for delay equations, Guglielmi N. and Hairer E. (2001) Implementing Radau IIa methods for stiff delay differential equations. Computing 67:1-12). This problem requires very stringent tolerances. For more information read, http://www.scholarpedia.org/article/Stiff_delay_equations. I can safely say that Maple can boast that it can solve this delay differential equation by using a switch function (instead of Heaviside/picecewise function). Code is attached below and results are compared with the output from RADAR code.  Note that dsolve/numeric is probably taking more time steps compared to RADAR, but the fact that Maple's dsolve numeric solved this model (which cannot be solved in Mathematica or MATLAB[needs confirmation for MATLAB]) should make Maple's code writers proud. It is very likely that we will be trying to submit an educational/research article on this topic/example soon to a journal. For some weird reasons, stiff=true gives slightly inaccurate results.

restart:

 

radar5data:=readdata("C:\\Users\\Venkat16core-office\\Google Drive\\waltmanproblem\\sol.txt",[string,string,float,string,string,float,float,float,float,float,float]):

nops(radar5data);

1059

(7)

radar5data[1059];

["X", "=", 300.000000, "Y", "=", 0.6154486288e-15, 0.3377120916e-6, 0.4221403310e-6, 0.2142554563e-5, 299.9999999, 299.6430338]

(8)

eq[1]:=diff(y[1](t),t)=-r*y[1](t)*y[2](t)-s*y[1](t)*y[4](t);

eq[1] := diff(y[1](t), t) = -r*y[1](t)*y[2](t)-s*y[1](t)*y[4](t)

(9)

eq[2]:=diff(y[2](t),t)=-r*y[1](t)*y[2](t)+alpha*r*y[1](y[5](t))*y[2](y[5](t))*H1;#Heaviside(t-35);

eq[2] := diff(y[2](t), t) = -r*y[1](t)*y[2](t)+alpha*r*y[1](y[5](t))*y[2](y[5](t))*H1

(10)

eq[3]:=diff(y[3](t),t)=r*y[1](t)*y[2](t);

eq[3] := diff(y[3](t), t) = r*y[1](t)*y[2](t)

(11)

eq[4]:=diff(y[4](t),t)=-s*y[1](t)*y[4](t)-gamma1*y[4](t)+beta*r*y[1](y[6](t))*y[2](y[6](t))*H2;#Heaviside(t-197);

eq[4] := diff(y[4](t), t) = -s*y[1](t)*y[4](t)-gamma1*y[4](t)+beta*r*y[1](y[6](t))*y[2](y[6](t))*H2

(12)

eq[5]:=diff(y[5](t),t)=H1*(y[1](t)*y[2](t)+y[3](t))/(y[1](y[5](t))*y[2](y[5](t))+y[3](y[5](t)));#eq[7]:=y[7](t)=HH(t);

eq[5] := diff(y[5](t), t) = H1*(y[1](t)*y[2](t)+y[3](t))/(y[1](y[5](t))*y[2](y[5](t))+y[3](y[5](t)))

(13)

eq[6]:=diff(y[6](t),t)=H2*(10.^(-12)*0+y[2](t)+y[3](t))/(10.^(-12)*0+y[2](y[6](t))+y[3](y[6](t)));

eq[6] := diff(y[6](t), t) = H2*(y[2](t)+y[3](t))/(y[2](y[6](t))+y[3](y[6](t)))

(14)

H1:=1/2+1/2*tanh(100*(t-35));H2:=1/2+1/2*tanh(100*(t-197));

H1 := 1/2+(1/2)*tanh(100*t-3500)

H2 := 1/2+(1/2)*tanh(100*t-19700)

(15)

alpha:=1.8;beta:=20.;gamma1:=0.002;r:=5.*10^4;s:=10.^5;

alpha := 1.8

beta := 20.

gamma1 := 0.2e-2

r := 50000.

s := 100000.

(16)

seq(eq[i],i=1..6);

diff(y[1](t), t) = -50000.*y[1](t)*y[2](t)-100000.*y[1](t)*y[4](t), diff(y[2](t), t) = -50000.*y[1](t)*y[2](t)+90000.0*y[1](y[5](t))*y[2](y[5](t))*(1/2+(1/2)*tanh(100*t-3500)), diff(y[3](t), t) = 50000.*y[1](t)*y[2](t), diff(y[4](t), t) = -100000.*y[1](t)*y[4](t)-0.2e-2*y[4](t)+1000000.*y[1](y[6](t))*y[2](y[6](t))*(1/2+(1/2)*tanh(100*t-19700)), diff(y[5](t), t) = (1/2+(1/2)*tanh(100*t-3500))*(y[1](t)*y[2](t)+y[3](t))/(y[1](y[5](t))*y[2](y[5](t))+y[3](y[5](t))), diff(y[6](t), t) = (1/2+(1/2)*tanh(100*t-19700))*(y[2](t)+y[3](t))/(y[2](y[6](t))+y[3](y[6](t)))

(17)

ics:=y[1](0)=5.*10^(-6),y[2](0)=10.^(-15),y[3](0)=0,y[4](0)=0,y[5](0)=1e-40,y[6](0)=1e-20;

ics := y[1](0) = 0.5000000000e-5, y[2](0) = 0.1000000000e-14, y[3](0) = 0, y[4](0) = 0, y[5](0) = 0.1e-39, y[6](0) = 0.1e-19

(18)

#infolevel[all]:=10;

sol:=dsolve({seq(eq[i],i=1..6),ics},type=numeric,delaymax=300,initstep=1e-6,abserr=[1e-21,1e-21,1e-21,1e-21,1e-9,1e-9],[y[1](t),y[2](t),y[3](t),y[4](t),y[5](t),y[6](t)],relerr=1e-9,maxstep=10,optimize=false,compile=true,maxfun=0):

 

 

 note that compile = true was used for efficiency

t11:=time():sol(300);time()-t11;

[t = 300., y[1](t) = 0.615611371327094e-15, y[2](t) = 0.337706811581908e-6, y[3](t) = 0.422136411682798e-6, y[4](t) = 0.214253771204037e-5, y[5](t) = 299.999986716780, y[6](t) = 299.643054284209]

.141

(19)

with(plots):

nd:=nops(radar5data);

nd := 1059

(20)

radar5data[nd];

["X", "=", 300.000000, "Y", "=", 0.6154486288e-15, 0.3377120916e-6, 0.4221403310e-6, 0.2142554563e-5, 299.9999999, 299.6430338]

(21)

 Values at t = 300 match with expected results.

pr[1]:=plot([seq([radar5data[i][3],log(radar5data[i][6])/log(10)],i=1..nd)],style=point,color=green):

p[1]:=odeplot(sol,[t,log(y[1](t))/log(10)],0..300,axes=boxed,thickness=3):

display({pr[1],p[1]});

 

pr[2]:=plot([seq([radar5data[i][3],log(radar5data[i][7])/log(10)],i=1..nd)],style=point,color=green):

p[2]:=odeplot(sol,[t,log(y[2](t))/log(10)],0..300,axes=boxed,thickness=3,numpoints=1000):

display({pr[2],p[2]});

 

pr[3]:=plot([seq([radar5data[i][3],log(radar5data[i][8])/log(10)],i=2..nd)],style=point,color=green):

 

p[3]:=odeplot(sol,[t,log(y[3](t))/log(10)],0..300,axes=boxed,thickness=3):

display({pr[3],p[3]});

 

pr[4]:=plot([seq([radar5data[i][3],log(radar5data[i][9])/log(10)],i=496..nd)],style=point,color=green,view=[197..300,-9..-5]):

p[4]:=odeplot(sol,[t,log(y[4](t))/log(10)],197..300,axes=boxed,thickness=3,view=[197..300,-9..-5]):

display({pr[4],p[4]});

 

pr[5]:=plot([seq([radar5data[i][3],radar5data[i][10]],i=1..nd)],style=point,color=green):

p[5]:=odeplot(sol,[t,y[5](t)],0..300,axes=boxed,thickness=3):

display({pr[5],p[5]});

 

pr[6]:=plot([seq([radar5data[i][3],radar5data[i][11]],i=1..nd)],style=point,color=green):

p[6]:=odeplot(sol,[t,y[6](t)],0..300,axes=boxed,thickness=3):

display({pr[6],p[6]});

 


Download delayimmunetopost.mws

LL_104)_NASDAQ.mw
Portfolio_Optimization.txt

Portfolio Optimization with Google Spreadsheet and Maple
 

I will in this post show how to manage data and do portfolio optimization in Maple by using google spreadsheet.

You can either use a direct link to the data:

https://docs.google.com/spreadsheets/d/1L5-yUB0EWeBdJNMdELKBRmBQ1JJ0QymrtDLkVhHCVn8/pub?gid=649021574&single=true&output=csv

or you can set up your own google spreadsheet. If you choice to set up your own spreedsheet follow the below road map:

1) select which market you want to follow:

NASDAQ

http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download

NYSE

http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download

AMEX

http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download


2) Create a new google spreadsheet and name two sheets Blad1 and Panel. In the first cell of Blad1 you put the formula:

=IMPORTDATA("http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download")

you need to change the url to match your selection in 1).


3) In the first cell of Panel you put the name "Ticker" and then you copy all the ticker names from Blad1.

4) In the script editor you put in the below java script code:


function PanelCreation_Stock() 

{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Blad1");
var dstSheet = ss.getSheetByName("Panel");
var curDat = new Date();
var day1 = curDat.getDay();
if(day1 == 0 || day1 == 1)
{
return;
}
var lCol = dstSheet.getLastColumn();
var srcdate = dstSheet.getRange(1, 1, 1, lCol).getValues();

for(var k=1;k<=srcdate[0].length-1;k++)
{
if(Utilities.formatDate(srcdate[0][k],"GMT", "dd-MMM-yy") == Utilities.formatDate(curDat,"GMT", "dd-MMM-yy"))
{
return;
}
}
var snRows = sourceSheet.getLastRow();
var dnRows = dstSheet.getLastRow();

var srcStock = sourceSheet.getRange("A2:A" + snRows).getValues();
var srcLastSale = sourceSheet.getRange("C2:C" + snRows).getValues();

var dstStock = dstSheet.getRange("A2:A" + dnRows).getValues();
var dstLastSale = dstSheet.getRange("Z2:Z" + dnRows).getValues();

for(var j=0;j<dnRows-1;j++)
{
dstLastSale[j][0]="n/a";
}
var flag = "true";
var foundStock;
for(var i=0;i<snRows-1;i++) //snRows
{
var sStockVal = srcStock[i][0];

//var foundStock = ArrayLib.indexOf(dstStock,0, sStockVal);

flag="false";
for(var j=0;j<dnRows-1;j++)
{
if(dstStock[j][0].toString().toUpperCase() == srcStock[i][0].toString().toUpperCase())
{
flag = "true";
foundStock = j;
break;
}
}
if(flag=="true")
{
dstLastSale[foundStock][0] = srcLastSale[i][0];
}
else
{
var dnRows1 = dstSheet.getLastRow()+1;
dstSheet.getRange("A" + dnRows1).setValue(srcStock[i][0]);
dstSheet.getRange(dnRows1,lCol+1,1,1).setValue(srcLastSale[i][0]);
for(var k=2;k<=lCol;k++)
{
if(dstSheet.getRange(dnRows1, k).getValue()=="")
{
dstSheet.getRange(dnRows1, k).setValue("n/a");
}
}
}
}
dstSheet.getRange(1,lCol+1).setValue(curDat);
dstSheet.getRange(2, lCol+1, dstLastSale.length, 1).setValues(dstLastSale);
}

 
5) Set it to run each day at 12:00. The code will save the new last sale price for monday to friday with one days lag.

Now we can move on to Maple.


In Maple run the following code to load the data:

 

X := proc (Url) local theDLL, URLDownloadToFile, myDirectory, myFile, Destination, DL;

 

theDLL := "C:\\WINDOWS\\SYSTEM32\\urlmon.dll";

 

URLDownloadToFile := define_external('URLDownloadToFileA', pCaller::(integer[4]), szURL::string, szFileName::string, dwReserved::(integer[4]), lpfnCB::(integer[4]), 'RETURN'::(integer[4]), LIB = theDLL);

 

if FileTools[Exists]("C:\\mydir") = true then FileTools:-RemoveDirectory("C:\\mydir", recurse = true, forceremove = true) else end if;

 

FileTools:-MakeDirectory("C:\\mydir");
myDirectory := "C:\\mydir";
myFile := "data1.csv";
Destination := cat(myDirectory, "\\", myFile);

 

DL := proc () local M;

 

URLDownloadToFile(0, Url, Destination, 0, 0);
M := ImportMatrix("C:\\mydir\\data1.csv", delimiter = ",", datatype = string);
M := Matrix(M, datatype = anything)

 

end proc;

 

return DL()

 

end proc:

 

data := X("https://docs.google.com/spreadsheets/d/1L5-yUB0EWeBdJNMdELKBRmBQ1JJ0QymrtDLkVhHCVn8/pub?gid=649021574&single=true&output=csv");
L := LinearAlgebra:-Transpose(data);

If you use your own spreadsheet you need to change the url to match that spreadsheet.
Select File -> Publish to the web in google spreadsheet

We can now run the portfolio optimization in Maple:

with(Statistics):
with(ListTools):
with(LinearAlgebra):
with(Optimization):
with(plots):

 

Nr, Nc := ArrayTools:-Size(L):
symb := L[1 .. 1, 2 .. Nc]:
LL := L[2 .. Nr, 2 .. Nc]:
Nr, Nc := ArrayTools:-Size(LL):

 

# Removing stocks with missing observations
for i to Nc do if Occurrences("n/a", convert(Column(LL, i), list)) >= 1 then AA[i] := i else AA[i] := 0 end if
end do;

 

DD := RemoveInRange([seq(AA[i], i = 1 .. Nc)], 0 .. 1):
symbb := DeleteColumn(symb, DD):
LLL := map(parse, DeleteColumn(LL, DD)):
Nr, Nc := ArrayTools:-Size(LLL):

 

# Calculate Return
for j to Nc do
for i from 2 to Nr do

 

r[i, j] := (LLL[i, j]-LLL[i-1, j])/LLL[i-1, j]

 

end do
end do;

 

RR := Matrix([seq([seq(r[i, j], j = 1 .. Nc)], i = 2 .. Nr)], datatype = float[8]);
n, nstock := ArrayTools:-Size(RR):

 

# Portfolio Optimization
W := Vector(nstock, symbol = w):
y := Vector(n, fill = 2, datatype = float[8]):
s1 := Optimization[LSSolve]([y, RR])[2];
Nr, Nc := ArrayTools:-Size(s1):

 

j := 0:
for i to Nr do if s1[i] <> 0 then j := j+1; ss1[j] := symbb[1, i] = s1[i] end if end do;

 

Vector(j, proc (i) options operator, arrow; ss1[i] end proc);
LineChart(s1);

 

 

 


The PDE & BC project , a very nice and challenging one, also one where Maple is pioneer in all computer algebra systems, has restarted, including now also the collaboration of Katherina von Bülow.

Recapping, the PDE & BC project started 5 years ago implementing some of the basic methods found in textbooks to match arbitrary functions and constants to given PDE boundary conditions of different kinds. At this point we aim to fill gaps, and the first one we tackled is the case of 1st order PDE that can be solved without boundary conditions in terms of an arbitrary function, and where a single boundary condition (BC) is given for the PDE unknown function, and this BC does not depend on the independent variables of the problem. It looks simple ... It can be rather tricky though. The method we implemented is a simple however ingenious use of differential invariants  to match the boundary condition.


The resulting new code, the portion already tested, is available for download in the Maplesoft R&D webpage for Differential Equations and Mathematical Functions (the development itself is bundled within the library that contains the new developments for the Physics package, in turn within the zip linked in the webpage).


The examples that can now be handled, although restricted in generality to "only one 1st order linear or nonlinear PDE and only one boundary condition for the unknown function itself", illustrate well how powerful it can be to use more advanced methods to tackle these tricky situations where we need to match an arbitrary function to a boundary condition.


To illustrate the idea, consider first a linear example, among the simplest one could imagine:

PDEtools:-declare(f(x, y, z))

f(x, y, z)*`will now be displayed as`*f

(1)

pde := diff(f(x, y, z), x)+diff(f(x, y, z), y)+diff(f(x, y, z), z) = f(x, y, z)

diff(f(x, y, z), x)+diff(f(x, y, z), y)+diff(f(x, y, z), z) = f(x, y, z)

(2)

Input now a boundary condition (bc) for the unknownf(x, y, z) such that this bc does not depend on the independent variables {x, y, z}; this bc can however depend on arbitrary symbolic parameters, for instance

bc := f(alpha+beta, alpha-beta, 1) = alpha*beta

f(alpha+beta, alpha-beta, 1) = alpha*beta

(3)

With the recent development, this kind of problem can now be solved in one go:

sol := pdsolve([pde, bc])

f(x, y, z) = (1/4)*(x-2*z+2+y)*(x-y)*exp(z-1)

(4)

Nice! And how do you verify this result for correctness? With pdetest , which actually also tests the solution against the boundary conditions:

pdetest(sol, [pde, bc])

[0, 0]

(5)

And what has been done to obtain the solution (4)? First the PDE was solved regardless of the boundary condition, so in general, obtaining:

pdsolve(pde)

f(x, y, z) = _F1(-x+y, -x+z)*exp(x)

(6)

In a second step, the arbitrary function _F1(-x+y, -x+z) got determined such that the boundary condition f(alpha+beta, alpha-beta, 1) = alpha*beta is matched. Concretely, the mapping _F1 is what got determined. You can see this mapping reversing the solving process in two steps. Start taking the difference between the general solution (6) and the solution (4) that matches the boundary condition

(f(x, y, z) = _F1(-x+y, -x+z)*exp(x))-(f(x, y, z) = (1/4)*(x-2*z+2+y)*(x-y)*exp(z-1))

0 = _F1(-x+y, -x+z)*exp(x)-(1/4)*(x-2*z+2+y)*(x-y)*exp(z-1)

(7)

and isolate here _F1(-x+y, -x+z)

PDEtools:-Solve(0 = _F1(-x+y, -x+z)*exp(x)-(1/4)*(x-2*z+2+y)*(x-y)*exp(z-1), _F1(-x+y, -x+z))

_F1(-x+y, -x+z) = (1/4)*exp(-x+z-1)*(x^2-2*x*z-y^2+2*y*z+2*x-2*y)

(8)

So this is the value _F1(-x+y, -x+z) that got determined. To see now the actual solving mapping _F1, that takes for arguments -x+y and -x+z and returns the right-hand side of (8), one can perform a change of variables introducing the two parameters `&tau;__1` and `&tau;__2` of the _F1 mapping:

{tau__1 = -x+y, tau__2 = -x+z, tau__3 = z}

{tau__1 = -x+y, tau__2 = -x+z, tau__3 = z}

(9)

solve({tau__1 = -x+y, tau__2 = -x+z, tau__3 = z}, {x, y, z})

{x = -tau__2+tau__3, y = -tau__2+tau__1+tau__3, z = tau__3}

(10)

PDEtools:-dchange({x = -tau__2+tau__3, y = -tau__2+tau__1+tau__3, z = tau__3}, _F1(-x+y, -x+z) = (1/4)*exp(-x+z-1)*(x^2-2*x*z-y^2+2*y*z+2*x-2*y), proc (u) options operator, arrow; simplify(u, size) end proc)

_F1(tau__1, tau__2) = -(1/4)*exp(tau__2-1)*tau__1*(tau__1-2*tau__2+2)

(11)

So the solving mapping _F1 is

_F1 = unapply(rhs(_F1(tau__1, tau__2) = -(1/4)*exp(tau__2-1)*tau__1*(tau__1-2*tau__2+2)), tau__1, tau__2)

_F1 = (proc (tau__1, tau__2) options operator, arrow; -(1/4)*exp(tau__2-1)*tau__1*(tau__1-2*tau__2+2) end proc)

(12)

Wow! Although this pde & bc problem really look very simple, this solution (12) is highly non-obvious, as is the way to get it just from the boundary condition f(alpha+beta, alpha-beta, 1) = alpha*beta and the solution (6) too. Let's first verify that this mapping is correct (even when we know, by construction, that it is correct). For that, apply (12) to the arguments of the arbitrary function and we should obtain (8)

(_F1 = (proc (tau__1, tau__2) options operator, arrow; -(1/4)*exp(tau__2-1)*tau__1*(tau__1-2*tau__2+2) end proc))(-x+y, -x+z)

_F1(-x+y, -x+z) = -(1/4)*exp(-x+z-1)*(-x+y)*(x-2*z+2+y)

(13)

Indeed this is equal to (8)

normal((_F1(-x+y, -x+z) = -(1/4)*exp(-x+z-1)*(-x+y)*(x-2*z+2+y))-(_F1(-x+y, -x+z) = (1/4)*exp(-x+z-1)*(x^2-2*x*z-y^2+2*y*z+2*x-2*y)))

0 = 0

(14)

Skipping the technical details, the key observation to compute a solving mapping is that, given a 1st order PDE where the unknown depends on k independent variables, if the boundary condition depends on k-1 arbitrary symbolic parameters alpha, beta, one can always seek a "relationship between these k-1parameters and the k-1differential invariants that enter as arguments in the arbitrary function _F1 of the solution", and get the form of the mapping _F1 from this relationship and the bc. The method works in general. Change for instance the bc (3) making its right-hand side be a sum instead of a product

bc := f(alpha+beta, alpha-beta, 1) = alpha+beta

f(alpha+beta, alpha-beta, 1) = alpha+beta

(15)

sol := pdsolve([pde, bc])

f(x, y, z) = (x-z+1)*exp(z-1)

(16)

pdetest(sol, [pde, bc])

[0, 0]

(17)

An interesting case happens when the boundary condition depends on less than k-1 parameters, for instance:

bc__1 := subs(beta = alpha, bc)

f(2*alpha, 0, 1) = 2*alpha

(18)

sol__1 := pdsolve([pde, bc__1])

f(x, y, z) = ((x-z+1)*_C1+x-y)*exp(((z-1)*_C1+y)/(1+_C1))/(1+_C1)

(19)

As we see in this result, the additional difficulty represented by having few parameters got tackled by introducing an arbitrary constant _C1 (this is likely to evolve into something more general...)

pdetest(sol__1, [pde, bc__1])

[0, 0]

(20)

Finally, consider a nonlinear example

PDEtools:-declare(u(x, y))

u(x, y)*`will now be displayed as`*u

(21)

pde := 3*(u(x, y)-y)^2*(diff(u(x, y), x))-(diff(u(x, y), y)) = 0

3*(u(x, y)-y)^2*(diff(u(x, y), x))-(diff(u(x, y), y)) = 0

(22)

Here we have 2 independent variables, so for illustration purposes use a boundary condition that depends on only one arbitrary parameter

bc := u(0, alpha) = alpha

u(0, alpha) = alpha

(23)

All looks OK, but we still have another problem: check the arbitrary function _F1 entering the general solution of pde when tackled without any boundary condition:

pdsolve(pde)

u(x, y) = RootOf(-y^3+3*y^2*_Z-3*y*_Z^2+_Z^3-_F1(_Z)-x)

(24)

Remove this RootOf to see the underlying algebraic expression

DEtools[remove_RootOf](u(x, y) = RootOf(-y^3+3*y^2*_Z-3*y*_Z^2+_Z^3-_F1(_Z)-x))

-y^3+3*y^2*u(x, y)-3*y*u(x, y)^2+u(x, y)^3-_F1(u(x, y))-x = 0

(25)

So this is a pde where the general solution is implicit, actually depending on an arbitrary function of the unknown u(x, y) The code handles this problem in the same way, just that in cases like this there may be more than one solution. For this very particular bc (23) there are actually three solutions:

pdsolve([pde, bc])

u(x, y) = x^(1/3)+y, u(x, y) = -(1/2)*x^(1/3)-((1/2)*I)*3^(1/2)*x^(1/3)+y, u(x, y) = -(1/2)*x^(1/3)+((1/2)*I)*3^(1/2)*x^(1/3)+y

(26)

Verify these three solutions against the pde and the boundary condition

map(pdetest, [u(x, y) = x^(1/3)+y, u(x, y) = -(1/2)*x^(1/3)-((1/2)*I)*3^(1/2)*x^(1/3)+y, u(x, y) = -(1/2)*x^(1/3)+((1/2)*I)*3^(1/2)*x^(1/3)+y], [pde, bc])

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

(27)

:)


Download PDEs_and_Boundary_Conditions.mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft


In connection with recent developments for symbolic sequences, a number of improvements were implemented regarding symbolic differentiation, that is the computation of n^th order derivatives were n is a symbol, the simplest example being the n^th derivative of the exponential, which of course is the exponential itself. This post is about these developments, done in collaboration with Katherina von Bülow, and available for download as usual from the Maplesoft R&D web page for Differential Equations and Mathematical functions (the update itself is bundled with the official updates of the Maple Physics package).

 

It is important to note that Maple is pioneer in having an actual implementation of symbolic differentiation, something that works for real, since several releases.  The development, however, was somewhat stuck because we were unable to compute the symbolic n^th derivative of a composite function f(g(z)). A formula for this problem is actually known, it is the Faà di Bruno formula, but, in order to implement it, first we were missing the incomplete Bell functions , that got implemented in Maple 15, nice, but then we were still missing differentiating symbolic sequences, and functions whose arguments are symbolic sequences (i.e. the number of arguments of the function is n, a symbol, of unknown value at the time of differentiating). All this got implemented now within the new MathematicalFunctions:-Sequence package, opening the door widely to these improvements in n^th differentiation.

 

The symbolic differentiation code works as mostly all other computer algebra code, by mapping complicated problems into a composition of simpler problems all of which are tractable; what follows is then an illustration of these basic cases.

 

Among the simplest new case that can now be handled there is that of a power where the exponent is linear in the differentiation variable. This is actually an easy problem

(%diff = diff)(f^(alpha*z+beta), `$`(z, n))

%diff(f^(alpha*z+beta), `$`(z, n)) = alpha^n*f^(alpha*z+beta)*ln(f)^n

(1)

More complicated, consider the k^th power of a generic function; the corresponding symbolic derivative can be mapped into a sum of symbolic derivatives of powers of g(z) with lower degree

(%diff = diff)(g(z)^k, `$`(z, n))

%diff(g(z)^k, `$`(z, n)) = k*binomial(n-k, n)*(Sum((-1)^_k1*binomial(n, _k1)*g(z)^(k-_k1)*(Diff(g(z)^_k1, [`$`(z, n)]))/(k-_k1), _k1 = 0 .. n))

(2)

In some cases where g(z) is a known function, the computation can be carried on furthermore. For example, for g = ln the result can be expressed using Stirling numbers of the first kind

(%diff = diff)(ln(alpha*z+beta)^k, `$`(z, n))

%diff(ln(alpha*z+beta)^k, `$`(z, n)) = alpha^n*(Sum(pochhammer(k-_k1+1, _k1)*Stirling1(n, _k1)*ln(alpha*z+beta)^(k-_k1), _k1 = 0 .. n))/(alpha*z+beta)^n

(3)

The case of sin and cos are relatively simpler, but then assumptions on the exponent are required in order to proceed further ahead from (2), for example

`assuming`([(%diff = diff)(sin(alpha*z+beta)^k, `$`(z, n))], [k::posint])

%diff(sin(alpha*z+beta)^k, `$`(z, n)) = (-1)^k*piecewise(n = 0, (-sin(alpha*z+beta))^k, alpha^n*I^n*(Sum(binomial(k, _k1)*(2*_k1-k)^n*exp(I*(2*_k1-k)*(alpha*z+beta+(1/2)*Pi)), _k1 = 0 .. k))/2^k)

(4)

The case of functions of arbitrary number of variables (typical situation where symbolic sequences are required) is now handled properly. This is the pFq hypergeometric function of symbolic order p and q 

(%diff = diff)(hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[j], j = 1 .. q)], z), `$`(z, n))

%diff(hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[j], j = 1 .. q)], z), `$`(z, n)) = (product(pochhammer(a[i], n), i = 1 .. p))*hypergeom([`$`(a[i]+n, i = 1 .. p)], [`$`(b[j]+n, j = 1 .. q)], z)/(product(pochhammer(b[j], n), j = 1 .. q))

(5)

The case of the MeijerG function is more complicated, but in practice, for the computer, once it knows how to handle symbolic sequences, the more involved problem becomes computable

(%diff = diff)(MeijerG([[`$`(a[i], i = 1 .. n)], [`$`(b[i], i = n+1 .. p)]], [[`$`(b[i], i = 1 .. m)], [`$`(b[i], i = m+1 .. q)]], z), `$`(z, k))

%diff(MeijerG([[`$`(a[i], i = 1 .. n)], [`$`(b[i], i = n+1 .. p)]], [[`$`(b[i], i = 1 .. m)], [`$`(b[i], i = m+1 .. q)]], z), `$`(z, k)) = MeijerG([[-k, `$`(a[i]-k, i = 1 .. n)], [`$`(b[i]-k, i = n+1 .. p)]], [[`$`(b[i]-k, i = 1 .. m)], [0, `$`(b[i]-k, i = m+1 .. q)]], z)

(6)

Not only the mathematics of this result is correct: the object returned is actually computable to the end (if you provide the values of n, p, m and q), and the typesetting is actually fully readable, as in textbooks, including copy and paste working properly; all this is new.

The n^th derivative of a number of mathematical functions that were not implemented before, are now also implemented, covering the gaps, for example:

(%diff = diff)(BellB(a, z), `$`(z, n))

%diff(BellB(a, z), `$`(z, n)) = Sum(Stirling2(a, _k1)*pochhammer(_k1-n+1, n)*z^(_k1-n), _k1 = 0 .. a)

(7)

(%diff = diff)(bernoulli(z), `$`(z, n))

%diff(bernoulli(z), `$`(z, n)) = pochhammer(nu-n+1, n)*bernoulli(nu-n, z)

(8)

(%diff = diff)(binomial(z, m), `$`(z, n))

%diff(binomial(z, m), `$`(z, n)) = (Sum((-1)^(_k1+m)*Stirling1(m, _k1)*pochhammer(_k1-n+1, n)*(z-m+1)^(_k1-n), _k1 = 1 .. m))/factorial(m)

(9)

(%diff = diff)(euler(a, z), `$`(z, n))

%diff(euler(a, z), `$`(z, n)) = pochhammer(a-n+1, n)*euler(a-n, z)

(10)

In the same way the fundamental formulas for the n^th derivative of all the 12 elliptic Jacobi functions  as well as the four elliptic JacobiTheta functions,  the LambertW , LegendreP  and some others are now all implemented.

Finally there is the "holy grail" of this problem: the n^th derivative of a composite function f(g(z)) - this always-unreachable implementation of Faa di Bruno formula. We now have it :)

(%diff = diff)(f(g(z)), `$`(z, n))

%diff(f(g(z)), `$`(z, n)) = Sum(((D@@k)(f))(g(z))*IncompleteBellB(n, k, `$`(diff(g(z), [`$`(z, j)]), j = 1 .. n-k+1)), k = 0 .. n)

(11)

Note the symbolic sequence of symbolic order derivatives of lower degree, both of of f and g, also within the arguments of the IncompleteBellB function. This is a very abstract formula ... And does this really work? Of course it does :). Consider, for instance, a case where the n^th derivatives of f(z) and g(z) can both be computed by the system:

sin(cos(alpha*z+beta))

sin(cos(alpha*z+beta))

(12)

This is the n^th derivative expressed using Faa di Bruno's formula, in turn expressed using symbolic sequences within the IncompleteBellB  function

(%diff = diff)(sin(cos(alpha*z+beta)), `$`(z, n))

%diff(sin(cos(alpha*z+beta)), `$`(z, n)) = Sum(sin(cos(alpha*z+beta)+(1/2)*k*Pi)*IncompleteBellB(n, k, `$`(cos(alpha*z+beta+(1/2)*j*Pi)*alpha^j, j = 1 .. n-k+1)), k = 0 .. n)

(13)

These results can all be verified. Take for instance n = 3

eval(%diff(sin(cos(alpha*z+beta)), `$`(z, n)) = Sum(sin(cos(alpha*z+beta)+(1/2)*k*Pi)*IncompleteBellB(n, k, `$`(cos(alpha*z+beta+(1/2)*j*Pi)*alpha^j, j = 1 .. n-k+1)), k = 0 .. n), n = 3)

%diff(sin(cos(alpha*z+beta)), z, z, z) = Sum(sin(cos(alpha*z+beta)+(1/2)*k*Pi)*IncompleteBellB(3, k, `$`(cos(alpha*z+beta+(1/2)*j*Pi)*alpha^j, j = 1 .. 4-k)), k = 0 .. 3)

(14)

Compute now the inert functions: on the left-hand side this is just the (now explicit) 3rd order derivative, while on the right-hand side we have a sum of IncompleteBellB  functions, where the number of arguments, expressed in (13) using symbolic sequences that depend on the summation index k and the differentiation order n, now in (14) depend only on k, and get transformed into explicit sequences of arguments when the summation is performed and k assumes integer values

value(%diff(sin(cos(alpha*z+beta)), z, z, z) = Sum(sin(cos(alpha*z+beta)+(1/2)*k*Pi)*IncompleteBellB(3, k, `$`(cos(alpha*z+beta+(1/2)*j*Pi)*alpha^j, j = 1 .. 4-k)), k = 0 .. 3))

alpha^3*sin(alpha*z+beta)*cos(cos(alpha*z+beta))-3*alpha^3*cos(alpha*z+beta)*sin(alpha*z+beta)*sin(cos(alpha*z+beta))+alpha^3*sin(alpha*z+beta)^3*cos(cos(alpha*z+beta)) = alpha^3*sin(alpha*z+beta)*cos(cos(alpha*z+beta))-3*alpha^3*cos(alpha*z+beta)*sin(alpha*z+beta)*sin(cos(alpha*z+beta))+alpha^3*sin(alpha*z+beta)^3*cos(cos(alpha*z+beta))

(15)

Take left-hand side minus right-hand side

simplify((lhs-rhs)(alpha^3*sin(alpha*z+beta)*cos(cos(alpha*z+beta))-3*alpha^3*cos(alpha*z+beta)*sin(alpha*z+beta)*sin(cos(alpha*z+beta))+alpha^3*sin(alpha*z+beta)^3*cos(cos(alpha*z+beta)) = alpha^3*sin(alpha*z+beta)*cos(cos(alpha*z+beta))-3*alpha^3*cos(alpha*z+beta)*sin(alpha*z+beta)*sin(cos(alpha*z+beta))+alpha^3*sin(alpha*z+beta)^3*cos(cos(alpha*z+beta))))

0

(16)

NULL

:)


Download SymbolicOrderDifferentiation.mw


Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. This problem mentioned in  page of tasks still without  Maple implementation. 

The post presents the implementation of this task in Maple. Required parameter of the procedure (named  KnightTour)  is  address  - the address of the initial square in the algebraic notation. The second parameter  opt  is optional parameter:

1) if  opt is sequence  (by default) then  the procedure returns the sequence of moves of the knight in the usual algebraic notation,

2)  if  opt is diagram   then  the procedure returns the plot of moves of the knight and  sequentially numbers all the visited squares,

3) if  opt is animation  then  the procedure returns an animation of moves of the knight.

In the procedure is used a solution with maximum symmetry by George Jelliss, http://www.mayhematics.com/t/8f.htm

 

Code of the procedure:

KnightTour := proc(address::symbol, opt::symbol := sequence)

local L, n, L1, k, i, j, squares, border, chessboard, letters, digits, L2, L3, Tour, T, F;

uses ListTools, plottools, plots;

L := [a1, b3, d2, c4, a5, b7, d8, e6, d4, b5, c7, a8, b6, c8, a7, c6, b8, a6, b4, d5, e3, d1, b2, a4, c5, d7, f8, h7, f6, g8, h6, f7, h8, g6, e7, f5, h4, g2, e1, d3, e5, g4, f2, h1, g3, f1, h2, f3, g1, h3, g5, e4, d6, e8, g7, h5, f4, e2, c1, a2, c3, b1, a3, c2];

n := Search(address, L);

L1 := [L[n .. 64][], L[1 .. n-1][]];

if opt = sequence then return L1[] fi;

k := 0;

for i to 8 do

for j from `if`(type(i, odd), 1, 2) by 2 to 8 do

k := k+1;

squares[k] := polygon([[i-1/2, j-1/2], [i-1/2, j+1/2], [i+1/2, j+1/2], [i+1/2, j-1/2]], color = grey);

od;  od;

squares := convert(squares, list);

border := curve([[1/2, 1/2], [1/2, 17/2], [17/2, 17/2], [17/2, 1/2], [1/2, 1/2]], color = black, thickness = 4);

chessboard := display(squares, border);

letters := [a, b, c, d, e, f, g, h];

digits := [$ 1 .. 8];

L2 := convert~(L1, string);

L3 := subs(letters=~digits, map(t->[parse(t[1]), parse(t[2])], L2));

Tour := curve(L3, color = red, thickness = 3);

T := textplot([seq([op(L3[i]), i], i = 1 .. 64)], align = above, font = [times, bold, 16]);

if opt = diagram then return display(chessboard, Tour, T, axes = none) fi;

F := seq(display(chessboard, curve(L3[1 .. s], color = red, thickness = 3), textplot([seq([op(L3[i]), i], i = 1 .. s)], align = above, font = [times, bold, 16])), s = 1 .. 64);

display(seq(F[i]$5, i = 1 .. 64), insequence = true, axes = none);

end proc:

 

 Examples of use:

KnightTour(f3);

KnightTour(f3, diagram);

 

 

KnightTour(f3, animation);

                                 

 

 

 KnightTour.mw

Dear friends,

some time ago I shared a story here on the use of Maple to compute the cycle index of the induced action on the edges of an ordinary graph of the symmetric group permuting the vertices and the use of the Polya Enumeration Theorem to count non-isomorphic graphs by the number of edges. It can be found at the following Mapleprimes link.

I am writing today to alert you to another simple Maple program that is closely related and demonstrates Maple's capability to implement concepts from group theory and Polya enumeration. This link at Math.Stackexchange.com shows how to use the cycle index of the induced action by the symmetric group permuting vertices on the edges of a multigraph that includes loops to count set partitions of multisets containing two instances of n distinct types of items. The sequence that corresponds to these set partitions is OEIS A020555 where it is pointed out that we can equivalently count multigraphs with n labeled i.e. distinct edges where the vertices of the graph represent the multisets of the multiset partition and are connected by an edge k if the two instances of the value k are included in the sets represented by the two vertices that constitute the edge. The problem then reduces to a simple substitution into the aforementioned cycle index of a polynomial representing the set of labels on an edge including no labels on an edge that is not included.

This computation presents a remarkable simplicity while also implementing a non-trivial application of Polya counting. It is hoped that MaplePrimes users will enjoy reading this program, possibly profit from some of the techniques employed and be motivated to use Maple in their work on combinatorics problems.

Best regards,

Marko Riedel

 

Symbolic sequences enter in various formulations in mathematics. This post is about a related new subpackage, Sequences, within the MathematicalFunctions package, available for download in Maplesoft's R&D page for Mathematical Functions and Differential Equations (currently bundled with updates to the Physics package).

 

Perhaps the most typical cases of symbolic sequences are:

 

1) A sequence of numbers - say from n to m - frequently displayed as

n, `...`, m

 

2) A sequence of one object, say a, repeated say p times, frequently displayed as

 "((a,`...`,a))"

3) A more general sequence, as in 1), but of different objects and not necessarily numbers, frequently displayed as

a[n], `...`, a[m]

or likewise a sequence of functions

f(n), `...`, f(m)

In all these cases, of course, none of n, m, or p are known: they are just symbols, or algebraic expressions, representing integer values.

 

These most typical cases of symbolic sequences have been implemented in Maple since day 1 using the `$` operator. Cases 1), 2) and 3) above are respectively entered as `$`(n .. m), `$`(a, p), and `$`(a[i], i = n .. m) or "`$`(f(i), i = n .. m)." To have computer algebra representations for all these symbolic sequences is something wonderful, I would say unique in Maple.

Until recently, however, the typesetting of these symbolic sequences was frankly poor, input like `$`(a[i], i = n .. m) or ``$\``(a, p) just being echoed in the display. More relevant: too little could be done with these objects; the rest of Maple didn't know how to add, multiply, differentiate or map an operation over the elements of the sequence, nor for instance count the sequence's number of elements.

 

All this has now been implemented.  What follows is a brief illustration.

restart

First of all, now these three types of sequences have textbook-like typesetting:

`$`(n .. m)

`$`(n .. m)

(1)

`$`(a, p)

`$`(a, p)

(2)

For the above, a$p works the same way

`$`(a[i], i = n .. m)

`$`(a[i], i = n .. m)

(3)

Moreover, this now permits textbook display of mathematical functions that depend on sequences of paramateters, for example:

hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[i], i = 1 .. q)], z)

hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[i], i = 1 .. q)], z)

(4)

IncompleteBellB(n, k, `$`(factorial(j), j = 1 .. n-k+1))

IncompleteBellB(n, k, `$`(factorial(j), j = 1 .. n-k+1))

(5)

More interestingly, these new developments now permit differentiating these functions even when their arguments are symbolic sequences, and displaying the result as in textbooks, with copy and paste working properly, for instance

(%diff = diff)(hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[i], i = 1 .. q)], z), z)

%diff(hypergeom([`$`(a[i], i = 1 .. p)], [`$`(b[i], i = 1 .. q)], z), z) = (product(a[i], i = 1 .. p))*hypergeom([`$`(a[i]+1, i = 1 .. p)], [`$`(b[i]+1, i = 1 .. q)], z)/(product(b[i], i = 1 .. q))

(6)

It is very interesting how much this enhances the representation capabilities; to mention but one, this makes 100% possible the implementation of the Faa-di-Bruno  formula for the nth symbolic derivative of composite functions (more on this in a post to follow this one).

But the bread-and-butter first: the new package for handling sequences is

with(MathematicalFunctions:-Sequences)

[Add, Differentiate, Map, Multiply, Nops]

(7)

The five commands that got loaded do what their name tells. Consider for instance the first kind of sequences mentione above, i.e

`$`(n .. m)

`$`(n .. m)

(8)

Check what is behind this nice typesetting

lprint(`$`(n .. m))

`$`(n .. m)

 

All OK. How many operands (an abstract version of Maple's nops  command):

Nops(`$`(n .. m))

m-n+1

(9)

That was easy, ok. Add the sequence

Add(`$`(n .. m))

(1/2)*(m-n+1)*(n+m)

(10)

Multiply the sequence

Multiply(`$`(n .. m))

factorial(m)/factorial(n-1)

(11)

Map an operation over the elements of the sequence

Map(f, `$`(n .. m))

`$`(f(j), j = n .. m)

(12)

lprint(`$`(f(j), j = n .. m))

`$`(f(j), j = n .. m)

 

Map works as map, i.e. you can map extra arguments as well

MathematicalFunctions:-Sequences:-Map(Int, `$`(n .. m), x)

`$`(Int(j, x), j = n .. m)

(13)

All this works the same way with symbolic sequences of forms "((a,`...`,a))" , and a[n], `...`, a[m]. For example:

`$`(a, p)

`$`(a, p)

(14)

lprint(`$`(a, p))

`$`(a, p)

 

MathematicalFunctions:-Sequences:-Nops(`$`(a, p))

p

(15)

Add(`$`(a, p))

a*p

(16)

Multiply(`$`(a, p))

a^p

(17)

Differentation also works

Differentiate(`$`(a, p), a)

`$`(1, p)

(18)

MathematicalFunctions:-Sequences:-Map(f, `$`(a, p))

`$`(f(a), p)

(19)

MathematicalFunctions:-Sequences:-Differentiate(`$`(f(a), p), a)

`$`(diff(f(a), a), p)

(20)

For a symbolic sequence of type 3)

`$`(a[i], i = n .. m)

`$`(a[i], i = n .. m)

(21)

MathematicalFunctions:-Sequences:-Nops(`$`(a[i], i = n .. m))

m-n+1

(22)

Add(`$`(a[i], i = n .. m))

sum(a[i], i = n .. m)

(23)

Multiply(`$`(a[i], i = n .. m))

product(a[i], i = n .. m)

(24)

The following is nontrivial: differentiating the sequence a[n], `...`, a[m], with respect to a[k] should return 1 when n = k (i.e the running index has the value k), and 0 otherwise, and the same regarding m and k. That is how it works now:

Differentiate(`$`(a[i], i = n .. m), a[k])

`$`(piecewise(k = i, 1, 0), i = n .. m)

(25)

lprint(`$`(piecewise(k = i, 1, 0), i = n .. m))

`$`(piecewise(k = i, 1, 0), i = n .. m)

 

MathematicalFunctions:-Sequences:-Map(f, `$`(a[i], i = n .. m))

`$`(f(a[i]), i = n .. m)

(26)

Differentiate(`$`(f(a[i]), i = n .. m), a[k])

`$`((diff(f(a[i]), a[i]))*piecewise(k = i, 1, 0), i = n .. m)

(27)

lprint(`$`((diff(f(a[i]), a[i]))*piecewise(k = i, 1, 0), i = n .. m))

`$`((diff(f(a[i]), a[i]))*piecewise(k = i, 1, 0), i = n .. m)

 

 

And that is it. Summarizing: in addition to the former implementation of symbolic sequences, we now have textbook-like typesetting for them, and more important: Add, Multiply, Differentiate, Map and Nops. :)

 

The first large application we have been working on taking advantage of this is symbolic differentiation, with very nice results; I will see to summarize them in a post to follow in a couple of days.

 

Download MathematicalFunctionsSequences.mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

       Calculation of RSCR mechanism as a  solution to underdetermined system of nonlinear equations.  

 

RSCR.mw 

https://vk.com/doc242471809_376439263
https://vk.com/doc242471809_408704758

RCCC mechanism
https://vk.com/doc242471809_375452868

Solving DAEs in Maple

As I had mentioned in many posts, Maple cannot solve nonlinear DAEs. As of today (Maple 2015), given a system of index 1 DAE dy/dt = f(y,z); 0 = g(y,z), Maple “extends” g(y,z) to get dz/dt = g1(y,z). So, any given index 1 DAE is converted to a system of ODEs dy/dt = f, dz/dt = g1 with the constraint g = 0, before it solves. This is true for all the solvers in Maple despite the wrong claims in the help files. It is also true for MEBDFI, the FORTRAN implementation of which actually solves index 2 DAEs directly. In addition, the initial condition for the algebraic variable has to be consistent. The problem with using fsolve is that one cannot specify tolerance. Often times one has to solve DAEs at lower tolerances (1e-3 or 1e-4), not 1e-15. In addition, one cannot use compile =true for high efficiency.

The approach in Maple fails for many DAEs of practical interest. In addition, this can give wrong answers. For example, dy/dt = z, y^2+z^2-1=0, with y(0)=0 and z(0)=1 has a meaningful solution only from 0 to Pi/2, Maple’s dsolve/numeric will convert this to dy/dt = z and dz/dt = -y and integrate in time for ever. This is wrong as at t = Pi/2, the system becomes index 2 DAE and there is more than 1 acceptable solution.

We just recently got our paper accepted that helps Maple's dsolve and many ODE solvers in other languages handle DAEs directly. The approach is rather simple, the index 1 DAE is perturbed as dy/dt = f.S ; -mu.diff(g,t) = g. The switch function is a tanh function which is zero till t = tinit (initialization time). Mu is chosen to be a small number. In addition, lhs of the perturbed equation is simplified using approximate initial guesses as Maple cannot handle non-constant Mass matrix. The paper linked below gives below more details.

http://depts.washington.edu/maple/pubs/U_Apprach_FULL_DRAFT.pdf  

Next, I discuss different examples (code attached).

Example 1: Simple DAE (one ODE and one AE), the proposed approach helps solve this system efficiently without knowing the exact initial condition.

Hint: The code is given with a semicolon at the end of the most of the statements for educational purposes for new users. Using semicolon after dsolve numeric in classic worksheet crashes the code (Maplesoft folks couldn’t fix this despite my request).

Example 2:

This is a nickel battery model (one ODE and one AE). This fails with many solvers unless exact IC is given. The proposed approach works well. In particular, stiff=true, implicit=true option is found to be very efficient. The code given in example 1 can be used to solve example 2 or any other DAEs by just entering ODEs, AEs, ICs in proper places.

Example 3:

This is a nonlinear implicit ODE (posted in Mapleprimes earlier by joha, (http://www.mapleprimes.com/questions/203096-Solving-Nonlinear-ODE#answer211682 ). This can be converted to index 1 DAE and solved using the proposed approach.

Example 4:

This example was posted earlier by me in Mapleprimes (http://www.mapleprimes.com/posts/149877-ODEs-PDEs-And-Special-Functions) . Don’t try to solve this in Maple using its dsolve numeric solver for N greater than 5 directly. The proposed approach can handle this well. Tuning the perturbation parameters and using compile =true will help in solving this system more efficiently.

Finally example 1 is solved for different perturbation parameters to show how one can arrive at convergence. Perhaps more sophisticated users and Maplesoft folks can enable this as automatically tuned parameters in dsolve/numeric.

Note:

This post should not be viewed as just being critical on Maple. I have wasted a lot of time assuming that Maple does what it claims in its help file. This post is to bring awareness about the difficulty in dealing with DAEs. It is perfectly fine to not have a DAE solver, but when literature or standard methods are cited/claimed, they should be implemented in proper form. I will forever remain a loyal Maple user as it has enabled me to learn different topics efficiently. Note that Maplesim can solve DAEs, but it is a pain to create a Maplesim model/project just for solving a DAE. Also, events is a pain with Maplesim. The reference to Mapleprimes links are missing in the article, but will be added before the final printing stage. The ability of Maple to find analytical Jacobian helps in developing many robust ODE and DAE solvers and I hope to post my own approaches that will solve more complicated systems.

I strongly encourage testing of the proposed approach and implementation for various educational/research purposes by various users. The proposed approach enables solving lithium-ion and other battery/electrochemical storage models accurately in a robust manner. A disclosure has been filed with the University of Washington to apply for a provisional patent for battery models and Battery Management System for transportation, storage and other applications because of the current commercial interest in this topic (for batteries). In particular, use of this single step avoids intialization issues/(no need to initialize separately) for parameter estimation, state estimation or optimal control of battery models.

 

Appendix A

Maple code for Examples 1-4 from "Extending Explicit and Linealry Implicit ODE Solvers for Index-1 DAEs", M. T. Lawder,

V. Ramadesigan, B. Suthar and V. R. Subramanian, Computers and Chemical Engineering, in press (2015).

Use y1, y2, etc. for all differential variables and z1, z2, etc. for all algebraic variables

 

Example 1

restart;

with(plots):

Enter all ODEs in eqode

eqode:=[diff(y1(t),t)=-y1(t)^2+z1(t)];

eqode := [diff(y1(t), t) = -y1(t)^2+z1(t)]

(1)

Enter all AEs in eqae

eqae:=[cos(y1(t))-z1(t)^0.5=0];

eqae := [cos(y1(t))-z1(t)^.5 = 0]

(2)

Enter all initial conditions for differential variables in icodes

icodes:=[y1(0)=0.25];

icodes := [y1(0) = .25]

(3)

Enter all intial conditions for algebraic variables in icaes

icaes:=[z1(0)=0.8];

icaes := [z1(0) = .8]

(4)

Enter parameters for perturbation value (mu), switch function (q and tint), and runtime (tf)

pars:=[mu=0.1,q=1000,tint=1,tf=5];

pars := [mu = .1, q = 1000, tint = 1, tf = 5]

(5)

Choose solving method (1 for explicit, 2 for implicit)

Xexplicit:=2;

Xexplicit := 2

(6)

Standard solver requires IC z(0)=0.938791 or else it will fail

solx:=dsolve({eqode[1],eqae[1],icodes[1],icaes[1]},numeric):

Error, (in dsolve/numeric/DAE/checkconstraints) the initial conditions do not satisfy the algebraic constraints
  error = .745e-1, tolerance = .559e-6, constraint = cos(y1(t))-z1(t)^.5000000000000000000000

 

ff:=subs(pars,1/2+1/2*tanh(q*(t-tint)));

ff := 1/2+(1/2)*tanh(1000*t-1000)

(7)

NODE:=nops(eqode);NAE:=nops(eqae);

NODE := 1

NAE := 1

(8)

for XX from 1 to NODE do
EQODE||XX:=lhs(eqode[XX])=rhs(eqode[XX])*ff;
end do;

EQODE1 := diff(y1(t), t) = (-y1(t)^2+z1(t))*(1/2+(1/2)*tanh(1000*t-1000))

(9)

for XX from 1 to NAE do
EQAE||XX:=subs(pars,-mu*(diff(rhs(eqae[XX])-lhs(eqae[XX]),t))=rhs(eqae[XX])-lhs(eqae[XX]));
end do;

EQAE1 := -.1*sin(y1(t))*(diff(y1(t), t))-0.5e-1*(diff(z1(t), t))/z1(t)^.5 = -cos(y1(t))+z1(t)^.5

(10)

 

Dvars1:={seq(diff(z||x(t),t)=D||x,x=1..NAE)};

Dvars1 := {diff(z1(t), t) = D1}

(11)

Dvars2:={seq(rhs(Dvars1[x])=lhs(Dvars1[x]),x=1..NAE)};

Dvars2 := {D1 = diff(z1(t), t)}

(12)

icsn:=seq(subs(y||x(0)=y||x(t),icodes[x]),x=1..NODE),seq(subs(z||x(0)=z||x(t),icaes[x]),x=1..NAE);

icsn := y1(t) = .25, z1(t) = .8

(13)

for j from 1 to NAE do

EQAEX||j:=subs(Dvars1,eqode,icsn,Dvars2,lhs(EQAE||j))=rhs(EQAE||j);

end do:

Sys:={seq(EQODE||x,x=1..NODE),seq(EQAEX||x,x=1..NAE),seq(icodes[x],x=1..NODE),seq(icaes[x],x=1..NAE)};

Sys := {-0.1824604200e-1-0.5590169945e-1*(diff(z1(t), t)) = -cos(y1(t))+z1(t)^.5, y1(0) = .25, z1(0) = .8, diff(y1(t), t) = (-y1(t)^2+z1(t))*(1/2+(1/2)*tanh(1000*t-1000))}

(14)

if Xexplicit=1 then

sol:=dsolve(Sys,numeric):

else

sol:=dsolve(Sys,numeric,stiff=true,implicit=true):
end if:

 

for XX from 1 to NODE do
a||XX:=odeplot(sol,[t,y||XX(t)],0..subs(pars,tf),color=red);
end do:

for XX from NODE+1 to NODE+NAE do
a||XX:=odeplot(sol,[t,z||(XX-NODE)(t)],0..subs(pars,tf),color=blue);
end do:

display(seq(a||x,x=1..NODE+NAE),axes=boxed);

 

End Example 1

 

Example 2

restart;

with(plots):

eq1:=diff(y1(t),t)=j1*W/F/rho/V;

eq1 := diff(y1(t), t) = j1*W/(F*rho*V)

(15)

eq2:=j1+j2=iapp;

eq2 := j1+j2 = iapp

(16)

j1:=io1*(2*(1-y1(t))*exp((0.5*F/R/T)*(z1(t)-phi1))-2*y1(t)*exp((-0.5*F/R/T)*(z1(t)-phi1)));

j1 := io1*(2*(1-y1(t))*exp(.5*F*(z1(t)-phi1)/(R*T))-2*y1(t)*exp(-.5*F*(z1(t)-phi1)/(R*T)))

(17)

j2:=io2*(exp((F/R/T)*(z1(t)-phi2))-exp((-F/R/T)*(z1(t)-phi2)));

j2 := io2*(exp(F*(z1(t)-phi2)/(R*T))-exp(-F*(z1(t)-phi2)/(R*T)))

(18)

params:={F=96487,R=8.314,T=298.15,phi1=0.420,phi2=0.303,W=92.7,V=1e-5,io1=1e-4,io2=1e-10,iapp=1e-5,rho=3.4};

params := {F = 96487, R = 8.314, T = 298.15, V = 0.1e-4, W = 92.7, io1 = 0.1e-3, io2 = 0.1e-9, rho = 3.4, iapp = 0.1e-4, phi1 = .420, phi2 = .303}

(19)

eqode:=[subs(params,eq1)];

eqode := [diff(y1(t), t) = 0.5651477584e-2*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)-0.5651477584e-2*y1(t)*exp(-19.46229155*z1(t)+8.174162450)]

(20)

eqae:=[subs(params,eq2)];

eqae := [0.2e-3*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)-0.2e-3*y1(t)*exp(-19.46229155*z1(t)+8.174162450)+0.1e-9*exp(38.92458310*z1(t)-11.79414868)-0.1e-9*exp(-38.92458310*z1(t)+11.79414868) = 0.1e-4]

(21)

icodes:=[y1(0)=0.05];

icodes := [y1(0) = 0.5e-1]

(22)

icaes:=[z1(0)=0.7];

icaes := [z1(0) = .7]

(23)

solx:=dsolve({eqode[1],eqae[1],icodes[1],icaes[1]},type=numeric):

Error, (in dsolve/numeric/DAE/checkconstraints) the initial conditions do not satisfy the algebraic constraints
  error = .447e9, tolerance = .880e4, constraint = -2000000*(-1+y1(t))*exp(19.46229155000000000000*z1(t)-8.174162450000000000000)-2000000*y1(t)*exp(-19.46229155000000000000*z1(t)+8.174162450000000000000)+exp(38.92458310000000000000*z1(t)-11.79414868000000000000)-exp(-38.92458310000000000000*z1(t)+11.79414868000000000000)-100000

 

pars:=[mu=0.00001,q=1000,tint=1,tf=5001];

pars := [mu = 0.1e-4, q = 1000, tint = 1, tf = 5001]

(24)

Xexplicit:=2;

Xexplicit := 2

(25)

ff:=subs(pars,1/2+1/2*tanh(q*(t-tint)));

ff := 1/2+(1/2)*tanh(1000*t-1000)

(26)

NODE:=nops(eqode):NAE:=nops(eqae);

NAE := 1

(27)

for XX from 1 to NODE do
EQODE||XX:=lhs(eqode[XX])=rhs(eqode[XX])*ff:
end do;

EQODE1 := diff(y1(t), t) = (0.5651477584e-2*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)-0.5651477584e-2*y1(t)*exp(-19.46229155*z1(t)+8.174162450))*(1/2+(1/2)*tanh(1000*t-1000))

(28)

for XX from 1 to NAE do
EQAE||XX:=subs(pars,-mu*(diff(rhs(eqae[XX])-lhs(eqae[XX]),t))=rhs(eqae[XX])-lhs(eqae[XX]));
end do;

EQAE1 := -0.2e-8*(diff(y1(t), t))*exp(19.46229155*z1(t)-8.174162450)+0.3892458310e-7*(1-y1(t))*(diff(z1(t), t))*exp(19.46229155*z1(t)-8.174162450)-0.2e-8*(diff(y1(t), t))*exp(-19.46229155*z1(t)+8.174162450)+0.3892458310e-7*y1(t)*(diff(z1(t), t))*exp(-19.46229155*z1(t)+8.174162450)+0.3892458310e-13*(diff(z1(t), t))*exp(38.92458310*z1(t)-11.79414868)+0.3892458310e-13*(diff(z1(t), t))*exp(-38.92458310*z1(t)+11.79414868) = 0.1e-4-0.2e-3*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)+0.2e-3*y1(t)*exp(-19.46229155*z1(t)+8.174162450)-0.1e-9*exp(38.92458310*z1(t)-11.79414868)+0.1e-9*exp(-38.92458310*z1(t)+11.79414868)

(29)

Dvars1:={seq(diff(z||x(t),t)=D||x,x=1..NAE)};

Dvars1 := {diff(z1(t), t) = D1}

(30)

Dvars2:={seq(rhs(Dvars1[x])=lhs(Dvars1[x]),x=1..NAE)};

Dvars2 := {D1 = diff(z1(t), t)}

(31)

icsn:=seq(subs(y||x(0)=y||x(t),icodes[x]),x=1..NODE),seq(subs(z||x(0)=z||x(t),icaes[x]),x=1..NAE);

icsn := y1(t) = 0.5e-1, z1(t) = .7

(32)

for j from 1 to NAE do

EQAEX||j:=subs(Dvars1,eqode,icsn,Dvars2,lhs(EQAE||j))=rhs(EQAE||j);

end do;

EQAEX1 := -0.2e-8*(0.5368903705e-2*exp(5.449441630)-0.2825738792e-3*exp(-5.449441630))*exp(5.449441630)+0.3697835394e-7*(diff(z1(t), t))*exp(5.449441630)-0.2e-8*(0.5368903705e-2*exp(5.449441630)-0.2825738792e-3*exp(-5.449441630))*exp(-5.449441630)+0.1946229155e-8*(diff(z1(t), t))*exp(-5.449441630)+0.3892458310e-13*(diff(z1(t), t))*exp(15.45305949)+0.3892458310e-13*(diff(z1(t), t))*exp(-15.45305949) = 0.1e-4-0.2e-3*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)+0.2e-3*y1(t)*exp(-19.46229155*z1(t)+8.174162450)-0.1e-9*exp(38.92458310*z1(t)-11.79414868)+0.1e-9*exp(-38.92458310*z1(t)+11.79414868)

(33)

Sys:={seq(EQODE||x,x=1..NODE),seq(EQAEX||x,x=1..NAE),seq(icodes[x],x=1..NODE),seq(icaes[x],x=1..NAE)};

Sys := {-0.5810962488e-6+0.8802389238e-5*(diff(z1(t), t)) = 0.1e-4-0.2e-3*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)+0.2e-3*y1(t)*exp(-19.46229155*z1(t)+8.174162450)-0.1e-9*exp(38.92458310*z1(t)-11.79414868)+0.1e-9*exp(-38.92458310*z1(t)+11.79414868), y1(0) = 0.5e-1, z1(0) = .7, diff(y1(t), t) = (0.5651477584e-2*(1-y1(t))*exp(19.46229155*z1(t)-8.174162450)-0.5651477584e-2*y1(t)*exp(-19.46229155*z1(t)+8.174162450))*(1/2+(1/2)*tanh(1000*t-1000))}

(34)

if Xexplicit=1 then

sol:=dsolve(Sys,numeric,maxfun=0):

else

sol:=dsolve(Sys,numeric,stiff=true,implicit=true,maxfun=0):

end if:

 

for XX from 1 to NODE do
a||XX:=odeplot(sol,[t,y||XX(t)],0..subs(pars,tf),color=red);
end do:

for XX from NODE+1 to NODE+NAE do
a||XX:=odeplot(sol,[t,z||(XX-NODE)(t)],0..subs(pars,tf),color=blue);
end do:

b1:=odeplot(sol,[t,y1(t)],0..1,color=red):
b2:=odeplot(sol,[t,z1(t)],0..1,color=blue):

display(b1,b2,axes=boxed);

 

display(seq(a||x,x=1..NODE+NAE),axes=boxed);

 

End Example 2

 

Example 3

restart;

with(plots):

eq1:=diff(y1(t),t)^2+diff(y1(t),t)*(y1(t)+1)+y1(t)=cos(diff(y1(t),t));

eq1 := (diff(y1(t), t))^2+(diff(y1(t), t))*(y1(t)+1)+y1(t) = cos(diff(y1(t), t))

(35)

solx:=dsolve({eq1,y1(0)=0},numeric):

Error, (in dsolve/numeric/make_proc) Could not convert to an explicit first order system due to 'RootOf'

 

eqode:=[diff(y1(t),t)=z1(t)];

eqode := [diff(y1(t), t) = z1(t)]

(36)

eqae:=[subs(eqode,eq1)];

eqae := [z1(t)^2+z1(t)*(y1(t)+1)+y1(t) = cos(z1(t))]

(37)

icodes:=[y1(0)=0.0];

icodes := [y1(0) = 0.]

(38)

icaes:=[z1(0)=0.0];

icaes := [z1(0) = 0.]

(39)

pars:=[mu=0.1,q=1000,tint=1,tf=4];

pars := [mu = .1, q = 1000, tint = 1, tf = 4]

(40)

Xexplicit:=2;

Xexplicit := 2

(41)

ff:=subs(pars,1/2+1/2*tanh(q*(t-tint)));

ff := 1/2+(1/2)*tanh(1000*t-1000)

(42)

NODE:=nops(eqode);NAE:=nops(eqae);

NODE := 1

NAE := 1

(43)

for XX from 1 to NODE do
EQODE||XX:=lhs(eqode[XX])=rhs(eqode[XX])*ff:
end do;

EQODE1 := diff(y1(t), t) = z1(t)*(1/2+(1/2)*tanh(1000*t-1000))

(44)

for XX from 1 to NAE do
EQAE||XX:=subs(pars,-mu*(diff(rhs(eqae[XX])-lhs(eqae[XX]),t))=rhs(eqae[XX])-lhs(eqae[XX]));
end do;

EQAE1 := .1*sin(z1(t))*(diff(z1(t), t))+.2*z1(t)*(diff(z1(t), t))+.1*(diff(z1(t), t))*(y1(t)+1)+.1*z1(t)*(diff(y1(t), t))+.1*(diff(y1(t), t)) = cos(z1(t))-z1(t)^2-z1(t)*(y1(t)+1)-y1(t)

(45)

 

Dvars1:={seq(diff(z||x(t),t)=D||x,x=1..NAE)};

Dvars1 := {diff(z1(t), t) = D1}

(46)

Dvars2:={seq(rhs(Dvars1[x])=lhs(Dvars1[x]),x=1..NAE)};

Dvars2 := {D1 = diff(z1(t), t)}

(47)

icsn:=seq(subs(y||x(0)=y||x(t),icodes[x]),x=1..NODE),seq(subs(z||x(0)=z||x(t),icaes[x]),x=1..NAE);

icsn := y1(t) = 0., z1(t) = 0.

(48)

for j from 1 to NAE do

EQAEX||j:=subs(Dvars1,eqode,icsn,Dvars2,lhs(EQAE||j))=rhs(EQAE||j);

end do;

EQAEX1 := .1*sin(0.)*(diff(z1(t), t))+.1*(diff(z1(t), t)) = cos(z1(t))-z1(t)^2-z1(t)*(y1(t)+1)-y1(t)

(49)

Sys:={seq(EQODE||x,x=1..NODE),seq(EQAEX||x,x=1..NAE),seq(icodes[x],x=1..NODE),seq(icaes[x],x=1..NAE)};

Sys := {.1*(diff(z1(t), t)) = cos(z1(t))-z1(t)^2-z1(t)*(y1(t)+1)-y1(t), y1(0) = 0., z1(0) = 0., diff(y1(t), t) = z1(t)*(1/2+(1/2)*tanh(1000*t-1000))}

(50)

if Xexplicit=1 then

sol:=dsolve(Sys,numeric):

else

sol:=dsolve(Sys,numeric,stiff=true,implicit=true):

end if:

 

for XX from 1 to NODE do
a||XX:=odeplot(sol,[t,y||XX(t)],0..subs(pars,tf),color=red);
end do:

for XX from NODE+1 to NODE+NAE do
a||XX:=odeplot(sol,[t,z||(XX-NODE)(t)],0..subs(pars,tf),color=blue);
end do:

display(seq(a||x,x=1..NODE+NAE),axes=boxed);

 

End Example 3

 

Example 4

restart;

with(plots):

N:=11:h:=1/(N+1):

for i from 2 to N+1 do eq1[i]:=diff(y||i(t),t)=(y||(i+1)(t)-2*y||i(t)+y||(i-1)(t))/h^2-y||i(t)*(1+z||i(t));od:

for i from 2 to N+1 do eq2[i]:=0=(z||(i+1)(t)-2*z||i(t)+z||(i-1)(t))/h^2-(1-y||i(t)^2)*(exp(-z||i(t)));od:

eq1[1]:=(3*y1(t)-4*y2(t)+y3(t))/(2*h)=0: eq1[N+2]:=y||(N+2)(t)-1=0:

eq2[1]:=(3*z1(t)-4*z2(t)+1*z3(t))/(2*h)=0: eq2[N+2]:=z||(N+2)(t)=0:

eq1[1]:=subs(y1(t)=z||(N+3)(t),eq1[1]):

eq1[N+2]:=subs(y||(N+2)(t)=z||(N+4)(t),eq1[N+2]):

eqode:=[seq(subs(y1(t)=z||(N+3)(t),y||(N+2)(t)=z||(N+4)(t),eq1[i]),i=2..N+1)]:

eqae:=[eq1[1],eq1[N+2],seq(eq2[i],i=1..N+2)]:

icodes:=[seq(y||j(0)=1,j=2..N+1)]:

icaes:=[seq(z||j(0)=0,j=1..N+2),z||(N+3)(0)=1,z||(N+4)(0)=1]:

pars:=[mu=0.00001,q=1000,tint=1,tf=2]:

Xexplicit:=2:

ff:=subs(pars,1/2+1/2*tanh(q*(t-tint))):

NODE:=nops(eqode):NAE:=nops(eqae):

for XX from 1 to NODE do

EQODE||XX:=lhs(eqode[XX])=rhs(eqode[XX])*ff: end do:

for XX from 1 to NAE do

EQAE||XX:=subs(pars,-mu*(diff(rhs(eqae[XX])-lhs(eqae[XX]),t))=rhs(eqae[XX])-lhs(eqae[XX])); end do:

Dvars1:={seq(diff(z||x(t),t)=D||x,x=1..NAE)}:

Dvars2:={seq(rhs(Dvars1[x])=lhs(Dvars1[x]),x=1..NAE)}:

icsn:=seq(subs(y||x(0)=y||x(t),icodes[x]),x=1..NODE),seq(subs(z||x(0)=z||x(t),icaes[x]),x=1..NAE):

for j from 1 to NAE do

EQAEX||j:=subs(Dvars1,eqode,icsn,Dvars2,lhs(EQAE||j))=rhs(EQAE||j):

end do:

Sys:={seq(EQODE||x,x=1..NODE),seq(EQAEX||x,x=1..NAE),seq(icodes[x],x=1..NODE),seq(icaes[x],x=1..NAE)}:

if Xexplicit=1 then

sol:=dsolve(Sys,numeric,maxfun=0):

else

sol:=dsolve(Sys,numeric,stiff=true,implicit=true,maxfun=0):

end if:

 

for XX from 1 to NODE do

a||XX:=odeplot(sol,[t,y||(XX+1)(t)],1..subs(pars,tf),color=red): end do:

for XX from NODE+1 to NODE+NAE do

a||XX:=odeplot(sol,[t,z||(XX-NODE)(t)],1..subs(pars,tf),color=blue): end do:

display(seq(a||x,x=1..NODE),a||(NODE+NAE-1),a||(NODE+NAE),axes=boxed);

 

End of Example 4

 

Sometimes the parameters of the switch function and perturbation need to be tuned to obtain propoer convergence. Below is Example 1 shown for several cases using the 'parameters' option in Maple's dsolve to compare how tuning parameters affects the solution

restart:

with(plots):

eqode:=[diff(y1(t),t)=-y1(t)^2+z1(t)]: eqae:=[cos(y1(t))-z1(t)^0.5=0]:

icodes:=[y1(0)=0.25]: icaes:=[z1(0)=0.8]:

pars:=[tf=5]:

Xexplicit:=2;

Xexplicit := 2

(51)

ff:=subs(pars,1/2+1/2*tanh(q*(t-tint))):

NODE:=nops(eqode):NAE:=nops(eqae):

for XX from 1 to NODE do
EQODE||XX:=lhs(eqode[XX])=rhs(eqode[XX])*ff:
end do:

for XX from 1 to NAE do
EQAE||XX:=subs(pars,-mu*(diff(rhs(eqae[XX])-lhs(eqae[XX]),t))=rhs(eqae[XX])-lhs(eqae[XX]));
end do:

 

Dvars1:={seq(diff(z||x(t),t)=D||x,x=1..NAE)}:

Dvars2:={seq(rhs(Dvars1[x])=lhs(Dvars1[x]),x=1..NAE)}:

icsn:=seq(subs(y||x(0)=y||x(t),icodes[x]),x=1..NODE),seq(subs(z||x(0)=z||x(t),icaes[x]),x=1..NAE):

for j from 1 to NAE do

EQAEX||j:=subs(Dvars1,eqode,icsn,Dvars2,lhs(EQAE||j))=rhs(EQAE||j):

end do:

Sys:={seq(EQODE||x,x=1..NODE),seq(EQAEX||x,x=1..NAE),seq(icodes[x],x=1..NODE),seq(icaes[x],x=1..NAE)}:

if Xexplicit=1 then

sol:=dsolve(Sys,numeric,'parameters'=[mu,q,tint],maxfun=0):

else

sol:=dsolve(Sys,numeric,'parameters'=[mu,q,tint],stiff=true,implicit=true):

end if:

 

sol('parameters'=[0.1,1000,1]):

plot1:=odeplot(sol,[t-1,y1(t)],0..4,color=red):
plot2:=odeplot(sol,[t-1,z1(t)],0..4,color=blue):

sol('parameters'=[0.001,10,1]):

plot3:=odeplot(sol,[t-1,y1(t)],0..4,color=red,linestyle=dot):
plot4:=odeplot(sol,[t-1,z1(t)],0..4,color=blue,linestyle=dot):

display(plot1,plot2,plot3,plot4,axes=boxed);

 

In general, one has to decrease mu, and increase q and tint until convergence (example at t=3)

sol('parameters'=[0.001,10,1]):sol(3+1);

[t = 4., y1(t) = .738587929442734, z1(t) = .546472878850096]

(52)

sol('parameters'=[0.0001,100,10]):sol(3+10);

[t = 13., y1(t) = .738684397167344, z1(t) = .546618936273638]

(53)

sol('parameters'=[0.00001,1000,20]):sol(3+20);

[t = 23., y1(t) = .738694113087217, z1(t) = .546633473784526]

(54)

 

The results have converged to 4 digits after the decimal. Of course, absolute and relative tolerances of the solvers can be modified if needed

 

Download SolvingDAEsinMaple.mws

@Markiyan Hirnyk   I try not to use this package, as I think the results are not reliable enough. Here is the example, where instead of the three real roots it finds only one, despite the hint to look for the three roots:

restart;

DirectSearch:-SolveEquations(100^x=x^100, AllSolutions, solutions=3);

 

There are many other examples, particularly in discrete optimization in which it returns false results. Here is one example (well-known to you).

First 38 39 40 41 42 43 44 Last Page 40 of 71