I am preparing some slides for numerical method classes. I have difficulty identify the best package to draw plot like below enter image description here

I know there is software like Geogebra, but I prefer to do within latex. Any suggestions for a quick and convenient package?


Check the following code:




% Axes
\draw[-latex,name path=xaxis] (-1,0) -- (12,0) node[above]{\large $x$};
\draw[-latex] (0,-2) -- (0,8)node[right]{\large $y$};;

% Function plot
\draw[ultra thick, orange,name path=function]  plot[smooth,domain=1:9.5] (\x, {0.1*\x^2-1.5}) node[left]{$F(x)$};

% plot tangent line
\node[violet,right=0.2cm] at (8,4.9) {\large tangent};

\draw[gray,thin,dotted] (8,0) -- (8,4.9) node[circle,fill,inner sep=2pt]{};
\draw[dashed, violet,name path=Tfunction]  plot[smooth,domain=4.25:9.5] (\x, {1.6*\x-7.9});

% x-axis labels
\draw (8,0.1) -- (8,-0.1) node[below] {$x^{(k)}$};
\draw [name intersections={of=Tfunction and xaxis}] ($(intersection-1)+(0,0.1)$) -- ++(0,-0.2) node[below,fill=white] {$x^{(k+1)}$} ;




enter image description here

I used the library intersections to get the coordinates of the intersection between the tangent and the x-axis lines. To this end, I saved both paths using name path=xaxis and name path=Tfunction for x-axis line and tangent line, respectively.

The function corresponds to 0.1*x^2-1.5.


With pgfplots and pgfplotstable:

enter image description here

\documentclass[margin=5pt, varwidth]{standalone}

% Input 1/2 =====

\tikzset{trig format=rad, 
declare function={
% Input 2/2 =====
f(\x)=exp(0.9*\x) -\x*\x;  
% Calc ====
df(\x)=( f(\x+dx) -f(\x) )/dx;

% Start row
\pgfplotstableread[header=false, col sep=comma,
0, \xStart, \fxnStart, \dfxnStart,  \xNewStart

% Further rows
\pgfplotsforeachungrouped \n in {1,...,\Steps} {%%
\ifnum\n=1 \pgfplotstablegetelem{0}{[index]4}\of\newtontable \else
\pgfplotstablegetelem{0}{[index]4}\of\nextrow \fi
col sep=comma,      row sep=crcr, 
\n,   \xOld,   \fxn, \dfxn, \xNew \noexpand\\
% Concatenate in loop
%\n \pgfplotstabletypeset{\temprow} \\ % Show for test
% Concatenate with startrow

% Output =============================

\savebox\ExampleText{% ======================
% Title =======
$f(x) = \fxshow   \\[1em]
f'(x)\approx \dfrac{f(x+\Delta x)-f(x)}{\Delta x},~~\Delta x=\dx \\[0.5em]
t_n(x) = f'(x_n)\cdot (x-x_n)+f(x_n) \\[0.5em]
x_0=\xStart,~~    x_{n+1}=x_n-\dfrac{f(x_n)}{f'(x_n)}    $  \\[0.5em]
%Table =======
\pgfplotstabletypeset[column type=r, 
% Show integers as intgers and general number format:
every column/.style={postproc cell content/.style={
@cell content=\pgfmathifisint{##1}
        {\pgfmathprintnumber[fixed,  fixed zerofill,  precision=5]{##1}}  
display columns/0/.style={column name=$n$},
display columns/1/.style={column name=$x_n$},
display columns/2/.style={column name=$f(x_n)$},
display columns/3/.style={column name=$f'(x_n)$},
display columns/4/.style={column name=$x_{n+1}$},
every head row/.style={after row=\hline, before row=\hline},
every last row/.style={after row=\hline},
]{\newtontable} \\[0.5em]
{$\Rightarrow~ \boldsymbol{ x  \approx\xNew}$  }
%\usebox{\ExampleText} % Show for test

% Curve =============================
\begin{axis}[local bounding box=Curve,
title style={align=left, anchor=south west, 
draw=none, text width=\mywidth, 
at={(rel axis cs:0,1)},   name=Example, 
trig format=rad, 
axis lines = center,
axis line style = {-latex},
xlabel style={anchor=north},
ylabel style={anchor=east},
xmin=-3,      xmax=3,
%ymin=-0.5,     ymax=3.7,
%minor ytick={-0.5,0,...,3.5},
%legend pos=outer north east,
legend style={at={(0.0,-0.05)},anchor=north west},
legend cell align=left,
enlarge y limits=upper,
enlarge x limits,
% Curve
\addplot[thick, domain=-1.5:3, blue]{f(x)}; 
% Tangents
\foreach \row in {0,...,\Steps}{%%
\pgfmathsetmacro\xSshow{\xS<0 ? \xS : "+\xS"}
\pgfmathsetmacro\ySshow{\yS<0 ? \yS : "+\yS"}
\pgfmathsetmacro\Pos{\row==3 || \row==999 ? -0.05 : 1.05}

\noexpand\addplot[red, domain=\xS-\vL:\xS+\vR, forget plot]{\dyS*(x-\xS)+\yS} node[pos=\Pos]{$t_\Index$}; 
\noexpand\addplot[red, mark=*, mark size=1.5pt, mark options={fill=white, draw=black}] coordinates{(\xS,\yS) };
\noexpand\addlegendentry[]{$t_\Index(x)=\dyS\cdot (x \xSshow) \ySshow$}
\noexpand\addplot[densely dashed, forget plot] coordinates{(\xS,\yS) (\xS,0)} node[below]{$x_\Index$};

% Zero of Curve
\addplot[mark=*, mark size=1.75pt, forget plot] coordinates{(\xRes,\yRes)};
  • If this answer was your master thesis, it wouldn't be more scientific and prolific :) – Diaa Oct 22 '20 at 15:44

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.