Skip to main content

differential equations - Solving nonlinear ODE with boundary conditions


I am trying to solve the below fucntions using a NDsolve, but I can't get the the converges of them at the certain initial variables.I'm using Mathematica 11.3. I appreciate any help.


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 == k2 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}]

Answer



The quantity κ2 in the question is undefined. However, the cross-posting identified by Mariusz Iwaniuk in a comment above suggests that k2 actually is meant. With this change, the computation is equivalent to solving the ODE, {y''[r] + 2 y'[r]/r == k2 Sinh[y[r]], y'[R] == 0, y'[ϵ] == 0} with a discontinuity in y'[r] equal to Z g at r == 1. With this modification to the ODE, it can be recast as


Z = 800; g = Rationalize[0.0238, 0];
k2 = Rationalize[ 0.000194519, 0];
ϵ = 10^-4; R = Rationalize[1.5472, 0];

ps = ParametricNDSolveValue[{y''[r] + 2 y'[r]/r == k2 Sinh[y[r]],
y[ϵ] == y0, y'[ϵ] == 0, WhenEvent[r == 1, y'[r] -> y'[r] + Z g]}, {y, y'},
{r, ϵ, R}, {y0}, Method -> "StiffnessSwitching", WorkingPrecision -> 30];
sol = FindRoot[Last[ps[y0]][R], {y0, -11.25, -11.}, Evaluated -> False][[1, 2]]
(* -11.1979 *)

Plot[First[ps[sol]][r], {r, ϵ, R}, AxesLabel -> {r, y}, ImageSize -> Large,
LabelStyle -> {Black, Bold, Medium}]

enter image description here



Plot[Last[ps[sol]][r], {r, ϵ, R}, PlotRange -> All, AxesLabel -> {r, y'}, 
ImageSize -> Large, LabelStyle -> {Black, Bold, Medium}]

enter image description here


The computation takes only seconds.


Addendum


In response to a comment below, the solution for k2 = Rationalize[0.05716690559951900467, 0]; is


sol = Quiet@FindRoot[Last[ps[y0]][R], {y0, -12, 0}, Evaluated -> False][[1, 2]]
(* -5.51467 *)


The corresponding plot of y looks very much like that above but shifted approximately by the shift in y0.


enter image description here


The corresponding plot of y' also looks very much like that above. Note that the FindRoot search range has been expanded in this computation, and Quiet has been prepended to avoid printing the numerous errors that FindRoot encounters before finding the correct answer.


Second Addendum


Varying k2, as requested by the OP in a comment below, is accomplished as follows.


psk = ParametricNDSolveValue[{y''[r] + 2 y'[r]/r == k Sinh[y[r]], 
y[ϵ] == y0, y'[ϵ] == 0, WhenEvent[r == 1, y'[r] -> y'[r] + Z g]}, {y, y'},
{r, ϵ, R}, {y0, k}, Method -> "StiffnessSwitching", WorkingPrecision -> 30];
ytablog = Table[{Exp[k], Quiet@FindRoot[Last[psk[y0, Exp[k]]][R], {y0, -12, 0},
Evaluated -> False][[1, 2]]}, {k, -9, 0, 1/10}];

ListLogLinearPlot[ytablog, PlotRange -> All, Joined -> True, AxesLabel -> {k2, y[0]},
ImageSize -> Large, LabelStyle -> {Black, Bold, Medium}]

enter image description here


A rough fit to this curve is Log[k2] - 2.655. Varying R can be accomplished in a similar manner.


Interestingly, varying R between 5/4 and 2 has minimal effect on the plot just above. Below are plots of y[r] for R = Range[5/4,2,1/4] - {"Blue", "Orange", "Green", "Red"}.


enter image description here


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...