Math & ScienceWrong Calculations in Function Plot

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
computernormal
Posts: 3
Joined: Tue Aug 27, 2013 1:01 am

Wrong Calculations in Function Plot

Post by computernormal »

Hello forum,

for my bachelor thesis I need to draw the function f(x)=sin(x²).

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      legend pos=outer north east,
      grid=major,
      xlabel=$t$,
      ylabel=$f(t)$,
      xmin=0
    ]
      \addplot [smooth, blue, mark=none]{sin(deg(x^2))};
      \legend{$f(t)=\sin(x^2)$};
    \end{axis}
  \end{tikzpicture}
\end{document}
But this code shows me the following function.
plot-sine-squared.png
plot-sine-squared.png (9.25 KiB) Viewed 5281 times
In a calculation table by tabularcalc or in other online function drawers is the function value for f(4,8)=-0.88!

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tabularcalc}

\begin{document}
  \htablecalc[3]{$x$}{x=4.8}
    {$f(x)=x$}{x}
    {$g(x)=x^{2}$}{sin(deg(x^2))}
    {$h(x)=\sqrt{x^{2}}$}{round(root(2,x*x),3)}
\end{document}
The result is this.
table-sine-squared.png
table-sine-squared.png (5.72 KiB) Viewed 5281 times
Why does LaTeX draw false here? :shock:

Greetings
Andre

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Wrong Calculations in Function Plot

Post by mas »

I have not used tabularcalc. But it looks like the problem seems to be in the units used.

sin(4.8**2) (for radians) will give you -0.88. sin(4.8**2) (for degrees) is 0.39137.

You are plotting the graph using degrees. Try using a single unit.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Wrong Calculations in Function Plot

Post by cgnieder »

The function is correct (http://www.wolframalpha.com/input/?i=sin%28x^2%29) and the plot nearly: you need a higher sampling:

Code: Select all

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      legend pos=outer north east,
      grid=major,
      xlabel=$t$,
      ylabel=$f(t)$,
      xmin=0
    ]
      \addplot[smooth, blue, mark=none,samples=200]{sin(deg(x^2))};
      \legend{$f(t)=\sin(x^2)$};
    \end{axis}
  \end{tikzpicture}
\end{document}

Regards
Attachments
sinx2.png
sinx2.png (16.04 KiB) Viewed 5289 times
site moderator & package author
computernormal
Posts: 3
Joined: Tue Aug 27, 2013 1:01 am

Re: Wrong Calculations in Function Plot

Post by computernormal »

Thanks ... saved my day :)
Post Reply