tzplot-Paket: Probleme bei der Flächenberechnung

tzplot-Paket: Probleme bei der Flächenberechnung

tzplotIch habe angefangen , Paket anstelle von Paket zu verwenden tikz. Es gibt viele nützliche Abkürzungen. Aber jetzt habe ich ein Problem (vielleicht ist es mein Fehler). Aus diesem Code:

\documentclass[12pt,a4paper]{article}
\usepackage{tzplot}
\def \N {50} % <-- change to higher/lower number if You want a more/less accurate shading
\begin{document}
    \begin{tikzpicture}[font=\scriptsize,scale=1.5]
        % axex, grid and ticklabels
        \tzhelplines[thick](-2,-4)(2,2)
        \tzshoworigin
        \tzaxes(-2,-4)(2,2)
        \tzticks{-2,-1,,1,2} % x-ticks
        {-4,-3,-2,-1,1,2} % y-ticks
        % plotting
        \clip (-2,-4) rectangle (2,2);
        \def\Fx{.3*(\x)^3-2} % <-- def cubic (F)
        \tzfn\Fx[-2:2]       % <-- naming the path F
        \def\Gx{-2*(\x)^2+2} % <-- def parabola (G)
        \tzfn\Gx[-2:2]       % <-- naming the path G
        \tzXpoint*{Fx}{Gx}(P){$A$}[0]{2pt}
        \tzdot(P-2){$B$}[0]{2pt}  
        \tzgetxyval(P){\Px}{\Py}% <-- coordinate of first point of intersection between F and G
        \tzgetxyval(P-2){\Qx}{\Qy}% <-- coordinate of second point of intersection between F and G
        \pgfmathsetmacro{\dx}{(\Qx-\Px)/\N};
        \def \t {0}; % <-- initial Riemann area
        \foreach \n in {0,1,...,\N}{% <-- loop for shading area
            \pgfmathsetmacro{\x}{\Px+\n*\dx};
            \tzvXpointat{Fx}{\x}(R)%{$R_\n$}[b]
            \tzgetxyval(R){\Rx}{\Ry} % <-- IT SEEMS DONT WORK IN THIS CASE
            \tzvXpointat{Gx}{\x}(S)%{$S_\n$}[t]
            \tzgetxyval(S){\Sx}{\Sy} % <-- IT SEEMS DONT WORK IN THIS CASE
            \pgfmathsetmacro{\t}{\t+(\Sy-\Ry)*\dx} %<-- temporary Riemann area
            \draw[blue!50,opacity=.3] (R)--(S);
        }
        \tzfn[very thick,magenta]\Fx[-2:2]
        \tzfn[very thick,cyan]\Gx[-2:2]
        \draw (0,-3.5) node[fill=yellow,font=\ttfamily] {Area|,=\;\t}; % <-- final Riemann area
    \end{tikzpicture}
\end{document}

Ich habe diese Ausgabe:

Bildbeschreibung hier eingeben

Mit dem gewünschten Code: ...

  1. zeichnen Sie die beiden Funktionen auf;
  2. schattieren Sie den Bereich zwischen den beiden Funktionen (hierfür kann ich den Parameter \N setzen)
  3. Berechnen Sie einen Näherungswert (Riemann-Verfahren) der schattierten Fläche.

Bei den ersten beiden Punkten kein Problem. Beim dritten Punkt ist das Ergebnis immer Null (siehe im Bild die gelbe Zone). Wenn ich in derselben Zone versuche, nicht den Flächenwert, sondern einen der zuvor berechneten Werte \Rx, \Ry, \Sx, auszudrucken \Sy, treten Fehler auf. Ich denke also, dass die Probleme aus der Berechnung dieser Variablen resultieren. Irgendwelche Vorschläge? (Entschuldigen Sie mein Englisch)

Hinweis: Im Fall der Abbildung muss der Wert der schattierten Fläche 7,68 Flächeneinheiten entsprechen.

Hinweis 1: Vielleicht kann man eine Variable innerhalb der Schleife nicht außerhalb der Schleife verwenden! Aber ich finde keine Lösung ...

Antwort1

Ich denke, Ihre Berechnung ist völlig in Ordnung, aber Sie stoßen auf Gültigkeitsbereichsprobleme, da alles innerhalb der \foreachSchleife gültig ist. Sie müssen den berechneten Wert also mit \global\letoder etwas Ähnlichem außerhalb der Schleife verschieben:

\documentclass[12pt,a4paper]{article}
\usepackage{tzplot}
\def\N{50} % <-- change to higher/lower number if You want a more/less accurate shading
\begin{document}
    \begin{tikzpicture}[font=\scriptsize,scale=1.5]
        % axex, grid and ticklabels
        \tzhelplines[thick](-2,-4)(2,2)
        \tzshoworigin
        \tzaxes(-2,-4)(2,2)
        \tzticks{-2,-1,,1,2} % x-ticks
        {-4,-3,-2,-1,1,2} % y-ticks
        % plotting
        \clip (-2,-4) rectangle (2,2);
        \def\Fx{.3*(\x)^3-2} % <-- def cubic (F)
        \tzfn\Fx[-2:2]       % <-- naming the path F
        \def\Gx{-2*(\x)^2+2} % <-- def parabola (G)
        \tzfn\Gx[-2:2]       % <-- naming the path G
        \tzXpoint*{Fx}{Gx}(P){$A$}[0]
        \tzdot(P-2){$B$}[0]  
        \tzgetxyval(P){\Px}{\Py} % <-- coordinate of first point of intersection between F and G
        \tzgetxyval(P-2){\Qx}{\Qy} % <-- coordinate of second point of intersection between F and G
        \pgfmathsetmacro{\dx}{(\Qx-\Px)/\N}
        \def\gt{0} % <-- initial Riemann area (global variable)
        \def\t{0}  % <-- initial Riemann area (local variable inside loop)
        \foreach \n in {0,1,...,\N} { % <-- loop for shading area
            \pgfmathsetmacro{\x}{\Px+\n*\dx}
            \tzvXpointat{Fx}{\x}(R) % {$R_\n$}[b]
            \tzgetxyval(R){\Rx}{\Ry} 
            \tzvXpointat{Gx}{\x}(S) % {$S_\n$}[t]
            \tzgetxyval(S){\Sx}{\Sy} 
            \pgfmathsetmacro{\t}{\gt + (\Sy-\Ry)*\dx} % <-- temporary Riemann area
            \global\let\gt\t % <-- set global variable to new value
            \draw[blue!50,opacity=.3] (R)--(S);
        }
        \tzfn[very thick,magenta]\Fx[-2:2]
        \tzfn[very thick,cyan]\Gx[-2:2]
        \draw (0,-3.5) node[fill=yellow,font=\ttfamily] {Area\;=\;\gt}; % <-- final Riemann area
    \end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Übrigens wäre ich vorsichtig mit der Definition von Einzelbuchstaben-Makros \defauf Dokumentebene.

verwandte Informationen