Suppose I have f(x)=x^2+x+sin(x). Then I want to plot g(f(x)), where g(x) is polynomial in x, then I want to further plot h(g(f(x))), where h(x) is another polynomial in x, and the it continues. Is there a way to save f(x), g(x) and give them a name instead of rewriting it every time?

Currently I just rewrite everything as follows: let g(x)=2x

\draw[domain=-1:1] plot (\x^2+\x+sin(\x));
\draw[domain=-1:1] plot (2*(\x^2+\x+sin(\x)));
% I have to rewrite f(x)

I'd like to define a variable f that represents the function f I can just call and plot. Is there a way to do this?

\documentclass[tikz, border=1cm]{standalone}
declare function={
\draw[red] plot[domain=-1:1]  ( \x, {f(\x)} );
\draw[green] plot[domain=-1:1]  ( \x, {g(f(\x))} );
\draw[blue] plot[domain=-1:1]  ( \x, {h(f(\x))} );

Three curves

  • 1
    If you want the trig function in radians, you can use the option trig format=rad. Dec 7 '21 at 22:03
  • Do you know how to have multivariate functions? I tried adding another argument to the function, but that didn't work. I tried this: f(\x,\y)=\x+\y
    – Schach21
    Dec 8 '21 at 5:27
  • Exactly that works (with a semicolon). What error do you get? Dec 8 '21 at 8:20
  • Is there any trick to let us write the variable without distracting backslashes? Dec 8 '21 at 8:53
  • @GodMustBeCrazy: You could use PGFPlots, but that is beyond the scope of this answer. (but it will not work with declare function - only directly in a \addplot or \newcommand) Dec 8 '21 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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