0

I have to plot something like that (the function I have to integrate is more complicate than this) function

How can I do that? Which pst package do I need?

Thanks!

  • 2
    With pgfplots you can only do it if you can compute the integral numerically somewhere else. Otherwise you need to switch to PSTricks, Asymptote and so on. – percusse Aug 23 '15 at 10:58
  • The pgfplots package is not a computer algebra system. If you want to plot functions like yours, I suggest you compute a polynomial expression for $f( x )$, which is easy, and use it to express the plot function. –  Aug 23 '15 at 11:01

1 Answers1

1

Here's an approach using pgfplots in combination with the sagetex package (to handle the necessary calculations that percussse mentions). The calculations are carried out by a computer algebra system, Sage, with the integral calculated by f(t1) = integrate(x^2,x,t1,3) and then y coordinates for plotting are determined by y1_coords = [(f(t1)).n(digits=6) for t1 in x1_coords]. That's based on this answer from AskSagemath. Perhaps there's a better way by now given this is an old answer?

\documentclass{standalone}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepackage{sagetex}
\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations}
\pgfplotsset{compat=newest}% use newest version
\begin{document}
\begin{sagesilent}
####### SCREEN SETUP #####################
LowerX = -3.0
UpperX = 3.0
LowerY = -2.0
UpperY = 20.0
step = .01
Scale = 1.0
xscale=1.0
yscale=1.0
#####################TIKZ PICTURE SET UP ###########
output = r""
output += r"\begin{tikzpicture}"
output += r"[line cap=round,line join=round,x=8.75cm,y=8cm]"
output += r"\begin{axis}["
output += r"grid = none,"
#Change "both" to "none" in above line to remove graph paper
output += r"minor tick num=4,"
output += r"every major grid/.style={Red!30, opacity=1.0},"
output += r"every minor grid/.style={ForestGreen!30, opacity=1.0},"
output += r"height= %f\textwidth,"%(yscale)
output += r"width = %f\textwidth,"%(xscale)
output += r"thick,"
output += r"black,"
output += r"axis lines=center,"
#Comment out above line to have graph in a boxed frame (no axes)
output += r"domain=%f:%f,"%(LowerX,UpperX)
output += r"line join=bevel,"
output += r"xmin=%f,xmax=%f,ymin= %f,ymax=%f,"%(LowerX,UpperX,LowerY, UpperY)
#output += r"xticklabels=\empty,"
#output += r"yticklabels=\empty,"
output += r"major tick length=5pt,"
output += r"minor tick length=0pt,"
output += r"major x tick style={black,very thick},"
output += r"major y tick style={black,very thick},"
output += r"minor x tick style={black,thin},"
output += r"minor y tick style={black,thin},"
#output += r"xtick=\empty,"
#output += r"ytick=\empty"
output += r"]"
##############FUNCTIONS#################################
t1 =  var('t1')
f(t1) = integrate(x^2,x,t1,3)
lambda t1: f(t1), (t1,-3,3)
x1_coords = srange(LowerX,UpperX,step)
y1_coords = [(f(t1)).n(digits=6) for t1 in x1_coords]
output += r"\addplot[thin, NavyBlue, unbounded coords=jump] coordinates {"
for i in range(0,len(x1_coords)):
    if (y1_coords[i])<LowerY or (y1_coords[i])>UpperY:
        output += r"(%f,inf) "%(x1_coords[i])
    else:
        output += r"(%f,%f) "%(x1_coords[i],y1_coords[i])
output += r"};"
output += r"\addlegendentry{$f(x)$}"
##### COMMENT OUT A LINE OF SAGESILENT BY STARTING WITH #
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

Which gives this output running in Sagemath Cloud: enter image description here Note that to use the sagetex package you need to install Sage on your computer or get a free account on Sagemath Cloud.

DJP
  • 12,451
  • Thanks! I didn't know Sage. The function I have to plot is http://i.imgur.com/adKFgB4.png but no success by now. How can I plot that? Thanks again – Lorenzo Fantera Aug 23 '15 at 15:37
  • I'm having trouble reading that. I that an x or an alpha? And variable t, along with x isn't specified. You should show what you've tried with a MWE. – DJP Aug 23 '15 at 15:50
  • sorry, i'm making some mistakes... The function I have to plot is like http://i.imgur.com/LzeXqdP.jpg. My problem with Sage is when the variable of integration is in the denominator. For example, taking the code you wrote me and putting "1/x", or "x^(-1)" instead of "x^2", Sage computes the code right (or I believe that, it gives me no issues) but the output remains the last one it "liked". It's not a domain problem, since I modified it in order to exclude indeterminations – Lorenzo Fantera Aug 23 '15 at 18:11