0

Let's say I have a function f(x,k) that depends on an integer k. I now want to use pgfplots to plot the function g(x) = f(x,0) + f(x,1) + ... + f(x,n). How can I achieve this? Here is my code:

    \begin{tikzpicture}
        \begin{axis}[
        axis lines=middle,
        xmin=-3, xmax=3,
        ymin=0, ymax=0.45,
        domain=-3:3,
        samples=100,
        smooth,
        xlabel=$x$,
        ylabel=$f(x)$,
        every axis y label/.style={at=(current axis.above origin),anchor=south},
        every axis x label/.style={at=(current axis.right of origin),anchor=west},
        xtick={-3,-2,-1,0,1,2,3},
        ytick={0,0.1,0.2,0.3,0.4},
        xticklabels={-3,-2,-1,0,1,2,3},
        yticklabels={0,0.1,0.2,0.3,0.4},
        enlargelimits=upper,
        clip=false
        ]
    \addplot[blue,thick] {x^2};
    \end{axis}
\end{tikzpicture}

What I want now is something like this:

        \addplot[blue,thick] {x^2+(x+1)^2+...+(x+100)^2};
kerf
  • 51

2 Answers2

2

A little late but the short answer is to change your ymin and ymax: ymin=305000, ymax=380000,, adjust your y-axis labels as you see fit and change your function to 101*x^2+10100*x+338350. The result running in Gummi is: enter image description here

That answer might seem a bit strange, so go to a Sage Cell Server and copy/paste the code below in

f=x^2
for i in range(1,101):
    f+=(x+i)^2
plot(f,-100,100)

then press Evaluate to get enter image description here

We're dealing with what looks like a parabola and if you think about it, each of the 101 terms in the sum is a quadratic. The x^2 terms all have coefficient 1, so the function has 101x^2. The x terms are 2x, 4x, ..., 200x so the coefficient of x is 2(1+2+...+100). There's a formula for that 2(100/2)(1+100). The constant terms are 1^2+2^2+3^2+...+100^2. There's a formula for that as well (1/6)(100)(100+1)(2(100)+1). The Sage cell server can do the work, though: enter image description here

In a more general case, which couldn't be reasoned through, you can use the sagetex package, which relies on an open source computer algebra system to do the work. For this problem we could write

\documentclass{article}
\usepackage{sagetex,amsmath,amssymb,tikz,pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{sagesilent}
f=x^2
for i in range(1,101):
    f+=(x+i)^2
g=diff(f,x)
sol = solve(g==0,x)
\end{sagesilent}
Let $f(x,k)=\sum_{i=0}^{k}(x+i)^2$. For $k=100$ this becomes, after 
explanding and collecting like terms, $f(x,100)=\sage{simplify(expand(f))}$
which is a parabola. Since $f'(x)=\sage{g}$ the minimum value is when 
$x=\sage{sol[0].rhs()}$.
The plot of $f(x,100)$ looks like this over $[-3,3]$:
\begin{center}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,xmin=-3,  xmax=3,ymin=305000,ymax=380000,domain=-3:3,samples=100,  smooth,xlabel=$x$,ylabel=$f(x)$, every axis y label/.style={at=(current axis.above origin), anchor=south},every axis x label/.style={at=(current axis.right of origin), anchor=west},xtick={-3,-2,-1,0,1,2,3},xticklabels={-3,-2,-1,0,1,2,3},enlargelimits=upper, clip=false]
\addplot[blue,thick] {101*x^2+10100*x+338350};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

The result in Cocalc is: enter image description here

Note that f=x^2 for i in range(1,101):f+=(x+i)^2 defines the function (the last number, 101, doesn't get executed in Python). I can pull up the expansion \sage{simplify(expand(f))}, calculate its derivative, g=diff(f,x) and find the value of the minimum sol = solve(g==0,x). These values can then be put into the LaTeX document.

Sage is not part of LaTeX so you will need a free Cocalc account or download Sage to your computer.

DJP
  • 12,451
1

An idea to start with maybe, but I am not sure whether this would also work with large numbers such as 100 ... Anyways: you could define a recursive function which would enable you to avoid repetitive code.

In the following code compfun(5) should be equivalent to (x+0)^2+(x+1)^2+...+(x+5)^2:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{math}

\begin{document} \begin{tikzpicture}[ evaluate={ function basefun(\i) { return pow(x+\i,2); }; function compfun(\j) { if \j < 1 then { return basefun(0); } else { return compfun(\j - 1) + basefun(\j); }; }; }, ] \begin{axis}[ axis lines=middle, xmin=-10, xmax=10, ymin=0, ymax=50, samples=100, smooth, xlabel=$x$, ylabel=$f(x)$, every axis y label/.style={ at=(current axis.above origin), anchor=south }, every axis x label/.style={ at=(current axis.right of origin), anchor=west }, enlargelimits=upper, ] \addplot[blue, thick] {compfun(5)}; \end{axis} \end{tikzpicture} \end{document}

enter image description here