2

I would like to plot the following differential equation :

on LaTeX, I have trouble to understand what I need to do for it to work (I know that there have been posts on it but I don't get it). Can someone please tell me which package(s) to use and give me a simple example to do so please?

It is to study the speed of a projectile in the gravitational field with air friction. I know all the initial conditions.

Black Mild
  • 17,569
Axel
  • 189
  • 6
  • 2
    Examples how to solve differential equation with LaTeX can be found e.g. here. Whether or not they satisfy your criterion of simplicity, I do not know, but you should not forget that LaTeX is not a computer algebra system. –  Apr 30 '20 at 20:02
  • Thank you for the link, I am going to try again, I was hoping having something more simple to start with. I know, I just really want to try on LaTeX because my teachers told me it was possible. – Axel Apr 30 '20 at 20:07
  • There is also the pst-ode package. It is simple to use, perhaps a bit less simple to compile. –  Apr 30 '20 at 20:09
  • Yes, I've seen that, it seemed quite simple to use but it didn't want to compile... So I gave up on that package. Thank you for suggesting it by the way. – Axel Apr 30 '20 at 20:12
  • You can compile it, just not with pdflatex or xelatex. You can compile it with latex, convert the dvi to pdf, or use xelatex. However, sadly the results of both methods may not be the same. –  Apr 30 '20 at 20:15
  • My goal was to find something simple where I put my differential equation and initial conditions and that gives me just an input of the solution (a pdf will be just fine) I just don't want to have a png file. For example, I don't want any of the slope fields. – Axel Apr 30 '20 at 20:16
  • Ok, thank you I will try to check that. – Axel Apr 30 '20 at 20:17
  • Could you please give the values for k and mm and the initial velocity? Which integration interval do you need (t_0, t_e)? – AlexG Apr 30 '20 at 20:22
  • Yes k = 1.15 * 10-3 and m = 58*10^-3 – Axel Apr 30 '20 at 20:23
  • v_0, t_0, t_e? – AlexG Apr 30 '20 at 20:25
  • 1
    initial time t_0 = 0s, end time t_e = 10, v_0 = 10, thank you AlexG – Axel Apr 30 '20 at 20:26
  • If anyone could give me please a script-example with my differential equation or at least first order, with any initial condition, it will be highly appreciated! – Axel Apr 30 '20 at 21:02
  • I think the question is interesting with a clear description. Here the friction is directly proportional to the square of its instantaneous speed, compare with https://farside.ph.utexas.edu/teaching/336k/Newtonhtml/node29.html I have just added tag tikz-pgf and asymptote to get more attention – Black Mild Sep 02 '22 at 16:05

1 Answers1

2

This example uses PSTricks package pst-ode to solve the differential equation numerically with the RKF45 method.

PSTricks requires the latex-->dvips-->ps2pdf workflow for typesetting. Since we use pgfplots here, solving the ODE with command \pstODEsolve is outsourced into an auxiliary document and the solution is written to file table.dat. Therefore, in order to typeset the code listed below, run pdflatex or lualatex with option --shell-escape.

Note that with RKF45, precision of the result does not depend on the chosen number of output points. The method uses adaptive step size control. For comparison, the solution with only 4 output points (t_0, t_1, t_2, t_e) is plotted against the fine-grained solution with 250 output points.

Typeset with pdflatex --shell-escape example.tex.


example.tex:

\documentclass{standalone}

%%%%%%%%%%%%%%%%%%%%%%% solve ODE in auxiliary document %%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents}[overwrite]{solve.tex}
\documentclass{article}
\usepackage{pst-ode}

\begin{document} 
% arguments:
%   algebraicAll --> all arguments in algebraic notation
%   saveData     --> also write result into file `table.dat'
%   `table'      --> PostScript variable that takes result
%   t | x[0]     --> output format in `table' and `table.dat'
%   0, 10        --> integration interval t_0, t_e
%   250          --> number of saved output points t_0, t_1, ..., t_e
%   10           --> initial value
%   9.81 - ...   --> right-hand side of ODE 
\pstODEsolve[algebraicAll,saveData]{table}{ t | y[0] }{ 0 }{ 10 }{ 250 }{ 10 }{ 
  9.81 - 1.15*10^-3 / (58*10^-3) * y[0]^2
}
% for comparison: 4 output points
\pstODEsolve[algebraicAll,saveData]{table2}{ t | y[0] }{ 0 }{ 10 }{ 4 }{ 10 }{ 
  9.81 - 1.15*10^-3 / (58*10^-3) * y[0]^2
}
dummy text
\end{document}
\end{filecontents}

\immediate\write18{latex solve}
\immediate\write18{dvips solve}
\immediate\write18{ps2pdf -dNOSAFER solve.ps}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xlabel=$t$,
    ylabel=$v$,
    ylabel style={rotate=-90}
  ]
  \addplot [thin,black] table {table2.dat}; % solution with 4 output points
  \addplot [blue] table {table.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here


Alternatively, without --shell-escape, the auxiliary document solve.tex can by typeset manually before typesetting the main document example.tex:

latex solve
dvips solve
ps2pdf -dNOSAFER solve.ps
pdflatex example
pdflatex example

ps2pdf must be run with option -dNOSAFER in order to allow it to write files (table.dat).

AlexG
  • 54,894
  • That's great, thank you a lot for taking your time to answer!! I am going to follow your instructions, and will validate your answer afterwards. – Axel May 01 '20 at 05:22
  • How do you set --shell-escape please? I am using MikTeX so I can choose between PdfLaTex or LuaLaTeX as you told me but I am not sure about how to set this option. – Axel May 01 '20 at 05:30
  • I am running pdflatex on the command line in a terminal. If you run it from within an editor, go to the editor settings, find the command line settings for TeX commands, and add --shell-escape. Where to do this is editor-specific. Look into the editor manual. In MiKTeX, you can also open a terminal and run pdflatex --shell-escape myfile.tex on the command line. – AlexG May 01 '20 at 05:43
  • Ok, I have finally suceeded (I think) using --shell-escape with the terminal, but it tells me that: `! LaTeX Error: Missing \begin{document}.

    See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

    l.4 \begin{filecontents}[o verwrite]{solve.tex}`

    – Axel May 01 '20 at 05:53
  • That's strange. Did you copy-paste the code above correctly? – AlexG May 01 '20 at 05:55
  • It is indeed. Yes I have copied-pasted all of it and renamed it correctly. Maybe I should save my tex files at a specific place in my computer? – Axel May 01 '20 at 05:59
  • I am going to try again! – Axel May 01 '20 at 06:06
  • You could also compile solve.tex manually. See bottom of edited answer. – AlexG May 01 '20 at 06:09
  • Well, when I execute what you gave me I have a pdf with the axis but no plot. But most importantly it produces a file with name [.tex, is that normal? – Axel May 01 '20 at 06:18