1

I hope everyone is well.

I've been working on a project, and part of it requires me sketch a rather complicated composite function (two or more functions in one). Here is the code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage[bottom]{footmisc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\onehalfspacing
\rmfamily
\setlength{\parindent}{0pt}
\usepackage{geometry}
\geometry{
    paper=a4paper,
    top=3cm,
    bottom=4cm,
    left=2.5cm, 
    right=2.5cm, 
    headheight=14pt, 
    footskip=1.2cm,
    headsep=1.2cm, 
}
\begin{document}
\begin{figure}[h!]
    \centering
        \caption{Graph showing the angular radius $\phi$ as a function of angle of the incidence $\theta_i$, for $n_2=1.33$, the general refractive index of water for light (Hecht, 2003):}
        \vspace{2mm}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
  grid=both,
  grid style={line width=.1pt, draw=gray!10},
  xmin = 0,
  xmax = 2,
  ymin = 0,
  ymax = 1, 
  axis lines = middle,
  enlargelimits = true,
  restrict y to domain=0:2
  samples=400
  ]
    \addplot[color = red,domain=0:1.570796327]  {(4*asin(0.75187969924 * sin(x))) - 2*sin(x)};
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

The key code here is what is being passed into the addplot operator: (4*asin(0.75187969924 * sin(x))) - 2*sin(x), and this is what I am having issues with. It is simply producing the wrong shape. Here is what it should produce, as seen on desmos: enter image description here And this is what it produces on LaTeX: enter image description here I am using the most recent version of LaTeX on TeXMaker.

I have tried to solve this problem for multiple hours. I have tried using deg() and rad() and nothing, but neither work. Any help to point me in the right direction would be greatly appreciated!

  • Note that texmaker is not really relevant to this. But please extend your example to something others can copy and test as is. There is no document class. – daleif Apr 13 '21 at 09:08
  • BTW: if I plot that function in Mathematica I don't seem to get your desmos graph. Perhaps this is related to radians vs degrees? – daleif Apr 13 '21 at 09:14
  • @daleif thanks for looking at my problem! Are you plotting it with the correct value of 1/n? if so, how is your graph different? I would hope that the numbers or values are different but the shape is the same, in which case then yes, it is to do with radians vs degrees. Desmos plots the graph in radians... – Dylan Wang Apr 13 '21 at 09:19
  • I jut graphed the function as is. – daleif Apr 13 '21 at 09:40
  • Answer is covered in the second answer in https://tex.stackexchange.com/questions/12951/incorrect-plot-using-pgfplots-trigonometric-functions-like-cos-sin-and-tan. – user202729 Oct 14 '22 at 01:43

1 Answers1

1

You can setup pgfplots to use radiants in your trigonometric functions.

Try

\pgfplotsset{trig format plots=rad} 

in your preamble. (E.g. after \pgfplotsset{compat=1.8})

Edit (complete working example):

\documentclass{article}

\usepackage{amsmath} \usepackage{amssymb} \usepackage{pgf,tikz,pgfplots} \pgfplotsset{compat=1.8} \pgfplotsset{trig format plots=rad} \usepackage{mathrsfs} \usetikzlibrary{arrows} \begin{document} \begin{figure}[h!] \centering \caption{Graph showing the angular radius $\phi$ as a function of angle of the incidence $\theta_i$, for $n_2=1.33$, the general refractive index of water for light (Hecht, 2003):} \vspace{2mm} \centering \begin{tikzpicture} \begin{axis}[ grid=both, grid style={line width=.1pt, draw=gray!10}, xmin = -1, xmax = 3, ymin = -1, ymax = 2, axis lines = middle, enlargelimits = true, % restrict y to domain=0:2 samples=400 ] \addplot[color = red,domain=0:3] {(4asin(0.75187969924 sin(x))) - 2*x}; \end{axis} \end{tikzpicture} \end{figure} \end{document}

produces:

output

flipper
  • 36
  • 1
    This way, you won't get confused by conversion between degrees and radiants. Especially together with the inverse functions this could create headaches ;) – flipper Apr 13 '21 at 09:25
  • Hey, I just tried that and it ended up becoming a straight line: – Dylan Wang Apr 13 '21 at 09:25
  • I think I might be stuffing up the actual equation... – Dylan Wang Apr 13 '21 at 09:26
  • it should be (almost) straight until approx. 0.7 (as in your other example). You also need to change the function in the plot. You substract - 2*sin(x) but in desmos you substract -2x. I will edit my answer with a complete working solution in a minute. – flipper Apr 13 '21 at 09:28
  • yeah thanks for the help. i just realised i did indeed stuff up the actual equation lol – Dylan Wang Apr 13 '21 at 09:28