Message Boards Message Boards

Are there any alternatives for Piecewise?

Posted 4 years ago

I'm trying to solve a system of differenial equations involving a piecewise function with NDSolve. I tried to define this function using Piecewise but the kernel keeps crashing. I also tried to express the function in terms of UnitStep and HeavisideTheta by means of SimplifyPWToUnitStep` but the crash still takes place. (More details on the system of equations and its numerical implementation can be found here: https://mathematica.stackexchange.com/questions/218704/kernel-crash-after-repeated-use-of-ndsolve)

Do you know if there is any other alternative or workaround that I could use in order to avoid this annoying crash?

Thank you!!

POSTED BY: Quim iguaz
4 Replies

I tried Ulrich Neumann's suggestion of the stackexchange link you gave and it works just fine.

To be explicit:

Vpot[p_, r_] := 
  Piecewise[{{p[r]^2/(4*Pi), 
     p[r] < 217/500}, {-(87/250) + (4*p[r])/
       5 + (87*(-1827 + 4400*p[r]))/(160000*Pi), 
     p[r] >= 109/
       250}, {(86031603 + 640*Pi*(217 - 500*p[r])^2 - 
        405876800*p[r] + 500350000*p[r]^2 - 50000000*p[r]^3)/(800000*
        Pi), True}}]; 

and

Upot[p_, r_] := 
  Piecewise[{{1, p[r] <= 217/500}, {(957 + 320*Pi)/(200*p[r]), 
     p[r] >= 109/250}, {10007/4 + Pi*(800 - 1736/(5*p[r])) - 
      253673/(250*p[r]) - 375*p[r], True}}];

and using (for an example code snippet

...., Upot[p, r] --w^2/E^v[r])*p[r...,

within NDSolve's equation list. I got: ;

wprecision = 15
bosonstar[pcmax, 3] (*returns null in less than a couple seconds on a macbook laptop*)

but the value of rfin is defined

rfin (*returns 3.47146583566452*)
POSTED BY: W. Craig Carter
Posted 4 years ago

Hi!

Thanks for the reply. I tried to implement the modifications you suggested but the code still crashes. I can only solve the equations a few times before the crash. (Just to make it clear, the problem is that I need to use NDSolve several times and it always happens that after some iterations it crashes, hence the do loop you find in the link. However, it is true that if I just needed to solve the equations once, the code I provided would work just fine in general).

POSTED BY: Quim iguaz

At this point you might want to contact Tech Support to see if the (possibly platform-dependent) crash can be replicated at our end.

POSTED BY: Daniel Lichtblau
Posted 4 years ago

Ok! Thanks for the help. I already reported it to Technical Support.

POSTED BY: Quim iguaz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract