# Finding a numerical solution and plotting it over a wide range

My problem is that I am trying to find a Numerical solution to a function of the form f(x,c)== K for x for some given values of c, K, and plot this function over a wide range of values of c.

My function f(x,c) is of the form Integrate[g(x,y,c),{y,a,b}], where a and b are some numerical values. This integration however cannot be evaluated symbolically in x and c,as g is a really complicated function and hence can only be performed numerically.

So if I write my function as f(x_,c_)= NIntegrate[g(x,y,c),{y,a,b}], I get some numerical value of f(x,c) for some numerical values of x and c. (If I write it as Integrate[g(x,y,c),{y,a,b}] I donot get any value for f(x,c)).

But with this form of the function, I cannot get any solution using NSolve, or FindRoot. (FindRoot doesn't converge, and it may not be practical if I plan to plot it for a wide range of values of c).

So at this point, plotting the solutions seems like a far fetched dream. I apologize for not providing details of the functions, as the function g arises from a lot of complicated functions.

EDIT:

I decided to post my attempt, and some extent of the functions, based on one of the Solutions by Carl. For my case arh is a known function, and gamma and w are numerical values:

eqn = Inactive[Integrate][
g1[Hi[mx], a, mx]*mx, {a, 1, arh[\[Gamma], w]}] +
Inactive[Integrate][
g2[Hi[mx], a, mx]*mx, {a, arh[\[Gamma], w], \[Infinity]}] ==0.1198/(9.2*10^24);

Distribute /@ D[eqn, mx]

int0[Hi_, mx_?NumberQ] :=NIntegrate[g1[Hi, a, mx], {a, 1, arh[\[Gamma], w]}]
int1[Hi_, mx_?NumberQ] :=
NIntegrate[
Derivative[0, 0, 1][g1][Hi, a, mx], {a, 1, arh[\[Gamma], w]}]
int2[Hi_, mx_?NumberQ] :=
NIntegrate[
Derivative[1, 0, 0][g1][Hi, a, mx], {a, 1, arh[\[Gamma], w]}]

int02[Hi_, mx_?NumberQ] :=
NIntegrate[g2[Hi, a, mx], {a, arh[\[Gamma], w], \[Infinity]}]
int12[Hi_, mx_?NumberQ] :=
NIntegrate[
Derivative[0, 0, 1][g2][Hi, a, mx], {a,
arh[\[Gamma], w], \[Infinity]}]
int22[Hi_, mx_?NumberQ] :=
NIntegrate[
Derivative[1, 0, 0][g2][Hi, a, mx], {a,
arh[\[Gamma], w], \[Infinity]}]


My Functions are of the form

g1[Hi_, a_, mx_] :=
a^2/(Hrh[a, \[Gamma], w, Hi]*Trh[\[Gamma], Hi]^3) \[Sigma]vtotal[
a, \[Gamma], w, Hi, mx, Trh11] nXeq[a, \[Gamma], w, Hi, mx,
Trh11]^2;
g2[Hi_, a_, mx_] :=
a^2/(H[a, \[Gamma], w, Hi]*Trh[\[Gamma], Hi]^3) \[Sigma]vtotal[
a, \[Gamma], w, Hi, mx, T] nXeq[a, \[Gamma], w, Hi, mx, T]^2;


And I try to Solve:

sol = NDSolveValue[{(int0[Hi[mx], a, mx] +
mx (int1[Hi[mx], mx] +
Derivative[1][Hi][mx] int2[Hi[mx], mx])) + (int02[Hi[mx],
a, mx] +
mx (int12[Hi[mx], mx] +
Derivative[1][Hi][mx] int22[Hi[mx], mx])) == 0,
Hi[10^-7] == 10^-9}, Hi, {mx, .1, 10^-19}];


This returns an error NDSolveValue::ndnum: Encountered non-numerical value for a derivative at mx == 1.*^-7.

NOTE: Trh, and \sigmavtotal and nXeq are defined previously, and are of the form: \[Sigma]vtotal[a_, \[Gamma]_, w_, Hi_, mx_, T_Symbol] nXeq[a_, \[Gamma]_, w_, Hi_, mx_, T_Symbol] Trh[\[Gamma]_, Hi_]

and the other functions are of the form T[a_, \[Gamma]_, w_, Hi_] and Trh11[a_, \[Gamma]_, w_, Hi_]

I want to plot for mx: 0.1 to 10^-19

• Share your attempt otherwise we will be not of much help. – zhk Oct 13 '18 at 4:12
• Recommend you provide code that fully runs to where we can replicate your error (I get a different error due to arh not a valid limit of integration) – MikeY Dec 13 '18 at 1:06

Without a concrete example I can only guess at an approach that might work.

g[x_, y_, c_] := 1/Sqrt[x^2 + y^2 + c^2]

a = 0; b = 1;


Functions that use numeric techniques should have their arguments restricted to numeric values.

f[x_?NumericQ, c_?NumericQ] := NIntegrate[g[x, y, c], {y, a, b}]

soln[c_?NumericQ, K_?NumericQ] := x /. FindRoot[f[x, c] == K, {x, 1/2}]


Verifying that soln produces a numeric value

soln[1/2, 1/2]

(* 1.85275 *)


Plotting soln for a range of c and K values

Plot3D[soln[c, K], {c, 0, 1}, {K, 1/10, 1}]


• I posted more details of my specific question. Thanks in Advance – TheInvoker Oct 13 '18 at 19:24

I would use NDSolveValue for this kind of problem. You are trying to find the dependence of x on c, so let's use x[c] to make this dependence explicit. Then your equation is:

eqn = Inactive[Integrate][g[x[c], y, c], {y, 1, 2}] == k;


where I arbitrarily chose $$a=1$$ and $$b=2$$. Let's differentiate this equation to obtain an ODE:

Distribute /@ D[eqn, c] //TeXForm


$$\int _1^2x'(c) g^{(1,0,0)}(x(c),y,c)dy+\int _1^2g^{(0,0,1)}(x(c),y,c)dy=0$$

Define:

int0[x_, c_?NumberQ] := NIntegrate[g[x, y, c], {y, 1, 2}]
int1[x_, c_?NumberQ] := NIntegrate[Derivative[0,0,1][g][x, y, c], {y, 1, 2}]
int2[x_, c_?NumberQ] := NIntegrate[Derivative[1,0,0][g][x, y, c], {y, 1, 2}]


where the _?NumberQ test prevents premature evaluations of NIntegrate with non-numeric integrands. Finally, let's come up with a sample g:

g[x_, y_, c_] := BesselI[1, x y] BesselJ[1, c y]


Then, the ODE to be solved is:

sol = NDSolveValue[
{
int2[x[c], c] x'[c] + int1[x[c], c] == 0,
x[1] == 1
},
x,
{c, .1, 2}
];


Let's check whether sol satisfies the required equation:

Plot[int0[sol[c], c], {c, .1, 2}, PlotRange -> 1]


And a visualization of the solution:

Plot[sol[c], {c, .1, 2}]
`

• This solution looks really promising. The only problem is that I donot know of any initial condition like x[1]==1 . I can guess an order, like x[10^-7]==10^-10. Here is what my eqn looks like:= eqn = Inactive[Integrate][ g1[Hi[mx], a, mx]*mx, {a, 1, arh[[Gamma], w]}] + Inactive[Integrate][ g2[Hi[mx], a, mx]*mx, {a, arh[[Gamma], w], [Infinity]}] == 0.1198/(9.2*10^24); There are two seperate complicated functions g1 and g2 over different known ranges, gamma and w are known, and arh is a known function. I need to find Hi[mx], and I need to plot from mx= 0.1 to 10^-19 – TheInvoker Oct 13 '18 at 18:16
• I posted more details by editing the question, thanks in advance – TheInvoker Oct 13 '18 at 19:25