TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Guido, ChrisS, cmhughes, Thorsten, Peter Jansson Feb 22 '14 at 7:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You 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 – Hankyboy 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
up vote 7 down vote accepted

run with latex->dvips->ps2pdf:


\psaxes[ticksize=0 4pt, subticks=5](0,0)(-4.4,-2.5)(4,2.5)
\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 }


enter image description here

share|improve this answer
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
With 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. – Herbert Feb 21 '14 at 19:32
I see what you did there! – Pouya Feb 21 '14 at 19:46

Seems indeed also reasonably easy to obtain with Asymptote. Following Herbert's inspired lead:

import graph;
// Unit
// 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.

enter image description here

share|improve this answer

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.

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}"

Here is the result of the code running in Sagemath Cloud: enter image description here

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.