# How to numerically plot the function $f(x)=\cos(x)\int^x_0\frac{\sin t}{\cos ^2(t) \sqrt{2+\sin^{2}\left(t\right)}}dt$

I'd like to numerically plot the function

$$f(x)=\cos(x)\int^x_0\frac{\sin t}{\cos ^2(t) \sqrt{2+\sin^{2}\left(t\right)}}dt \tag{1}$$

I can plot this analytically by evaluating the integral in $$(1)$$;

$$f(x)=\frac{\sqrt{2+\sin^{2}\left(x\right)}}{3}-\frac{\sqrt{2}\cos\left(x\right)}{3} \tag{2}$$

which exists on the range $$(0,\pi)$$. The problem is when I want to numerically plot this function from equation $$(1)$$ without explicitly integrating the integral in $$(1)$$.

I ask this because I would like to plot similar functions for powers other than $$2$$ and $$1/2$$, for example I would like to plot

$$f(x)=\cos(x)\int^x_0\frac{\sin t}{\cos ^2(t) \sqrt[m]{2+\sin^{m}\left(t\right)}}dt \tag{3}$$

but for larger powers the integral doesn't exist in closed form and so a nice formula like eq $$(2)$$ cannot be used and the plotting needs to be done numerically.

When I try and plot this numerically, for example by plotting $$(1)$$ with Desmos, the function is only defined up to $$x=\frac{\pi}{2}$$.

See the image below where I compare the numerical plot of $$(1)$$ in blue with the analytic plot of $$(2)$$ in red.

The integral in $$(1)$$ diverges as $$x\to\frac{\pi}{2}$$ however because $$\cos x \to 0$$ in the limit $$f(\frac{\pi}{2})$$ is finite, I think because of the diverging integral my plotter registers the value of $$(1)$$ as undefined and stops plotting once the plotter reaches this point. Is there a way to plot this function in its entirety numerically?

Any suggestions would be welcome.

Let $$J(x)=\int \frac{\sin x ~dx}{\cos ^2 x \sqrt{2+\sin^2 x}}=-\int \frac{dt}{t^2 \sqrt{3-t^2}}=\int\frac{udu}{\sqrt{3u^2-1}}$$ Here we have used \cos x=t$$, t=1/u$$, next we use $$3u^2-1=v$$, to get $$J=\frac{1}{6}\int \frac{dv}{\sqrt{v}}=\frac{1}{3} \sqrt{v}=\frac{\sqrt{2+\sin^2 x}}{3\cos x}.$$ Hence, $$f(x)=\frac{1}{3}[\sqrt{2+\sin^2x}-\sqrt{2} \cos x]$$ This is a periodic function with the period as $$2\pi$$. $$f(0)=0, f(\pi)=\frac{2\sqrt{2}}{3}, f(2\pi)=0$$ $$f'(x)=\frac{\sin x \cos x}{3\sqrt{2+\sin^2x}}+\frac{\sqrt{2}}{3}\sin x$$ So $$f'(0)=f'(\pi)=f'(2\pi)=0$$, so $$f_{min}=0$$ and $$f_{max}=f(\pi)=2\sqrt{2}/3$$. Now you can plot $$f(x)$$ for $$[0, \pi]$$ which is bell shaped touching $$x$$-axis at $$x=0,\pi$$. See below:

• Hi thanks for your answer, I think for your formula of $f(x)$, there needs to be a $\cos x$ multiplying the root at the end, similar to equation $(2)$ in my post. The issue I am having is plotting the function straight from equation $(1)$ rather than analytically calculating the integral to get $(2)$ which is what you've done unless I have missed your point. This is because for other powers such a nice formula as $(2)$ may not exist. This needs to be done without carrying out any integration analytically. I've edited my post to make this requirement clearer I hope. Commented Jan 8, 2021 at 17:33
• Thanks, you may see my answer now, I have corrected it. Commented Jan 8, 2021 at 18:16

So i found a way to plot it by using integration by parts to pull the divergent part of the integrand out of the integral. By taking

$$u=\frac{\sin(x)}{\sqrt{2+\sin^2\left(x\right)}} \qquad \frac{dv}{dx}=\sec^2(x) \implies \frac{du}{dx}=\frac{2\cos(x)}{(2+\sin^2x)^{\frac{3}{2}}} \qquad v=\tan(x)$$

We can perform integration by parts to find

$$f(x):=\cos(x)\int^x_0\frac{\sin x}{\cos^{2}\left(x\right)\sqrt{2+\sin^2x}}dx=\frac{\sin^2x}{\sqrt{2-\sin^2x}}-\cos(x)\int^x_0\frac{2\sin x}{(2+\sin^2x)^{\frac{3}{2}}}dx$$

The divergent part of the integral has now been removed and cancelled by the $$\cos (x)$$ term out front of the initial integral. Since the remaining integral is finite over $$(0,\pi)$$ my plotter can now plot it fine.