« home

Concave Functions

mathphysicsstatistical physicscetztikz

xln(x)x \ln(x) is convex and x1x - 1 is its tangent, so xln(x)x1xx \ln(x) \geq x - 1 \enskip \forall x.


Concave Functions

  Download

PNGPDFSVG

  Code

  LaTeX

concave-functions.tex (27 lines)

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      domain=0:1,
      xlabel=$x$,
      smooth,
      thick,
      axis lines=left,
      every tick/.style={thick},
      legend style={cells={anchor=west}},
      legend pos=north west]

    \addplot[color=blue]{x};

    \addplot[color=red]{-x*ln(x)};

    \legend{$x$,$-x \, \ln(x)$}

  \end{axis}
\end{tikzpicture}
\end{document}

  Typst

concave-functions.typ (44 lines)

#import "@preview/cetz:0.3.2": canvas
#import "@preview/cetz-plot:0.1.1": plot

#set page(width: auto, height: auto, margin: 8pt)

#let size = (8, 5)

#canvas({
  plot.plot(
    size: size,
    x-min: 0,
    x-max: 1,
    x-label: $x$,
    y-tick-step: 0.2,
    x-tick-step: 0.2,
    x-grid: true,
    y-grid: true,
    legend: "inner-north-west",
    legend-style: (stroke: .5pt),
    axis-style: "left",
    {
      plot.add-hline(0, style: (stroke: 0.5pt))
      plot.add-vline(0, style: (stroke: 0.5pt))

      // x function
      plot.add(
        style: (stroke: blue + 1.5pt),
        domain: (0, 1),
        label: $x$,
        x => x,
      )

      // -x ln(x) function
      plot.add(
        style: (stroke: red + 1.5pt),
        domain: (0.01, 1), // avoid x=0 since ln(0) is undefined
        samples: 100,
        label: $-x ln(x)$,
        x => -x * calc.ln(x),
      )
    },
  )
})