TransWikia.com

Order of operations in plot(x, {function})

TeX - LaTeX Asked by user2795243 on April 17, 2021

I draw a random function with plot (x, {function}):

begin{tikzpicture}[scale=1]
draw[thick,scale=1,domain=-0.83:10,samples=100,smooth,variable=x] plot(x,{(1/((x)+1))-cos((180/pi)*0.2*(x)^2)*0.5*x});
end{tikzpicture}

The output looks nice, however I don’t think that this is the function given in the expression because if I give the same function to Wolframalpha, it looks very different. I know for example that one must write (x)^2 instead of x^2 in plot to get the square function. Is there other similar rules that I am missing? Or maybe I did not copy the function correctly into Wolframalpha but I am quite sure I did. I put all possible parenthesis in the expression to make sure that the order of operations was right but it is still different..

One Answer

The variable x has to be converted to degrees inside the cosine function using deg(x) instead of x. Another option is to work with radians using the option pgfplotsset{trig format plots=rad} when you use pgfplots.

Here is my attempt using pgfplots:

documentclass[border=0.2cm]{standalone}

usepackage{pgfplots}

pgfplotsset{trig format plots=rad, compat=newest}


begin{document}

begin{tikzpicture}[scale=1]
  
begin{axis}
[
    ymin=-0.5,ymax=4.2,xmin=-0.85,xmax=1.05,
    axis x line = bottom,
    axis y line = left,
    axis x line shift=-0.5,
    axis y line shift=-0.85,
    minor tick num = 2,
    ticklabel style = {font=scriptsize},
    ticklabel style = {fill=white,inner sep =2pt},
    width = 10cm,
    height = 7cm,
    grid=both,
    major grid style = {lightgray!50},
    minor grid style = {lightgray!40,dotted},
]
addplot[thick,blue,domain=-0.7:0.96,samples=1000] {(1/(x+1))-cos((180/pi)*0.2*(x)^2)*0.5*x};       
end{axis}
  
end{tikzpicture}

end{document}

which yields:

enter image description here

close to the Wolframalpha results: enter image description here

Correct answer by LaTeXdraw-com on April 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