I have the following differential equation: $\frac{\mathrm{d}x}{\mathrm{d}t} = \alpha \frac{x}{x + c}$
And I have the function $p(t) = \frac{x(t)}{x(t)+c}$.
I want to plot the function p. How can I do that with TikZ?
I have the following differential equation: $\frac{\mathrm{d}x}{\mathrm{d}t} = \alpha \frac{x}{x + c}$
And I have the function $p(t) = \frac{x(t)}{x(t)+c}$.
I want to plot the function p. How can I do that with TikZ?
It is a basic of drawing. You can check TikZ/PGF documentation to see the syntax \draw plot. Also searching in this forum about graph and plot, etc.
Here is one way.
\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[declare function={c=1.2;cgap=.3;
xmin=c-4;xleft=c-cgap;
xmax=c+4;xright=c+cgap;
p(\x)=\x/(\x-c);}]
\draw[->] (xmin,0)--(xmax,0) node[below right]{$x$};
\draw[->] (0,{p(xleft)})--(0,{p(xright)}) node[below left]{$y$};
\draw[dashed] (xmin,1)--(xmax,1)
(c,{p(xleft)})--(c,{p(xright)});
\path (0,0) node[below left]{O} (c,0) node[below right]{$c$};
\draw[thick,magenta]
plot[domain=xmin:xleft] (\x,{p(\x)})
plot[domain=xright:xmax] (\x,{p(\x)}) node[above left=2mm]{$p(x)=\dfrac{x}{x-c}$};
\end{tikzpicture}
\end{document}
pst-ode but could not make it work. I will stick to my solution using Python and scipy.
– avincigu
Aug 02 '22 at 17:03
All within LaTeX, as requested: solving ODE and post-processing solution with \pstODEsolve from package pst-ode, plotting result with pgfplots.
Typeset twice with lualatex or latex + dvips + ps2pdf -dNOSAFER.
\documentclass[margin=1pt]{standalone}
\usepackage{pst-ode}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
% define some constants / initial condition
\pstVerb{
tx@Dict begin
/alpha 1.0 def
/const 1.0 def
/xinit 1.0 def
end
}
\pstODEsolve[algebraicAll,saveData]{x_p_vs_t}{ % result saved in file x_p_vs_t.dat
t | x[0] | x[0]/(x[0]+const) % result table (post-processing ODE solution): t, x(t), p(t)
}{0}{10}{101}{ % t_0 = 0, t_max = 10, 101 output points
xinit % initial condition x(t=0)
}{
alpha*x[0]/(x[0]+const) % ODE RHS
}
\begin{document}
\IfFileExists{x_p_vs_t.dat}{}{dummy text\end{document}}
\begin{tikzpicture}
\begin{axis}[axis y line=left, xlabel={$t$},ylabel={\color{blue}$p(t)$}]
\addplot [no markers, blue] table [x index=0, y index=2] {x_p_vs_t.dat};
\end{axis}
\begin{axis}[axis x line=none, axis y line=right, xlabel={$t$}, ylabel={$x(t)$}, ytick distance=1, ymin=0.5, ymax=9.8]
\addplot [no markers] table [x index=0, y index=1] {x_p_vs_t.dat};
\end{axis}
\end{tikzpicture}
\end{document}