6

I want to make a parametric plot using pgfplot in LaTex. My equations are

\rho= K (sinh t-t)
P= 1/3 K (sinh t - 8 sinh (1/2) t +3t) with K=1\pi

I have written the LaTex code for equations , now please help me plot the graph

\documentclass[12pt,a4paper]{article}
\usepackage[a4paper, total={6in, 8in}]{geometry}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{fourier}
    \usepackage{mathtools}
\usepackage{amsmath}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{marginnote}
\begin {document}

\underline {Chandrashekhar EOS (1935)} : \\
\begin{align*}
\overline \rho &= K ~\Big( \sinh ~t ~- ~t\Big)\\
\overline P &= \frac {1}{3} K ~ \Big(\sinh~t -~ 8~ \sinh \frac {1}{2} t  +~ 3t\Big)\\
 K &= \frac {1}{4\pi}\\
\end {align*}
\end {document}
  • You should have a look in the pgfplots manual, specifically section 4.3.3 on page 51 of the v1.10 manual. You also appear to have some differences between the formula in the LaTeX code and the first one... specifically, K=1/pi and K=1/(4*pi) – darthbith Aug 7 '14 at 13:33
  • Don't use ~ in math mode; the spacing you get is wrong. – egreg Aug 7 '14 at 17:04
3

Another fun with PSTricks. It really uses the parametric approach in question.

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot,pst-math,amsmath}
\usepackage[nomessages]{fp}

\FPeval\XMin{0-6}
\FPeval\XMax{7}
\FPeval\YMin{0-6}
\FPeval\YMax{6}

\FPeval\XOL{0-1/3} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-1/3} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

%\FPset\TrigLabelBase{3}
\FPeval\DeltaX{1}
\FPeval\DeltaY{1}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=12cm


\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    %xtrigLabels,
    %ytrigLabels,
    %trigLabelBase=\TrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
    yMaxValue=\YMax,
    yMinValue=\YMin,
}

\pstVerb{/K {.25 Pi div} def}
\def\x{t}
\def\yp{K*(SINH(t)-t)}
\def\yP{K/3*(SINH(t)-8*SINH(t/2)+3*t)}




\begin{document}
\pslegend[bt](80,0)
{
    \color{NavyBlue}\rule{12pt}{3pt} & \color{NavyBlue} $\overline{p} = K (\sinh t -t) $ \\
    \color{Maroon}\rule{12pt}{3pt}   & \color{Maroon} $\overline{P} = \tfrac{1}{3} K (\sinh t -8 \sinh (\tfrac{1}{2}t)+3t) $ \\
                                     & where $K=\frac{1}{4\pi}$
}
\begin{psgraph}
    [
        dx=\DeltaX,
        dy=\DeltaY,
        Dy=\DeltaY,
        Dx=\DeltaX,
        linecolor=gray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}
    \psaxes
    [
        dx=\DeltaX,
        dy=\DeltaY,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Red!30,
        subtickcolor=ForestGreen!30,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
    ](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psparametricplot[linecolor=NavyBlue]{\XMin}{7}{\x|\yp}
    \psparametricplot[linecolor=Maroon]{\XMin}{7}{\x|\yP}
\end{psgraph}
\end{document}

enter image description here

Notes for ShareLaTeX users

Go to upper left menu, and you will see the following. Change as pointed by the red arrow. And don't forget to recompile.

enter image description here

The lastest update based on OP's comment

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{pst-plot,pst-math,amsmath}
\usepackage[nomessages]{fp}

% Be careful, fp has not defined 
% the unary negate so we have to write 0-8 to represent -8 for example.

\FPeval\XMin{0-0}
\FPeval\XMax{7}
\FPeval\YMin{0-0}
\FPeval\YMax{6}

\FPeval\XOL{0-0} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-0} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

%\FPset\TrigLabelBase{3}
\FPeval\DeltaX{1}
\FPeval\DeltaY{1}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=12cm


\newlength\llx\llx=-20pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-20pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    %xtrigLabels,
    %ytrigLabels,
    %trigLabelBase=\TrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
    yMaxValue=\YMax,
    yMinValue=\YMin,
}

\pstVerb{/K {.25 Pi div} def}
\def\x{t}
\def\yp{K*(SINH(t)-t)}
\def\yP{K/3*(SINH(t)-8*SINH(t/2)+3*t)}




\begin{document}
\pslegend[rt]%(80,0)
{
    \color{NavyBlue}\rule{12pt}{3pt} & \color{NavyBlue} $\overline{p} = K (\sinh t -t) $ \\
    \color{Maroon}\rule{12pt}{3pt} & \color{Maroon} $\overline{P} = \tfrac{1}{3} K (\sinh t -8 \sinh (\tfrac{1}{2}t)+3t) $ \\
    & where $K=\frac{1}{4\pi}$
}
\begin{psgraph}
    [
        dx=\DeltaX,
        dy=\DeltaY,
        Dy=\DeltaY,
        Dx=\DeltaX,
        linecolor=gray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}
    \psaxes
    [
        dx=\DeltaX,
        dy=\DeltaY,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Red!30,
        subtickcolor=ForestGreen!30,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
    ](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psparametricplot[linecolor=NavyBlue]{\XMin}{7}{\x|\yp}
    \psparametricplot[linecolor=Maroon]{\XMin}{7}{\x|\yP}
\end{psgraph}
\end{document}

enter image description here

  • he wants a plot with K=1/π – Enthusiastic Engineer Aug 7 '14 at 15:21
  • thanks all for your nice effort, but I am using sharelatex.com to type my documents. is there any way I can set the graph in my document directly coding in sharelatex? – Tazkera Haque Trina Aug 8 '14 at 5:11
  • 1
    @TazkeraHaqueTrina I have no experience to typeset at that site, but; as you can run the codes I provided in my answer in-line with your other codes and documents and you need no further package run or special permeable; so I do not think you have any special issue using them. Copy my second part of answer which is edited to work in pdflatex (not the lua part) and render your file. I don't think you have any issue. – Enthusiastic Engineer Aug 8 '14 at 7:28
  • the last thing to know, what changes should I make to the code to transfer the axis from center to left margin, I really need to show the +x and +y axis there, not the negative axes. thanks – Tazkera Haque Trina Aug 8 '14 at 8:27
  • 1
    @TazkeraHaqueTrina See the update to my answer too. – Enthusiastic Engineer Aug 8 '14 at 11:03
7

Note: Other answers to this question may use other ks for plotting (eg. K=1/4π). In my answer, I have used question's desired k value which is K=1/π. That is why there may be a slight difference in the plots.


You can plot your functions with pgfplots. Run the following code with LuaLaTeX, I have used it to avoid probable numerical inaccuracies.

% lualatex
\documentclass{standalone}
    \usepackage{tikz}
    \usepackage{pgfplots}
        \pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\directlua{
    R= function(x,k)
    return k*(math.sinh(x)-x)
    end

    P = function(x,k)
    return k*(math.sinh(x)- 8*math.sinh(x/2)+3*x)/3
    end
        }
        \pgfmathdeclarefunction{R}{2}{%
            \edef\pgfmathresult{\directlua{tex.print(R(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}}%
        }%
        \pgfmathdeclarefunction{P}{2}{%
            \edef\pgfmathresult{\directlua{tex.print(P(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}}%
        }%
\begin{axis}
        [
    axis lines = center,
    grid=both,minor tick num=1,
    xlabel=$x$,ylabel=$y$,
    tick align=inside,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=1},
        legend entries={${\rho}= K\Big(\sinh t-t\Big)$\\$P= \frac{1}{3}K\Big(\sinh t-8\sinh\frac {1}{2} t  + 3t\Big)$\\},
        domain=-6:6,
        samples=60,
        ]
        \addplot [solid, thick] {R(x,1/pi)};
       \addplot [dashed, thick] {P(x,1/pi)};
\end{axis}
\end{tikzpicture}
\end{document}

and here is the output: enter image description here

P.S. Other answers exists for plotting parametric/complicated functions in this question: How to plot a complicated multi-variable function in a tex document automatically, not just importing an image


update: If you don't want to use the LuaLaTeX, here is another version of my answer which is suggested in this comment:

% pdflatex
\documentclass{standalone}
    \usepackage{tikz}
    \usepackage{pgfplots}
        \pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
       \pgfmathdeclarefunction{R}{2}{\pgfmathparse{#2 * (sinh(#1) - #1)}}
       \pgfmathdeclarefunction{P}{2}{\pgfmathparse{#2 * (sinh(#1) - 8*sinh(#1/2) + 3*#1)/3}}
\begin{axis}
        [
    axis lines = center,
    grid=both,minor tick num=1,
    xlabel=$x$,ylabel=$y$,
    tick align=inside,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=1},
        legend entries={${\rho}= K\Big(\sinh t-t\Big)$\\$P= \frac{1}{3}K\Big(\sinh t-8\sinh\frac {1}{2} t  + 3t\Big)$\\},
        domain=0:10,
        samples=60,
        ]
        \addplot [solid, thick] {R(x,1/pi)};
       \addplot [dashed, thick] {P(x,1/pi)};
\end{axis}
\end{tikzpicture}
\end{document}

Here is the output (it is optimized to show the positive axis only, change the domain to any value you need):

enter image description here

  • Good answer! Note that ` \pgfmathdeclarefunction{R}{2}{\pgfmathparse{#2 * (sinh(#1) - #1)}}` and \pgfmathdeclarefunction{P}{2}{\pgfmathparse{#2 * (sinh(#1) - 8*sinh(#1/2) + 3*#1)/3}} would also do the job - the PGF math engine can compute it with good precision. – Christian Feuersänger Aug 7 '14 at 16:12
  • @ChristianFeuersänger Thanks... I did not know that. I also updated my answer to include that point. – Enthusiastic Engineer Aug 7 '14 at 16:42
4

A solution with PSTricks, run with xelatex. The parametricplot P(t)=f(rho(t)) seems to be a line:

\documentclass{article}
\usepackage{mathtools}
\usepackage{pst-plot,pst-math}
\begin {document}

\begin{align*}
\overline{\rho}&= K\left(\sinh t-t\right)\\
\overline{P}   &= \frac{1}{3}K\left(\sinh t-8\sinh\frac{1}{2}t+ 3t\right)\\
             K &= \frac{1}{4\pi}
\end {align*}

\psset{llx=-15mm,lly=-5mm,urx=5mm,ury=5mm,algebraic,yMaxValue=7,
  plotpoints=500,xAxisLabel=$t$,yAxisLabel={},ticks=none}
\begin{psgraph}[labelFontSize=\scriptstyle,psgrid,gridcoor={(7,7)}]{->}(0,0)(7,7){10cm}{10cm}
\pstVerb{ /K 1 4 PI mul div def }
\psplot[linecolor=blue,arrows=-]{0}{7}{ K*(SINH(x)-x) }
\psplot[linecolor=red,arrows=-]{0}{7}{ K/3*(SINH(x)-8*SINH(x/2)+3*x) }
\psparametricplot[linewidth=1.5pt,arrows=-]{0}{5.2}{ K*(SINH(t)-t) | K/3*(SINH(t)-8*SINH(t/2)+3*t) }
\rput[rb](4.5,4.5){\blue$\overline{\rho}=K\left(\sinh t-t\right)$}
\rput[lb](0.5,2.5){\red$\overline{P}=\frac{1}{3}K\left(\sinh t-8\sinh\frac{1}{2}t+3t\right)$}
\uput[0](0,7){$\overline{P}$}\uput[180](0,7){$\overline{\rho}$}
\uput*[90](1.5,0.5){$\overline{P}=f(\overline{\rho})$}
\end{psgraph}

\end{document}

enter image description here

For sharelatex copy this one into a new projekt:

\documentclass{article}
\usepackage{pst-plot,pst-math}
\begin {document}

\psset{llx=-15mm,lly=-5mm,urx=5mm,ury=5mm,algebraic,yMaxValue=7,
  plotpoints=500,xAxisLabel=$t$,yAxisLabel={}}
\begin{psgraph}[labelFontSize=\scriptstyle]{->}(0,0)(7,7){10cm}{10cm}
\pstVerb{ /K 1 4 PI mul div def }
\psplot[linecolor=blue,arrows=-]{0}{7}{ K*(SINH(x)-x) }
\psplot[linecolor=red,arrows=-]{0}{7}{ K/3*(SINH(x)-8*SINH(x/2)+3*x) }
\psparametricplot[linewidth=1.5pt,arrows=-]{0}{5.2}{ K*(SINH(t)-t) | K/3*(SINH(t)-8*SINH(t/2)+3*t) }
\rput[rb](4.5,4.5){\blue$\overline{\rho}=K\left(\sinh t-t\right)$}
\rput[lb](0.5,2.5){\red$\overline{P}=\frac{1}{3}K\left(\sinh t-8\sinh\frac{1}{2}t+3t\right)$}
\uput[0](0,7){$\overline{P}$}\uput[180](0,7){$\overline{\rho}$}
\uput*[90](1.5,0.5){$\overline{P}=f(\overline{\rho})$}
\end{psgraph}

\end{document}

Then choose from the menu (top left) the compiler xelatex. However, sharelatex is not a good choice it doesn't update the TeX distribution.

  • How can i get this graph in sharelatex.com , as I was typing my document there, also It would have been better if you take the axis to the left margin instead of center. thanks for your nice effort – Tazkera Haque Trina Aug 8 '14 at 5:15
  • 1
    See my edited answer. At the end is a version for sharelatex. – user2478 Aug 8 '14 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.