# Get a numerical solution to a nonlinear ODE?

**URL:** <https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095>\
**Category:** Questions\
**Tags:** physics, equation-solving, numerical-computation\
**Created:** [September 15, 2018, 3:44am UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095 "2018-09-15T03:44:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammedalziyadi](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mohammedalziyadi](https://community.wolfram.com/u/mohammedalziyadi)\
**Post date:** [September 15, 2018, 3:44am UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/1 "2018-09-15T03:44:54Z")

</div>

I am trying to solve a nonlinear ODE BY applying a NDsolve and using StiffnessSwitching method, but when I try to find the root of my equation it gives me a error message. The same code is working well in Mathematica version 9, but in M.version 11.3 that I just upgraded is not working I do not know why this happened.  
Would anyone help me please?

Here is my code

```
Z=800;
g= 0.023800000000000000000;
k2= 0.000194519;
 R= 1.5472;
ytest0= -13.911917694213733`;
? = $MachineEpsilon ;
y1[ytest_?NumericQ] := 
    NDSolve[
      {y''[r] + 2 y'[r]/r == ?2 Sinh[y[r]] , y[1] == ytest, 
       y'[?] == 0}, y, {r, ?, 1}, 
       Method -> {"StiffnessSwitching", "NonstiffTest" -> False}];
 y2[ytest_?NumericQ] := 
   NDSolve[
     {y''[r] + 2 y'[r]/r == ?2 Sinh[y[r]], 
      y[1] == ytest, y'[R] == 0}, y, {r, 1, R}, 
      Method -> {"StiffnessSwitching", "NonstiffTest" -> False}];
y1Try[ytest_?NumericQ] := y'[1] /. y1[ytest];
y2Try[ytest_?NumericQ] := y'[1] /. y2[ytest];
f = ytest /. FindRoot[y1Try[ytest] - y2Try[ytest]==-Zg, {ytest, ytest0}]

```

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [September 15, 2018, 6:25am UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/2 "2018-09-15T06:25:47Z")

</div>

Hi,

I notice there is a typo in the code:

```
\[Kappa]2 = 0.000194519 (*it should be kappa instead of k*)

```

Also I highlighted `ParametricNDSolveValue` ([doc](https://reference.wolfram.com/language/ref/ParametricNDSolveValue.html)) to simplify some parts of your code. The notebook with code is attached.

 ![1685Sketch.png](https://global.discourse-cdn.com/wolfram/original/2X/3/32ef1b855f03c711dd9af2c0aa1876a482b7b2e7.png)

[ndsolve.nb](https://community.wolfram.com/uploads/short-url/jfaFhNOkJcCFoyjnfZwQvZCJ2ME.nb) (6.94 KB)

---

<div class="post-metadata">

**Author:** ![mohammedalziyadi](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mohammedalziyadi](https://community.wolfram.com/u/mohammedalziyadi)\
**Post date:** [September 15, 2018, 3:53pm UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/3 "2018-09-15T15:53:31Z")

</div>

Why did you use (ParametricNDSolveValue)? Also, I did not see that you used ytest0.

---

<div class="post-metadata">

**Author:** ![mohammedalziyadi](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mohammedalziyadi](https://community.wolfram.com/u/mohammedalziyadi)\
**Post date:** [September 16, 2018, 9:40pm UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/4 "2018-09-16T21:40:47Z")

</div>

The find root is not what I post it, when I write it as (see below) is not working??

```
f = ytest /. FindRoot[y1Try[ytest] - y2Try[ytest]==-Z/g, {ytest, ytest0}]

```

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [September 17, 2018, 4:18am UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/5 "2018-09-17T04:18:26Z")

</div>

Hi,

`ParametricNDSolveValue` is basically the same as your `y1` and `y2`, which are the functional of the solution of the two ODE’s. Looking at your code, I assume you want to know the 1st order derivative of each functional w.r.t. `ytest` parameter at location `x=1`. `ParametricNDSolveValue` kind of simplifies this process for you. Please correct me if I interpret your question incorrectly.

The other benefit of using `ParametricNDSolveValue` is that this function only processes the input equation once [( aka. conversion to canonical form)](https://reference.wolfram.com/language/tutorial/NDSolveStateData.html) even the resulting functional varies as the parameters get updated.

---

<div class="post-metadata">

**Author:** ![mohammedalziyadi](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@mohammedalziyadi](https://community.wolfram.com/u/mohammedalziyadi)\
**Post date:** [September 17, 2018, 4:56am UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/6 "2018-09-17T04:56:41Z")

</div>

You are right! Also, thank you for the great explanation about ParametricNDSolveValue. The question want to ask is why find root is not what I post it, when I write it as (see below) is not working??

> f = ytest /. FindRoot[y1Try[ytest] - y2Try[ytest]==(-Zg), {ytest,  
> ytest0}] I did not see the discontinuity (Zg) in y’[r] at x == 1

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [September 17, 2018, 1:28pm UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/7 "2018-09-17T13:28:17Z")

</div>

I checked the equation to see where the zero might be through DiscretePlot from -5 to 5 with `step=0.5`

 ![ScreenShot2018-09-17at9.11.03PM.png](https://global.discourse-cdn.com/wolfram/original/3X/0/2/02c32dd77d972a2360cb9cbdce655aa92fd87286.png)

Also I notice `y1Try[ytest]` stops working when `ytest<-11.5`. Not sure about the mathematical reason behind this non-linear equation that causing the solver’s failure at this domain.

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [September 21, 2018, 5:26pm UTC](https://community.wolfram.com/t/get-a-numerical-solution-to-a-nonlinear-ode/13095/8 "2018-09-21T17:26:43Z")

</div>

Hi

```
NDSolveValue[{y''[r] + 2 y'[r]/r == \[Kappa]2 Sinh[y[r]], y[1] == ytest0,
   y'[\[Epsilon]] == 0}, y, {r, \[Epsilon], 1}, 
 Method -> {"Shooting", 
   "StartingInitialConditions" -> {y[\[Epsilon]] == -11}}]

```

I found the shooting method extend the range of `ytest0` to cover your input. -11 is found by using the `StiffnessSwitching` method first.
