i want to plot the integral of y=sin(x^2) in pgfplots but dont know what to put inside the ''{ }''. This is because its a prety hard function to integrate. Can someone help me!
-
3You might be better off with Asymptote/PSTricks for this. – percusse Feb 21 '14 at 15:55
-
@Jubobs. According to wolframalpha the integral of sin(x^2) is sqrt(π/2) FresnelS(sqrt(2/π) x). It gives a plot in wolfram but i cant take the function and put it into pgfplots – Henrik Feb 21 '14 at 16:02
-
Pgfplots comes without numerical integration schemes. But since you already managed to plot your function in some external tool, you could sample the function and plot that data file in pgfplots. – Christian Feuersänger Feb 21 '14 at 21:33
3 Answers
run with latex->dvips->ps2pdf:
\documentclass[pstricks]{standalone}
\usepackage{pst-func}
\begin{document}
\begin{pspicture}(-5,-3)(4.5,3)
\psaxes[ticksize=0 4pt, subticks=5](0,0)(-4.4,-2.5)(4,2.5)
\psplot[algebraic,plotpoints=5000,linecolor=red]{-4}{4}{sin(x*x)}
\psCumIntegral[plotpoints=10000,Simpson=10,linecolor=blue]{0}{4}{ dup mul RadtoDeg sin }
\psCumIntegral[plotpoints=10000,Simpson=10,linecolor=blue]{0}{-4}{ dup mul RadtoDeg sin }
\end{pspicture}
\end{document}

-
I have a question about your code but please bear in mind that I'm an absolute ignorant of
pstricks(and most of other latex packages for that matter). Why have you plotted the integral in two separate domains (i.e. [-4,0] and [0,4]) instead of [-4,4]? – Pouya Feb 21 '14 at 19:03 -
2With that numerical integration it does nothing else than adding the area under the function with Simpson's rule. It is not the mathematical solution of
\int sin x^2 dx, which can be seen at mathworld.com. The integral must be symmetrical:F(x)=-F(-x)that's the reason why I plot it from the origin to the left and to the right. – Feb 21 '14 at 19:32 -
Seems indeed also reasonably easy to obtain with Asymptote. Following Herbert's inspired lead:
import graph;
// Unit
unitsize(2cm);
// Axis lengths
real xmin = -4, xmax = 4.25;
real ymin = -1.5, ymax = 1.5;
// The sin(x^2) function
real f(real x) {return sin(x^2);};
// Its integration (via the Simpson algorithm)
real g(real x) {return simpson(f, 0, x);};
// The curves
draw(graph(f, -4, 4, n = 1600, operator ..), red);
draw(graph(g, -4, 4, n = 1600, operator ..), blue);
// Axes
xaxis(xmin, xmax, Ticks(Step=1, step=0.2, OmitTick(0)));
yaxis(ymin, ymax, Ticks(Step=1, step=0.2, OmitTick(0)));
label("$O$", (0,0), 1.1SW);
If saved as, for example, fresnel.asy, you can process this code with the command line
asy -f pdf -V fresnel.asy
to obtain a PDF file and view it.

- 18,756
You can get that plot shown by Wolfram Alpha into pgfplots (and other complicated functions) by using the sagetex package. This gives you access to the free CAS system which can do the calculations of the value which then are typeset into into LaTeX. The y_coords array gets all the calculations of the integral and then all of the points (x_coords, y_coords) are typeset (by sage) into tikzpicture commands which are finally output into your tex document.
\documentclass{article}
\usepackage{sagetex}
\usepackage{pgfplots}
\usepackage{xcolor}
\pagestyle{empty}
\begin{document}
\begin{sagesilent}
x=var('x')
t = var('t')
x_coords = [t for t in srange(-4.0,4.0,.01)]
y_coords = [numerical_integral(sin(x^2), 0, t)[0] for t in srange(-4.0,4.0,.01)]
output = ""
output += r"\begin{tikzpicture}[scale=1]"
output += r"\begin{axis}[xmin=-4.0, xmax=4.0, ymin=-1.2, ymax=1.2,]"
output += r"\addplot[thin,blue] coordinates {"
for i in range(0,len(x_coords)):
output += r"(%f, %f) "%(x_coords[i],y_coords[i])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}
Here is the result of the code running in Sagemath Cloud:

- 12,451