## 386 Reputation

14 years, 111 days

## MaplePrimes Activity

### These are answers submitted by

Bogo,

Please see improved code. I replaced the integral procedure with a numerical dsolve solution in compiled parametric form. You can see that the modified version is much faster.

I know of at least one way to improve the speed even further, I will try that soon.

Hope you find this useful.

 > restart:
 > Digits:=15;
 (1)
 > de := diff(Y(x), x)+10^5*(Y(x)^2-YFD(x)^2)/x^2 = 0;
 (2)
 > YFD:=proc (x)  local xi,g; g:=2; if not type(evalf(x),'numeric') then       'procname'(x)    else Re(evalf((1/2)*g*(int((xi^2-x^2)^(1/2)*xi/(exp(xi)+1), xi = x .. infinity))/Pi^2)); end if; end proc;
 (3)
 > IC:=Y(1)=YFD(1);
 (4)
 > #infolevel[all]:=10;
 > sol:=dsolve({de,IC},type=numeric,known='YFD',method=lsode):
 > sol(1);
 (5)

Solution will be evaluated at a later stage

 > #sol(1.00001);
 > #sol(1.0001);

The itnegral equation is converted to a differential equation and solved in parametric and compiled form with respect to x and xi = 100 is taken to be infinity (not changing much beyond x = 45).

 > eq2:=diff(y(xi),xi)=(xi^2-1.*x^2)^(1/2)*xi/(exp(xi)+1.)/Pi^2;
 (6)
 > sol1:=dsolve({eq2,y(x)=0},type=numeric,'parameters'=[x],compile=true,abserr=1e-15,relerr=1e-14):
 Warning, relerr increased from .1e-13 to 5.00001e-013
 > sol1('parameters'=[1]);
 (7)
 > sol1(1);sol1(2);
 (8)
 > sol1(45.00000000000);
 (9)
 > sol1(100);
 (10)
 > YFD2:=proc (x)   global sol1,y; local xi,z1,g; g:=2; if not type(evalf(x),'numeric') then       'procname'(x)    else    sol1('parameters'=[x]); z1:=sol1(100); rhs(z1[2]);#subs(sol1(100),y(xi)):    end if; end proc;
 (11)
 > YFD(1);YFD2(1);
 (12)

One can note that the dsolve solution for the integral is much faster than the integral calucation and provides the same value.

 > de2:=diff(Y(x), x)+10^5*(Y(x)^2-YFD2(x)^2)/x^2 = 0;;
 (13)
 > sol2:=dsolve({de2,IC},type=numeric,known='YFD2',method=lsode):
 > sol2(1);
 (14)

Next time taken is compared for the original dsolve with integral for the procedure and the new dsolve.

 > t11:=time():sol(1.00001);time()-t11;
 (15)
 > t11:=time():sol2(1.00001);time()-t11;
 (16)
 > t11:=time():sol(1.0001);time()-t11;
 (17)
 > t11:=time():sol2(1.0001);time()-t11;
 (18)
 > with(plots):
 > odeplot(sol2,[x,Y(x)],1..2);
 >

Note that in sol2, compile=true can't be used as it is not able to recognize YDF2. If that can be enabled this code will run even faster.

## Partial solution...

see attached.

http://www.maplesoft.com/support/help/Maple/view.aspx?path=dsolve/numeric

I used method lsode. As this problem is stiff, use stiff = true, but you have to provide the diff of the function (see the help above above).

 > restart:
 > de := diff(Y(x), x)+10^5*(Y(x)^2-YFD(x)^2)/x^2 = 0;
 (1)
 > YFD:=proc (x)  local xi; g:=2; if not type(evalf(x),'numeric') then       'procname'(x)    else Re(evalf((1/2)*g*(int((xi^2-x^2)^(1/2)*xi/(exp(xi)+1), xi = x .. infinity))/Pi^2)); end if; end proc;
 Warning, `g` is implicitly declared local to procedure `YFD` (2)
 > IC:=Y(1)=YFD(1);
 (3)
 > #infolevel[all]:=10;
 > sol:=dsolve({de,IC},type=numeric,known='YFD',method=lsode,maxfun=0):
 > sol(1);
 (4)
 > sol(1.00001);
 (5)
 > sol(1.0001);
 (6)
 > #sol(1.001);
 >

I ran for shorttimes, it is taking long. You can read about lsode options if you want to use this. Also, please consider using classic worksheet or maple format for display. Most of the time was spent on copying and pasting from your code to mine, not the actual solution itself. Try classic worksheet, and see for yourself how easy it is to copy paste commands or write a code with large number of sentences/commands.

## solves...

Without BCs.  May be you can see that and find out which one makes sense. A second order BVP can have only 2 BCs. you have 3 for each variable.

with(inttrans);

this should work

## Try Events approach...

If you know what causes singularity, you can do this by finding the time at which event happens. See for example the code below. You can ofcourse use v(a) =b and call both initial t values and intial conditions as parameters in dsolve and then call the dsolve solution for different parameters.

 > restart;
 > Digits:=15;
 (1)
 > ode1:=      -0.1*diff(u(z),z,z) +      (z-2*diff(v(z)^(-1/2),z))*diff(u(z),z)+(3-2*diff(v(z)^(-1/2),z,z))*u(z)           =      0;
 (2)
 > ode2:= 0.1*diff(v(z),z,z)+0.01*z*diff(v(z),z)+0.02*v(z)-u(z)*v(z)^(1/2)=0;
 (3)
 > ics:= u(0)=0, v(0)=0.1, D(u)(0)=0, D(v)(0)=0:
 > sol:= dsolve({ode||(1..2), ics}, numeric,stop_cond=[v(z)-1e-7],abserr=1e-10):
 > s1:=sol(100);
 Warning, cannot evaluate the solution further right of 4.1328703, event #1 triggered a halt (4)
 > z1:=subs(s1,z);
 (5)
 >

## Alternate approach...

If you know the specific integral you want to calculate beforehand, then add an additional variable z(t) defined as

dz/dt = y^2 with z(0)=0. z(1) gives the integral. The advantage is that the integral you get is of the same order of accuracy as u. You can use arbitrary number of Digits (Digits = 20 or 30).

Unfortunately this approach can be used only if the resulting system is an ODE. If your resulting system is a DAE, Maple cannot solve it (DAE BVP)

 > restart:
 > eq:=-1.2*diff(y(t),t\$2)+0.2*y(t)=2;
 (1)
 > bcs:=y(0)=1,y(1)=0;
 (2)
 > eq2:=diff(z(t),t)=y(t)^2;
 (3)
 > sol:=dsolve({eq,eq2,bcs,z(0)=0},type=numeric,abserr=1e-8,maxmesh=100):
 > with(plots):
 > odeplot(sol,[[t,y(t)],[t,z(t)]],0..1,thickness=3,axes=boxed);
 > sol(1);
 (4)
 >

## Solve V(t) first and then solve for C(t)...

hope this helps.

 > restart:
 > eq1:=diff(V(t)*C(t),t)=G-K*C(t);
 (1)
 > eq2:=diff(V(t),t)=alpha-beta;
 (2)
 > dsolve({eq1,eq2});
 (3)
 > V(t):=c1+(alpha-beta)*t;
 (4)
 > eq1;
 (5)
 > dsolve({eq1,C(0)=c2});
 (6)
 >

## Try DAE form...

Then enter to Maple in DAE form. My guess is that you are lucky because this approach works for this case as the addition of u enables Maple to convert this system to explicit dy/dt = f form.  But in general Maple cannot solve nonlinear DAEs or ODEs if it cannot convert them to explicit ODE form dy/dt = f. In fact, this is a serious flaw. The assumption made here is that all ODEs and DAEs can be converted to dy/dt = f(y) form. This is not true for many nonlinear or implicit ODEs until you add addtional variables. Maple avoids using Newton Raphson or any nonlinear solvers in its ODE solver.

Code attached. It is best to post some range for parameters or the physics of the problem. I can't guess the values of the parameters. You have to find u(0) for every set of parameters. For the parameters I chose, there is a singularity at 3.22 or so.

 > restart:
 > ODE1:=A*u(t)^2+B*u(t)*(h(t)+C)+E*h(t)=F*cos(u(t));
 (1)
 > eqic:=A*u1^2+B*u1*C=F*cos(u1);
 (2)

For the simulation shown u has an initial condition of Pi/2. When parameters change, change ic accordingly.

 > ODE2:=diff(h(t),t)=u(t):
 > sol:=dsolve({ODE1,ODE2,h(0)=0,u(0)=Pi/2},type=numeric,parameters=[A,B,C,E,F],stiff=true,maxfun=0):
 > sol('parameters'=[0,0,1,1,1]):
 > with(plots):
 > odeplot(sol,[t,h(t)],0..3);
 > odeplot(sol,[t,u(t)],0..1);
 >
 >