使用 pgfplots 绘制微分方程方向图

使用 pgfplots 绘制微分方程方向图

在之前的帖子中,Jake 提供了一些代码,成功绘制了 [0; 1] 范围内的以下微分方程

dy/dx=2*x
dy/dx=x*sqrt(x)

看:如何在乳胶中绘制具有所有可能的解决方案曲线的斜率场

我无法确定如何将[-1; 1]两个维度的范围更改为。我的尝试

xmin=-1.1, xmax=1.1, % Axis limits
ymin=-1.1, ymax=1.1,
domain=-1:1, y domain=-1:1,

另外,我对由 y 组成的微分方程的符号遇到了一些困难,例如(dy/dx=x^2+y^2-1)

我的尝试:

declare function={f(\x) = \x^2 + f(\x)^2 - 1;}

答案1

使用 PGFPlotstable,您可以使用简单的数值积分方案直接在 LaTeX 中找到函数:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.8}

\usepackage{amsmath}

\pgfplotstableset{
    create on use/x/.style={
        create col/expr={
            \pgfplotstablerow/201*2-1
        }
    },
    create on use/y/.style={
        create col/expr accum={
            \pgfmathaccuma+(2/201)*(abs(\pgfmathaccuma^2)+abs(\thisrow{x}^2)-1)
        }{0.6}
    }
}


\pgfplotstablenew{201}\loadedtable

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    view={0}{90},
    domain=-1:1,
    y domain=-1:1,
    xmax=1, ymax=1,
    samples=21
]
\addplot3 [gray, quiver={u={1}, v={x^2+y^2-1}, scale arrows=0.075, every arrow/.append style={-latex}}] (x,y,0);
\addplot [thick, red] table [x=x, y=y] {\loadedtable};
\end{axis}
\end{tikzpicture}

\end{document} 

答案2

可以使用Plain-TeX / LaTeX 包\pstODEsolve对常微分方程 (ODE) 组进行数值求解。pst-ode

RKF45使用 Runge-Kutta-Fehlberg 方法 ( )对 ODE 进行积分。

运行 2 次lualatex或命令序列

latex myfile
dvips myfile
ps2pdf -dNOSAFER myfile.ps

在 TeX 输入上。两种方法都会在第一次运行时将结果表写入文本文件。第一种方法利用了luapstricksMarcel Krüger 最近编写的 PostScript 解释器。

\documentclass{article}

\usepackage{pst-ode}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%solve dy/dx=x^2 + y^2 - 1 numerically for different initial values of y in the
%interval x=[-1.1,1.1]; write resulting curves as tables with 100 output points
%into text files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%y0=-0.5 --> y0=-0.5.dat
\pstODEsolve[algebraicOutputFormat,algebraic,saveData]{%
  y0=-0.5%  %name of the output file `y0=-0.5.dat'
}{
  t | x[0]  %table format in `y0=-0.5.dat': x y
}{
  -1.1      %integration domain x_min=-1.1
}{
  1.1       %integration domain x_max=1.1
}{
  100       %number of output points
}{
  -0.5      %initial value y0(x_min)
}{
  t^2+x[0]^2-1  % right hand side of ODE, note the special notation:  x --> t, y --> x[0]
}

%y0=0.0 --> y0=0.0.dat
\pstODEsolve[algebraicOutputFormat,algebraic,saveData]{y0=0.0}{t | x[0]}{-1.1}{1.1}{100}{0.0}{t^2+x[0]^2-1}

%y0=0.5 --> y0=0.5.dat
\pstODEsolve[algebraicOutputFormat,algebraic,saveData]{y0=0.5}{t | x[0]}{-1.1}{1.1}{100}{0.5}{t^2+x[0]^2-1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\IfFileExists{y0=-0.5.dat}{}{dummy text\end{document}}

\begin{tikzpicture}
  \begin{axis}[
    axis equal image, % Unit vectors for both axes have the same length
    xmin=-1.1, xmax=1.1, % Axis limits
    ymin=-1.1, ymax=1.1,
    xtick={-1,-0.5,0,0.5,1}, ytick={-1,-0.5,0,0.5,1}, % Tick marks
    no markers,
    title={$\dfrac{\mathrm{d}y}{\mathrm{d}x}=x^2+y^2-1$},
    view={0}{90},samples=21,domain=-1.1:1.1, y domain=-1.1:1.1, %for direction field
  ]
  \addplot3 [gray, quiver={u={1}, v={x^2+y^2-1}, scale arrows=0.075, every arrow/.append style={-latex}}] (x,y,0);
  \addplot table {y0=-0.5.dat};
  \addplot table {y0=0.0.dat};
  \addplot table {y0=0.5.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

在此处输入图片描述

MWEAsymptote

% odeslope.tex:
%
\documentclass{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\centering
\begin{asy}
import graph;
import slopefield;
import fontsize;
defaultpen(fontsize(9pt));
size(200);
real dy(real x,real y) {return x^2+y^2-1;}
real xmin=-1, xmax=1;
real ymin=-0.2, ymax=1;

add(slopefield(dy,(xmin,ymin),(xmax,ymax),20,deepgreen+0.4bp,Arrow));

pair C=(0.5,0.4);
draw(curve(C,dy,(xmin,ymin),(xmax,ymax)),deepblue+1bp);

label("$C$",C,NE,UnFill);
dot(C,UnFill);

xaxis(YEquals(ymin),xmin,xmax,LeftTicks());
xaxis(YEquals(ymax),xmin,xmax);
yaxis(XEquals(xmin),ymin,ymax,RightTicks());
yaxis(XEquals(xmax),ymin,ymax);
\end{asy}
\caption{$\frac{\mathrm{d}y}{\mathrm{d}x}=x^2+y^2-1$}
\end{figure}
\end{document}
%
% Process:
%
% pdflatex odeslope.tex
% asy odeslope-*.asy
% pdflatex odeslope.tex

答案4

PSTricks 解决方案,可以运行,xelatex但需要花费大量时间latex-> dvips->ps2pdf

\documentclass[border=10pt]{standalone}
\usepackage{pst-plot,pst-ode}
\begin{document}

\psset{unit=3}
\begin{pspicture}(-1.2,-1.2)(1.1,1.1)
\psaxes[ticksize=0 4pt,axesstyle=frame,tickstyle=inner,subticks=20,
        Ox=-1,Oy=-1](-1,-1)(1,1)
\psset{arrows=->,algebraic}
\psVectorfield[linecolor=black!60](-0.9,-0.9)(0.9,0.9){ x^2+y^2-1 }
%y0_a=-0.5
\pstODEsolve[algebraicOutputFormat]{y0_a}{t | x[0]}{-1}{1}{100}{-0.5}{t^2+x[0]^2-1}
%y0_b=0.0
\pstODEsolve[algebraicOutputFormat]{y0_b}{t | x[0]}{-1}{1}{100}{0.0}{t^2+x[0]^2-1}
%y0_c=0.5
\pstODEsolve[algebraicOutputFormat]{y0_c}{t | x[0]}{-1}{1}{100}{0.5}{t^2+x[0]^2-1}

\psset{arrows=-,linewidth=1pt}%
\listplot[linecolor=red  ]{y0_a}
\listplot[linecolor=green]{y0_b}
\listplot[linecolor=blue ]{y0_c}
\end{pspicture}

\end{document}

在此处输入图片描述

相关内容