You can use pgfplots for this. I've written a new macro \derivative{<pgfmath function>}{<label>}{<x1>}{<x2>}{<point legend>}
that can be used inside a pgfplots axis. It draws a plot of the function, which is specified using pgfmath
syntax as a function of \x
, and a straight line connecting the function points at two specified x
values. It also adds a label to the plot, which is specified using the second argument, and a legend listing the two points, which has to be supplied in the fifth argument. You can influence the legend position and appearance using point legend/.style={<options>}
in the axis
options, and the position and appearance of the point labels using point 1/.style
and point 2/.style
.
For example
\begin{axis}[
azetinaplot,
domain=-0.5:2.5, samples=100,
xmin=-1, xmax=3,
point legend/.style={ at={(rel axis cs:1.1,0.5)}},
point 1/.style={anchor=south east}, point 2/.style={anchor=south east}
]
\derivative{\x^2}{$f(x)=x^2$}{1}{2}{$P_1=(1\,,\,1)$\\$P_2=(2\,,\,4)$}
\end{axis}
would yield
The azetinaplot
style is defined here as
\pgfplotsset{
azetinaplot/.style={
width=7cm,
height=7cm,
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlarge y limits,
clip=false
}
}
Another example:
\begin{axis}[
azetinaplot,
domain=-20:370, samples=100,
xmin=-20, xmax=370,
point 1/.append style={anchor=south east},
point 2/.append style={anchor=east}]
\derivative{sin(\x)}
{$f(x)=\sin(x)$}
{290}{340}
{$P_1=(290\,,\,-0.94)$\\$P_2=(340\,,\,-0.34)$}
\end{axis}
would yield
Here's the complete code:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\pgfplotsset{point legend/.style={},
point 1/.style={anchor=south},
point 2/.style={anchor=south}
}
\newcommand{\derivative}[5]{
\begin{scope}[declare function={f(\x)=#1;}]
\addplot [thick, red, latex-latex] {f(x)} node [anchor=west] {#2};
\addplot [black, mark=*] coordinates {(#3,{f(#3)}) (#4,{f(#4)})}
node [pos=0,/pgfplots/point 1] {$P_1$}
node [pos=1,/pgfplots/point 2] {$P_2$};
\pgfplotsextra{
\pgfmathsetmacro\first{f(#3)}
\pgfmathsetmacro\second{(f(#4)}
\pgfmathsetmacro\xdiff{#4-#3}
\pgfmathsetmacro\ydiff{f(#4)-f(#3)}
\draw (axis cs:#3,\first) -| (axis cs:#4,\second);
\draw [|-|,yshift=-2ex] (axis cs:#3,\first) -- node [inner sep=1pt,fill=white] {\pgfmathprintnumber{\xdiff}} (axis cs:#4,\first);
\draw [|-|,xshift=2ex] (axis cs:#4,\first) -- node [inner sep=1pt, fill=white] {\pgfmathprintnumber{\ydiff}} (axis cs:#4,\second);
\matrix at (rel axis cs:1,1) [matrix of nodes,/pgfplots/point legend] {#5\\};
}
\end{scope}
}
\pgfplotsset{
azetinaplot/.style={
width=7cm,
height=7cm,
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlarge y limits,
clip=false
}
}
\begin{tikzpicture}
\begin{axis}[
azetinaplot,
domain=-20:370, samples=100,
xmin=-20, xmax=370,
point 1/.append style={anchor=south east},
point 2/.append style={anchor=east}]
\derivative{sin(\x)}
{$f(x)=\sin(x)$}
{290}{340}
{$P_1=(290\,,\,-0.94)$\\$P_2=(340\,,\,-0.34)$}
\end{axis}
\end{tikzpicture}
\end{document}
I don't really get the question so I hope this is what you wanted. If you include a full document (such that we copy paste and see the problem on our systems) things are much more easier.
Here, you can change the default setting within a scope but your block
style had a node distance
which was resetting every time it is issued. I've made it 2mm such that we can see the difference easier.
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}[decision/.style={diamond, draw, text width=4.5em, text badly centered, node distance=3.5cm, inner sep=0pt},
block/.style ={rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em, minimum height=2em},
cloud/.style ={draw, ellipse, minimum height=2em},
line/.style ={draw,-latex'},
node distance = 1cm,
auto]
\node [block] (1st) {1st};
\node [block, right= of 1st] (2nd1) {2nd1};
\begin{scope}[node distance=2mm and 10mm]%Here we change it for everything inside this scope
\node [block, above= of 2nd1] (2nd2) {2nd2};
\node [block, below= of 2nd1] (2nd3) {2nd3};
\node [block, right= of 2nd1] (3rd1) {3rd1};
\node [block, above= of 3rd1] (3rd2) {3rd2};
\node [block, above= of 3rd2] (3rd3) {3rd3};
\end{scope}
\node [block, below= of 3rd1] (3rd4) {3rd4};
\node [block, below= of 3rd4] (3rd5) {3rd5};
\path [line] (1st) -- (2nd1);
\path [line] (2nd1) -- (2nd2);
\path [line] (2nd1) -- (2nd3);
\path [line] (2nd2) -- (3rd3);
\path [line] (2nd1) -- (3rd1);
\path [line] (1st) -- (2nd1);
\end{tikzpicture}
\end{document}
Best Answer
A quick try (change the text and scale as you need by only changing
\ut
):