# How to draw general functions and tangent lines

I wanted to draw following graphs of two general functions of Geometric interpretation of the Euler method. Thanks in advance

• Welcome to TeX.SX. Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. Commented Oct 3, 2017 at 12:04
• The question is not clear what aspect of plotting these graphs the OP needs help with. They both are reasonably complicated and a lot of work to build as a whole in one answer.
– lblb
Commented Oct 3, 2017 at 14:08
• Two general functions? Do you mean f(x) and the tangent line?. ... Do you want this graph for any function f, with defined x-step h? Commented Oct 4, 2017 at 6:43
• @Bobyandbob Yes, exactly Commented Oct 4, 2017 at 14:18

Using the tzplot package:

\documentclass[tikz]{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}
% \tzhelplines(9,7)
\tzaxes(-.5,-.3)(9,7){$x$}{$y$}
\def\Fx{.2*1.5^\x+1}
\tzfn[thick,blue]\Fx[0:8]{$y=f(x)=\frac1{10}\cdot1.5^x+1$}[a]
\tztangentat"tan"{Fx}{3}[0:8]
% intersections
\tzvXpointat*{Fx}{7}(A)
\tzvXpointat*{tan}{7}(B)
\tzvXpointat*{Fx}{3}(C)
% projections on x-axis
\tzprojsx[thick](C){$t_i$}(A){$t_{i+1}$}; % version 2
% horizontal dashed lines
\tzhfn[dashed](A)[8:0]{$y(t_{i+1})$}[l]
\tzhfn[dashed](B)[8:0]{$u_{i+1}$}[l]
\tzhfn[dashed](C)[8:0]{$u_i$}[l]
% labels
\tzline[<->]<1.2,0>(A){error}[r](B)
\tzline+[->](3,3.5){Tangent line}[at start,a](2.5,-1)
\tznode(6,3.5){$f(t_i,u_i)$}[l]
\tzline[<->,tzshorten={1em}{1.5em}]
(C|-{0,-1.5ex}){$h$}[inner sep=2pt,centered,fill=white]
(A|-{0,-1.5ex})
\end{tikzpicture}

\end{document}


A try with the function f(x)=scale*x^2. If i have time i will clearify my comments.

My approach contains 5 parameters:

• x-value of t_i: \pgfmathsetmacro\TI{1.5}
• x-value of t_{i+1}: \pgfmathsetmacro\TIplusone{3.5}
• scale of the function: \pgfmathsetmacro\scale{0.2} <=> (f(x)=scale*x^2)
• slope/gradient m of the function: \pgfmathsetmacro\m{\scale*2*\TI}
• intercept b of the function: \pgfmathsetmacro\b{\scale*\TI*\TI-\m*\TI}

Math (Background)

Given function:

f(x)=0.3*x^2


Finding the tangent line from f(x) in Point P(t_i,u_i):

1.) Compute slope

m = f'(t_i) = 0.3*2*t_i
m = f'(1.5) = 0.3*2*1.5


2.) insert P in line function

u_i = m * t_i+ b => b= ...


3. tangent line:

y = m*x+b


Result:

MWE:

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}

\pgfmathsetmacro\TI{1.5}
\pgfmathsetmacro\TIplusone{3.5}

\pgfmathsetmacro\scale{0.2}

\pgfmathsetmacro\m{\scale*2*\TI} % slope/gradient, f = \scale*\x*\x -> f'= \scale*2*x_0
\pgfmathsetmacro\b{\scale*\TI*\TI-\m*\TI} %intercept

\pgfmathsetmacro\domainStart{\TI-0.5}
\pgfmathsetmacro\domainEnd{\TIplusone+0.5}
\begin{document}

%custom dash pattern from https://tex.stackexchange.com/a/101263/124842
\makeatletter
\tikzset{
dot diameter/.store in=\dot@diameter,
dot diameter=1.5pt,
dot spacing/.store in=\dot@spacing,
dot spacing=5pt,
dots/.style={
line width=\dot@diameter,
line cap=round,
dash pattern=on 0pt off \dot@spacing
}
}
\makeatother

\begin{tikzpicture}[domain=\domainStart:\domainEnd]
% axis
\draw[->] (\domainStart-0.2,0) -- (\domainEnd+0.2,0) node[right] {$x$};
\draw[->] (\domainStart,-0.2) -- (\domainStart,\domainEnd) node[right] {$x$};

% functions
\draw[color=blue] plot (\x,\scale*\x*\x) node[right] {$f(x) =\scale*x^2$};
\draw[color=black] plot (\x,\m*\x+\b);% node[right] {$f'(x) =0.1*2*x$};
% dots
%
\draw[dots] (\TI,0) -- (\TI,\m*\TI+\b) node[below,pos=0] (A) {$t_i$};
%
\draw[dots] (\TIplusone,0) -- (\TIplusone,\scale*\TIplusone*\TIplusone) node[below,pos=0] (B) {$t_{i+1}$};

% error
\draw[black,<->] (\domainEnd+0.2,\m*\TIplusone+\b)  -- (\domainEnd+0.2,\scale*\TIplusone*\TIplusone) node[right,pos=0.5] {error};

% h
\draw[black,<->] (A)  -- (B) node[pos=0.5,fill=white,inner sep=0.5pt] {h};

%  y-ticks
\draw[dashed] (\domainStart,\m*\TI+\b+0.2) -- (\domainEnd,\m*\TI+\b+0.2) node[left,pos=0] {$u_{i}$};
\draw[dashed] (\domainStart,\m*\TIplusone+\b+0.2) -- (\domainEnd,\m*\TIplusone+\b+0.2) node[left,pos=0] {$u_{i+1}$};
\draw[dashed] (\domainStart,\scale*\TIplusone*\TIplusone+0.2) -- (\domainEnd,\scale*\TIplusone*\TIplusone+0.2) node[left,pos=0] {$y(t_{i+1})$} ;
\end{tikzpicture}
\end{document}

• I appreciate your help! Commented Oct 5, 2017 at 4:15