Why Maple generates unknown constant in the solution of PDE when all initial and boundary conditions are specified - maple

I do not understand why Maple 2017.3, generates a solution
to this wave PDE with unknown constant in it called _C6.
As all boundary conditions and initial conditions are given.
Actually the solution is wrong on another account,
it gives divide by zero when n=2. But my question is on
the presence of this constant.
This is the solution for wave PDE u_tt = 4* u_xx. String. With
both ends fixed. Initial position is u(x,0)= sin(x)^2
and zero initial velocity i.e. u_t(x,0) = 0
Maple:
restart;
pde:=diff(u(x,t),t$2)= 4*diff(u(x,t),x$2);
bc:=u(0,t)=0,u(Pi,t)=0;
ic:=u(x,0)=sin(x)^2,D[2](u)(x,0)=0;
sol:=pdsolve([pde,bc,ic],u(x,t));
Maple 2017.3 on windows gives
There should not be _C constants in the solution. For reference, here is Mathematica solution
ClearAll[u,t,x,n];
pde=D[u[x,t],{t,2}]==4D[u[x,t],{x,2}];
ic={Derivative[0,1][u][x,0]==0,u[x,0]==Sin[x]^2}
bc={u[0,t]==0,u[Pi,t]==0};
sol=DSolve[{pde,bc,ic},u[x,t],{x,t}];
sol=sol/.K[1]->n (*n looks better than K[1] for index*)
Notice there is no constant in the solution (but Mathematica solution has
the same problem for n=2 as Maple's. Divide by zero. So its solution is also wrong.
But my question here is not on the n=2 issue, but on the constant _C6 that Maple generates, and why is it there?
Maple 2017.3 on windows.
Update
Just to confirm that latest physics package (thanks to the answer ) did fix this issue

If I download and install the latest revision for Maple 2017 of the Physics,DEs,MathFuncs Library then I get the following using Maple 2017.2 for 64bit Linux.
restart;
Physics:-Version();
"/usr/local/maple/maple2017.2/lib/Physics2017.mla", 2018, March 9, 23:54 hours
pde:=diff(u(x,t),t$2)= 4*diff(u(x,t),x$2):
bc:=u(0,t)=0,u(Pi,t)=0:
ic:=u(x,0)=sin(x)^2,D[2](u)(x,0)=0:
sol:=pdsolve([pde,bc,ic],u(x,t)):
lprint(sol);
u(x, t) = Sum(4*((-1)^n-1)*sin(x*n)*cos(2*t*n)/(Pi*n*(n^2-4)), n = 1 .. infinity)

Related

how to convert this result using exponentials to hyperbolic trig functions?

The solution to Laplace PDE on rectangle is usually written using hyperbolic trig functions. I solve this PDE using Maple. Verified Maple solution is correct. But having hard time figuring how to make its result match the book result.
I tried sol:=convert(rhs(sol),trigh): then simplify(sol,trig); and it become little closer to the book solution, but is still can be more simplified.
Are there any tricks to do this?
Here is MWE
restart;
interface(showassumed=0):
pde:=diff(u(x,y),x$2)+diff(u(x,y),y$2)=0:
bc:=u(0,y)=0,u(a,y)=f(y),u(x,0)=0,u(x,b)=0:
sol:=pdsolve([pde,bc],u(x,y)) assuming(0<=x and x<=a and 0<=y and y<=b):
sol:=subs(infinity=20,sol);
Which gives
The above is same as the following, which I am trying to convert the above to
textbookU:= Sum(2*sin(n*Pi*y/b)*(Int(sin(n*Pi*y/b)*f(y),
y = 0 .. b))*sinh(n*Pi*x/b)/(b*sinh(n*Pi*a/b)), n = 1 .. 20);
The above are the same. I checked few points, and they give same answer. They must be the same, as the above textbook solution is correct, and I am assuming Maple solution is correct.
Now I tried to convert Maple sol to the above as follows
sol:=convert(rhs(sol),trigh):
simplify(sol,trig);
May be someone knows a better way to obtain the textbook solution form, starting from the Maple solution above.
Using Maple 2017.3 on windows
After the convert you can first expand it, to then simplify it again:
s := convert(sol, trigh):
s := expand(s):
simplify(s);
which gives:

how to plot the solution to this PDE?

Maple generates a strange solution form for this PDE. I am having hard time plotting the solution.
The solution is in terms of infinite series. I set the number of terms to say 20, and then set the time to plot the solution at t=2 seconds. Then want to plot the solution now for x=0..1. But the plot comes out empty.
When I sample the solution, and use listplot, I get correct solution plot.
Here is MWE
restart;
pde:=diff(u(x,t),t)=diff(u(x,t),x$2)+x;
bc:=u(0,t)=0,u(1,t)=0;
ic:=u(x,0)=x*(1-x);
sol:=pdsolve({pde,ic,bc},u(x,t)):
sol:=value(sol);
Now set the number of terms to 20 and set t=2
sol2:=subs(t=2,sol):
sol2:=subs(infinity=20,sol2);
The above is what I want to plot.
plot(rhs(sol2),x=0..1);
I get empty plot
So had to manually sample it and use listplot
f:=x->rhs(sol2);
data:=[seq([x,f(x)],x=0..1,.01)]:
plots:-listplot(data);
Solution looks correct, when I compare it to Mathematica's result. But Mathematica result is simpler as it does not have those integrals in the sum.
pde=D[u[x,t],t]==D[u[x,t],{x,2}]+x;
bc={u[0,t]==0,u[1,t]==0};
ic=u[x,0]==x(1-x);
DSolve[{pde,ic,bc},u[x,t],x,t];
%/.K[1]->n;
%/.Infinity->20;
%/.t->2;
And the plot is
Question is: How to plot Maple solution without manually sampling it?
Short answer seems to be that it is a regression in Maple 2017.3.
For me, your code works directly in Maple 2017.2 and Maple 2016.2 (without any unevaluated integrals). I will submit a bug report against the regression.
[edited] Let me know if any of these four ways work for your version (presumably Maple 2017.3).
restart;
pde:=diff(u(x,t),t)=diff(u(x,t),x$2)+x;
bc:=u(0,t)=0,u(1,t)=0;
ic:=u(x,0)=x*(1-x);
sol:=pdsolve({pde,ic,bc},u(x,t)):
sol:=value(sol);
sol5:=value(combine(subs([sum=Sum,t=2,infinity=20],sol))):
plot(rhs(sol5),x=0..1);
sol4:=combine(subs([sum=Sum,t=2,infinity=20],sol)):
(UseHardwareFloats,oldUHF):=false,UseHardwareFloats:
plot(rhs(sol4),x=0..1);
UseHardwareFloats:=oldUHF: # re-instate
sol2:=subs([sum=Sum,int=Int,t=2],sol):
# Switch integration and summation in second summand of rhs(sol).
sol3:=subsop(2=Sum(int(op([2,1,1],rhs(sol2)),op([2,2],rhs(sol2))),
op([2,1,2],rhs(sol2))),rhs(sol2)):
# Rename dummy index and combine summations.
sol3:=Sum(subs(n1=n,op([1,1],sol3))+op([2,1],sol3),
subs(n1=n,op([1,2],sol3))):
# Curtail to first 20 terms.
sol3:=lhs(sol2)=subs(infinity=20,simplify(sol3));
plot(rhs(sol3),x=0..1);
F:=unapply(subs([Sum='add'],rhs(sol3)),x):
plot(F,0..1);
[edited] Here is yet another way, working for me in Maple 2017.3 on 64bit Linux.
It produces the plot quickly, and doesn't involve curtailing any sum at 20 terms. Note that it does not do your earlier step of sol:=value(sol); since it does active int rather than Int before hitting any Sum with value. It also uses an assumption on x corresponding to the plotting range.
restart;
pde:=diff(u(x,t),t)=diff(u(x,t),x$2)+x:
bc:=u(0,t)=0,u(1,t)=0:
ic:=u(x,0)=x*(1-x):
sol:=pdsolve({pde,ic,bc},u(x,t)):
solA:=subs(sum=Sum,value(eval(eval(sol,t=2),Int=int))) assuming x>0, x<1;
plot(rhs(solA),x=0..1) assuming x>0, x<1;

ARPACK gives different answers from Matlab and NAG

I'm playing with ARPACK. I looked into the examples they provide, zndrv4.f, in the ARPACK/EXAMPLES/COMPLEX/ directory. I also came cross into NAG Fortran Library. In NAG, there are some linear problem solvers F12***. The F12*** routines in NAG are equivalent to the znaupd in ARPACK. So I want to check if they will yield the same results.
I first looked into the example provided in the user guide of F12ARF at http://www.nag.co.uk/numeric/fl/nagdoc_fl22/pdf/F12/f12arf.pdf for example. In the end, it yields the results of
509.9390
380.9092
659.1558
271.9412
around the shift=500. I solved the same generalized eigenvalue problem in Matlab. Matlab gave the same results.
But when I used znaupd from ARPACK to solve the same problem, I obtained different answers. The 4 eigenvalues are now
rd1 = 501.65650188259684
rd2 = 480.15153312181440
rd3 = 526.52596256924164
rd4 = 461.99019999608828
The routines in NAG and ARPACK both use SHIFTED INVERSE mode and solve a generalized problem.
I'm not sure what was wrong. I attached my scripts for the ARPACK zndrv4.f (it's basically the same as the example file provided by ARPACK, I just need to change a little bit the matrices around line 174 to be the same as that in NAG.) and the Matlab file zndrv4.m.
https://www.dropbox.com/s/b9f1btl7a2ugrh3/zndrv4.f?dl=0
https://www.dropbox.com/s/pctmennp64mkn9m/zndrv4.m?dl=0
Update: The M matrix in F12ARF is normalized (entries divided by a six). I followed this and got the above wrong results in ARPACK. Now if I didn't divide the entries by six, the ARPACK script gives me the correct answer (the same as Matlab). Now I'm even more confused. It seems to say that the ARPACK routine is not robust?

Using MATLAB pcg to Solve Inexactly

I am using MATLAB's PCG subroutine to solve a system of linear equations. However, I don't want it to solve exactly. I want it to run for only 20 iterations and if it doesn't converge, I want it to return the value at the 20th iteration.
What MATLAB (My version is the latest one) is doing however is returning a zero vector if it doesn't find an acceptable solution by 20 iterations. Is there any way to override this without changing the source code of pcg.m?
I have a code which I wrote which does that (I just copied from Wikipedia) but obviously, it is not close to how robust MATLAB's version is.
function [x] = conjgrad(A,b,x)
r=b-A*x;
p=r;
rsold=r'*r;
for i=1:20
Ap=A*p;
alpha=rsold/(p'*Ap);
x=x+alpha*p;
r=r-alpha*Ap;
rsnew=r'*r;
if sqrt(rsnew)<1e-10
break;
end
p=r+rsnew/rsold*p;
rsold=rsnew;
end

How to force Matlab/Octave cov function to use optional parameter

I'm using the cov (covariance) function of Matlab and Octave. Actually I'm using Octave, but in the end it has to work for both. This function has an optional second or third parameters to indicate whether normalization should be done with N or N-1.
If I do this: cov(points,1) (where points is 4x2 matrix) I get following error:
error: cov: x and y must have the same number of observations
On a general note I would like to know how is Matlab/Octave able to distinguish if the second parameter is another matrix or an optional parameter (because it can have 2 or 3 parameters).
More specifically I would like to know how can I solve my problem?
Matlab cov() documentation: http://www.mathworks.de/help/techdoc/ref/cov.html
Octave cov() documentation: http://www.gnu.org/software/octave/doc/interpreter/Correlation-and-Regression-Analysis.html
EDIT: I'm using Octave 3.2.4 on Ubuntu 12.04
EDIT2: The solution is to install a newer version of Octave. This features was implemented after 3.2.
This looks like an Octave bug (at least with version 3.0.5); it works fine in Matlab (at least with version 7.10).
Of course, to work around, you could just compute:
cov(a) * (N-1) / N
type help cov, and note that opt has been removed doesn't exist.
better yet, do
gedit /usr/share/octave/3.2.4/m/statistics/base/cov.m
and compare it to the old new code
Opt is gone recent. Use Oli's trick in the meantime
Edit: I take that back, this is a new feature. Not sure which version of Octave is using it, but you need an upgrade. Either, you do an unstable synaptic upgrade if you can find a repository, or you build it from source.
Edit 2: feature added Jan 2011 (3.4.1). 3.2.4 released Jan 2010. current version is 3.6.1