2

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?

avincigu
  • 31
  • 2

2 Answers2

2

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.

enter image description here

\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}
Black Mild
  • 17,569
  • 2
    Hi! Thank you for your suggestion but I think my question was maybe not written well enough. x is a function of t in my setting. I edited it so that it was more comprehensible. I managed to solve my issue by solving the ODE using scipy, computing the values of p, and then using the data to plot p in my latex file. Still, I don't know if there is a solution to do all of this in a latex file – avincigu Jul 27 '22 at 14:42
  • 1
    There is, with pkg pst-ode for example. – AlexG Jul 27 '22 at 15:32
  • @avincigu I misread your interesting question. Is there an initial condition of the differential equation? – Black Mild Jul 27 '22 at 19:14
  • Here is a way with TikZ https://tex.stackexchange.com/a/471743/140722 – Black Mild Jul 27 '22 at 19:55
  • 1
    Since scipy is part of Sage this should be easy with the sagetex package: just calculate and force the data through tikz. I have numerous examples of plotting data through tikz, such as the Cantor function. If you want help, just give your code for scipy, the values of your constants, and the picture of the resulting plot so I can make sure I haven't made any mistakes. – DJP Jul 27 '22 at 21:24
  • Thanks for your answer! I tried 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
  • @avincigu : pst-ode example added. – AlexG Aug 03 '22 at 13:22
2

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.

enter image description here

\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}

AlexG
  • 54,894