# 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. – magula Oct 3 '17 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 Oct 3 '17 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? – Bobyandbob Oct 4 '17 at 6:43
• @Bobyandbob Yes, exactly – Janeth_A Oct 4 '17 at 14:18

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! – Janeth_A Oct 5 '17 at 4:15