# Huge time to plot a function with replace all (/.)

I am looking to plot the function f[x, y]

  f[x_?NumericQ, y_?NumericQ] := Cos[x + y]/Sqrt[1 - y^2]


where y[x]<1 is the solution of the equation

y[x_] := y /. FindRoot[1 + int[x, y] + D[int[x, y], y] == 0, {y, 0.8}]


and where int is the numerical integral

int[x_?NumericQ, y_?NumericQ] := NIntegrate[Exp[-x/Sqrt[1 - t^2]]/(t^2 - y^2), {t, 0, y, 1}, Method -> "PrincipalValue"]

• First, if I try to calculate the derivative D[int[x, y], y]] at x = 50 and y = 0.8

Block[{x = 50, y = 0.8}, D[int[x, y], y]]


I get the error

(*\[PartialD](-5.391169080810072*^-23)/\[PartialD]0.8*)


General::ivar: 0.8 is not a valid variable.

• Second (my real problem), when I plot this function for 0<x<10 by replace all command

Plot[f[x, y]/.y -> y[x], {x, 0, 10}, Frame -> True, PlotRange ->{-3, 3}]]


it takes a huge time.

• You're reusing the symbol y in a lot of different ways in these 4 lines. There's a reason it's known as good practice to use unique & descriptive function/variable names. I think doing so in this scenario might help. – user6014 Sep 30 '18 at 14:28

You need to limit the scope of y in y[x_] := y /. FindRoot[1 + int[x, y] + D[int[x, y], y] == 0, {y, 0.8}].

f[x_?NumericQ, y_?NumericQ] := Cos[x + y]/Sqrt[1 - y^2]
ClearAll[y]
y[x_] := Module[{y},
y /. FindRoot[
1 + int[x, y] + Evaluate[D[int[x, y], y]] == 0, {y, 0.8}]];
int[x_?NumericQ, y_?NumericQ] :=
NIntegrate[Exp[-x/Sqrt[1 - t^2]]/(t^2 - y^2), {t, 0, y, 1},
Method -> "PrincipalValue"]
Block[{x = 50, y = 0.8}, Evaluate[D[int[x, y], y]]]


1.39305*10^-22

Your real problem seems to be, that the integral does not converge. I cannot even evaluate int[10, 0.8], as I get warning messages:

NIntegrate::zeroregion: Integration region {{0.8,0.800000000000000044408920985011711129445345555642315378316693834590}} cannot be further subdivided at the specified working precision. NIntegrate assumes zero integral there and on any further indivisible regions.

I do not think these messages should be ignored and there is a reasonable precision limit one should use. Before increasing precision, I would rethink possibility of analytical simplification.

• Thank you @Johu, it's a nice analysis. For me the integral converge: int[10, 0.8]=-0.000029950778. What's the problem for you? – Gallagher Sep 30 '18 at 14:56
• What's about the huge time in plotting this function? – Gallagher Oct 2 '18 at 7:48
• One cannot plot it if it cannot be evaluated. – Johu Oct 2 '18 at 8:15
• Do you have any suggestions about this huge time? – Gallagher Oct 2 '18 at 10:41