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

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:

Related

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;

Is there an inverse factorial expression I can use in Matlab?

I want to edit this to get numberOfCircuits on its own on the left. Is there a possible way to do this in MATLAB?
e1=power(offeredTraffic,numberOfCircuits)/factorial(numberOfCircuits)/sum
The math for this problem is given in https://math.stackexchange.com/questions/61755/is-there-a-way-to-solve-for-an-unknown-in-a-factorial, but it's unclear how to do this with Matlab's functionality.
I'm guessing the easy part is rearranging:
fact_to_invert = power(offeredTraffic,numberOfCircuits)/sum/e1;
Inverting can be done, for instance, by using fzero. First define a continuous factorial based on the gamma function:
fact = #(n) gamma(n+1);
Then use fzero to invert it numerically:
numberOfCircuits_from_inverse = fzero(#(x) fact(x)-fact_to_invert,1);
Of course you should round the result for safe measure, and if it's not an integer then something's wrong.
Note: it's very bad practice (and brings 7 years bad luck) to name a variable with a name which is also a built-in, such as sum in your example.

Numeric integration in Matlab

I want to do a numerical integration of the following equation in Matlab.
For simplicity, lets pretend that only the second fraction of the equation is there.
And this is what I have tried so far:
for lambda=280:2500
numerator(lambda) = trapz(Einc(lambda:lambda+1)*St(lambda));
denominator(lambda) = trapz(Eref(lambda:lambda+1)*St(lambda));
end
Fraction= nansum(numerator)/ nansum(denominator);
St is not defined in the entire 280-2500 range, that is why I use nansum at the end. But overall I feel that what I’m doing is wrong and that there should be a much more normal and correct way of calculating this, yet I have tried googling this and asked on Mathworks forum as well but no one answers me even though this should be a pretty common thing to calculate… Could someone please help me how do write this formula?

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?

Effcient way to do FFT shift in matlab (without using fftshift function)

http://www.mathworks.com/help/techdoc/ref/fftshift.html
If you check that link - thats what I want to do in the first picture - swap quadrants of a matrix.
However, I cant seem to think of a good way to do this without having several loops to pull out the relevant sub-matrices.
I need it to work with MxN matrices, where M and N can be any combination of even and odd.
Thanks
The following should work
sz = ceil(size(A)/2)
A = A([sz(1)+1:end, 1:sz(1)], [sz(2)+1:end, 1:sz(2)])
That only works for 2d matrices, but can be easily generalized to the Nd case.
If you enter type fftshift.m at MATLAB's command line, you'll see the source code for MATLAB's implementation of the function (use edit fftshift.m if you want to view it in the editor with syntax highlighting). I'm not posting the code here, as it is copyrighted. However, you can try it on your machine and re-implement the same in C. Its up to you to figure out the license terms etc, if you're into any of that.