TransWikia.com

Drawing a graph with two functions, include the cosine function

TeX - LaTeX Asked on February 17, 2021

First, I would like this plot to be in radians. I want a plot of two functions. $y = cos x$ and
$y = 1 - cos x$ for $0 leq x leq pi $.

I wrote the following LaTex code:

begin{tikzpicture}
begin{axis}[domain=0:1,legend pos=outer north east]
addplot {cos( x * pi))}; 
addplot {1 - cos( x * pi))}; 
addlegendentry{$ cos( x * pi)) $} 
addlegendentry{$ 1 -  cos( x * pi)) $} 
end{axis}
end{tikzpicture}

For the first curve, I got a straight line similar to $y 0 1$. For the second curve, I got
a straight line similar to $y = 0$. What did I do wrong?

One Answer

If domain is given in radians, you need to convert function argument to radians with axis option trig format=rad:

documentclass[margin=3mm]{standalone}
usepackage{pgfplots}

begin{document}
    begin{tikzpicture}
begin{axis}[grid,
             legend pos=outer north east,
             trig format=rad,
             domain=0:1,
             xtick={0,0.25,0.5,0.75,1},
             xticklabels={0,$pi/4$,$pi/2$,$3pi/4$,$pi$},
             no marks]
addplot {cos(pi*x)};
addplot {1 - cos(pi*x)};
    legend{$cos(x)$, $1 -  cos(x)$}
end{axis}
    end{tikzpicture}
end{document}

enter image description here

Correct answer by Zarko on February 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP