如何绘制凸函数?

如何绘制凸函数?

在此处输入图片描述我怎样才能在乳胶中绘制这样的凸函数?

答案1

tikz 解决方案

\documentclass[tikz,border={15mm 15mm 15mm 15mm},]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\pgfplotsset{
    every non boxed x axis/.style={
        xtick align=center,
        enlarge x limits=true,
        x axis line style={line width=0.8pt, -latex}
},
    every boxed x axis/.style={}, enlargelimits=false
}
\pgfplotsset{
    every non boxed y axis/.style={
        ytick align=center,
        enlarge y limits=true,
        y axis line style={line width=0.8pt, -latex}
},
    every boxed y axis/.style={}, enlargelimits=false
}
\usetikzlibrary{
   arrows.meta,
  intersections,
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in,axis equal image,
    axis lines=middle,
    xmin=0,xmax=8,
    xlabel=$x$,ylabel=$y$,
    ymin=-0.25,ymax=4,
    xtick={\empty},ytick={\empty}, axis on top
]

% 
\addplot[thick,domain=0.25:7,blue,name path = A]  {-x/3 + 2.75} coordinate[pos=0.4] (m) ;
\draw[thick,blue, name path =B] (0.15,4) .. controls (1,1) and (4,0) .. (6,2) node[pos=0.95, color=black, right]  {$f(x)$} coordinate[pos=0.075] (a1)  coordinate[pos=0.95] (a2);
\path [name intersections={of=A and B, by={a,b}}];

% 
\draw[densely dashed] (0,0) -| node[pos=0.5, color=black, label=below:$a$] {}(a1);
\draw[densely dashed] (0,0) -| node[pos=0.5, color=black, label=below:$x_{1}$] {}(a);
\draw[densely dashed, name path=D] (3,0) -|node[pos=0.5, color=black, label=below:$\lambda x_{1}+ (1-\lambda)x_{2}$] {} node[pos=1, fill,circle,inner sep=1pt] {}(m);
\draw[densely dashed] (0,0) -|node[pos=0.5, color=black, label=below:$x_{2}$] {}(b);
\draw[densely dashed] (0,0) -|node[pos=0.5, color=black, label=below:$b$] {}(a2);

% 
\path [name intersections={of=B and D, by={c}}] node[fill,circle,inner sep=1pt] at (c) {}; 

% 
\node[anchor=south west, text=black] (d) at (0.75,3) {$f[\lambda x_{1}+(1-\lambda)x_{2}]$};
\node[anchor=south west, text=black] (e) at (5,2.5) {$\lambda f(x_{1})+(1-\lambda)f(x_{2})$};
\draw[-{Latex[width=4pt,length=6pt]}, densely dashed] (d) -- (c);
\draw[-{Latex[width=4pt,length=6pt]}, densely dashed] (e) -- (m);
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

一个pstricks办法:

\documentclass[x11names, svgnames, border=3pt]{standalone}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf} %% for pdflatex compilation
\def\f{(x-1)²/2 + 0.1}

\begin{document}

\psset{unit=2.5cm, algebraic, plotstyle=curve, plotpoints=500,arrowinset=0.125, arrowsize=3pt, linejoin=1,labelsep =2pt}
\everymath{\scriptstyle}
\begin{pspicture*}(-0.2,-0.2)(3.2,2.4)
    \psaxes[linecolor=LightSteelBlue3, ticks=none, labels=none, arrows=->](0,0)(-0.2,- 0.2)(3.2, 2.4)%
    \psplot{0.2}{3}{\f}
    \psline[linecolor=RoyalBlue2](0.4,0.275)(2.8,1.72)
    \psset{linestyle=dashed, dash =3pt 2pt, linewidth=0.5pt,dotstyle=o}
    \psline(2.2,0)(2.2,1.36)(0,1.36)
    \uput[ur](0,1.36){$λf(u) + (1-λ)fm(v)$}
    \psCoordinates(*0.4 {\f})
    \uput[ur](0,0.275){$ f(u)$}\uput [d](0.4,0){$\vphantom{()}u$}
    \psCoordinates(*2.2 {\f})
    \uput[ur](0,0.82){$ f(λu + (1-λ)v)$}\uput [d](2.2,0){$λu + (1-λ)v$}
    \psCoordinates(*2.8 {\f})
    \uput[ur](0,1.72){$ f(v)$}\uput [d](2.8,0){$\vphantom{()}v$}
    \psset{linestyle=none, dotstyle=o, linecolor=red}
    \psCoordinates(*0.4 {\f})
    \psCoordinates(*2.2 {\f})
    \psCoordinates(*2.8 {\f})
    \psdot(2.2,1.36)
\end{pspicture*}

\end{document} 

在此处输入图片描述

相关内容