{"id":2553,"date":"2026-02-02T10:47:29","date_gmt":"2026-02-02T10:47:29","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2553"},"modified":"2026-02-02T10:47:37","modified_gmt":"2026-02-02T10:47:37","slug":"chapter-10-ufunc-trigonometric","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-10-ufunc-trigonometric\/","title":{"rendered":"Chapter 11: ufunc Trigonometric"},"content":{"rendered":"<h3 dir=\"auto\">1. What are trigonometric ufuncs in NumPy?<\/h3>\n<p dir=\"auto\">NumPy provides <strong>six main trigonometric functions<\/strong> as universal functions (ufuncs):<\/p>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Function<\/th>\n<th data-col-size=\"md\">Computes<\/th>\n<th data-col-size=\"sm\">Input unit<\/th>\n<th data-col-size=\"md\">Output range<\/th>\n<th data-col-size=\"lg\">Most common use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">np.sin<\/td>\n<td data-col-size=\"md\">sine<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">[-1, 1]<\/td>\n<td data-col-size=\"lg\">oscillations, waves<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">np.cos<\/td>\n<td data-col-size=\"md\">cosine<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">[-1, 1]<\/td>\n<td data-col-size=\"lg\">phase shifts, projections<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">np.tan<\/td>\n<td data-col-size=\"md\">tangent<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">(-\u221e, +\u221e)<\/td>\n<td data-col-size=\"lg\">slopes, angles<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">np.arcsin<\/td>\n<td data-col-size=\"md\">inverse sine (asin)<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">[-\u03c0\/2, \u03c0\/2]<\/td>\n<td data-col-size=\"lg\">find angle from sine<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">np.arccos<\/td>\n<td data-col-size=\"md\">inverse cosine (acos)<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">[0, \u03c0]<\/td>\n<td data-col-size=\"lg\">find angle from cosine<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">np.arctan<\/td>\n<td data-col-size=\"md\">inverse tangent (atan)<\/td>\n<td data-col-size=\"sm\">radians<\/td>\n<td data-col-size=\"md\">(-\u03c0\/2, \u03c0\/2)<\/td>\n<td data-col-size=\"lg\">find angle from tangent<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">There are also two-argument versions:<\/p>\n<ul dir=\"auto\">\n<li>np.arctan2(y, x) \u2192 angle of point (x,y) in correct quadrant (\u2212\u03c0, \u03c0]<\/li>\n<\/ul>\n<p dir=\"auto\">And hyperbolic versions (less common but important):<\/p>\n<ul dir=\"auto\">\n<li>np.sinh, np.cosh, np.tanh, np.arcsinh, etc.<\/li>\n<\/ul>\n<h3 dir=\"auto\">2. The single most important rule you must remember<\/h3>\n<p dir=\"auto\"><strong>All trigonometric functions in NumPy expect input in radians \u2014 not degrees.<\/strong><\/p>\n<p dir=\"auto\">This is the #1 source of confusion for beginners.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Wrong (most people forget this)\r\nprint(np.sin(90))           # sin(90 radians) \u2248 0.893 (not 1!)\r\n\r\n# Correct\r\nprint(np.sin(np.pi\/2))      # \u2248 1.0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Conversion helpers<\/strong> (memorize or bookmark these):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>deg = 45\r\nrad = np.deg2rad(deg)       # or deg * np.pi \/ 180\r\nprint(np.sin(rad))          # \u2248 0.707\r\n\r\n# Opposite direction\r\nprint(np.rad2deg(np.pi\/3))  # 60.0<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Basic usage examples \u2013 all six functions<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>angles_rad = np.linspace(0, 2*np.pi, 13)  # 0 to 360\u00b0 in 13 points\r\n\r\nprint(\"Angles (rad):\", angles_rad.round(3))\r\nprint(\"Angles (deg):\", np.rad2deg(angles_rad).astype(int))\r\n\r\nprint(\"\\nsin =\", np.sin(angles_rad).round(3))\r\nprint(\"cos =\", np.cos(angles_rad).round(3))\r\nprint(\"tan =\", np.tan(angles_rad).round(3))   # notice discontinuities<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Inverse functions<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>sine_values = np.array([-1, -0.707, 0, 0.707, 1])\r\n\r\nprint(\"arcsin =\", np.arcsin(sine_values).round(3))      # -\u03c0\/2 to \u03c0\/2\r\nprint(\"arccos =\", np.arccos(sine_values).round(3))      # 0 to \u03c0\r\nprint(\"arctan =\", np.arctan(sine_values).round(3))      # -\u03c0\/2 to \u03c0\/2<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very important: arctan2 (two-argument version)<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>x = np.array([ 1, -1, -1,  1,  0])\r\ny = np.array([ 0,  0,  1, -1,  1])\r\n\r\nangles = np.arctan2(y, x)\r\nprint(\"arctan2(y,x) in radians:\", angles.round(3))\r\nprint(\"in degrees:           \", np.rad2deg(angles).astype(int))\r\n# \u2192 [  0 180  90 -90  90 ]  \u2190 correct quadrant!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Realistic visualizations \u2013 the best way to build intuition<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>theta = np.linspace(0, 2*np.pi, 300)\r\n\r\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5.5))\r\n\r\n# Unit circle\r\nax1.plot(np.cos(theta), np.sin(theta), lw=2, color='teal')\r\nax1.set_aspect('equal')\r\nax1.set_title(\"Unit circle: cos(\u03b8), sin(\u03b8)\", fontsize=13)\r\nax1.set_xlabel(\"cos(\u03b8)\")\r\nax1.set_ylabel(\"sin(\u03b8)\")\r\nax1.axhline(0, color='gray', lw=0.8)\r\nax1.axvline(0, color='gray', lw=0.8)\r\nax1.grid(True, alpha=0.3)\r\n\r\n# All three functions\r\nax2.plot(theta, np.sin(theta), label=\"sin\", lw=2.2)\r\nax2.plot(theta, np.cos(theta), label=\"cos\", lw=2.2)\r\nax2.plot(theta, np.tan(theta), label=\"tan\", lw=2.2, alpha=0.8)\r\nax2.set_title(\"sin, cos, tan over one period\", fontsize=13)\r\nax2.set_xlabel(\"\u03b8 (radians)\")\r\nax2.set_ylabel(\"Value\")\r\nax2.set_ylim(-2, 2)\r\nax2.legend()\r\nax2.grid(True, alpha=0.3)\r\n\r\nplt.tight_layout()\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Very common realistic patterns you will use<\/h3>\n<p dir=\"auto\"><strong>Pattern 1 \u2013 Convert degrees to radians and compute<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>angles_deg = np.array([0, 30, 45, 60, 90, 120, 180, 270, 360])\r\n\r\nangles_rad = np.deg2rad(angles_deg)\r\n\r\nsines  = np.sin(angles_rad)\r\ncosines = np.cos(angles_rad)\r\n\r\nfor d, s, c in zip(angles_deg, sines, cosines):\r\n    print(f\"{d:3}\u00b0 \u2192 sin = {s:6.3f}, cos = {c:6.3f}\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Pattern 2 \u2013 Generate sine\/cosine waves (signal processing)<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>t = np.linspace(0, 2, 1000)          # time in seconds\r\nfrequency = 5                        # 5 Hz\r\namplitude = 2.5\r\n\r\nwave = amplitude * np.sin(2 * np.pi * frequency * t)\r\n\r\nplt.plot(t, wave, lw=1.8, color='teal')\r\nplt.title(\"5 Hz sine wave\")\r\nplt.xlabel(\"Time (s)\")\r\nplt.ylabel(\"Amplitude\")\r\nplt.grid(True, alpha=0.3)\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Pattern 3 \u2013 Find angles from ratios (arctan2 is critical)<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code># Robot arm \/ game physics \u2013 direction to target\r\ndx = np.array([3, -4, 0, 5])\r\ndy = np.array([4,  3, 5, 0])\r\n\r\nangles_rad = np.arctan2(dy, dx)\r\nangles_deg = np.rad2deg(angles_rad)\r\n\r\nfor x, y, deg in zip(dx, dy, angles_deg):\r\n    print(f\"dx={x:2}, dy={y:2} \u2192 angle = {deg:6.1f}\u00b0\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Pattern 4 \u2013 Phase shift and superposition<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Python<\/div>\n<div>\n<pre tabindex=\"0\"><code>t = np.linspace(0, 2, 1000)\r\n\r\nwave1 = np.sin(2 * np.pi * 3 * t)           # 3 Hz\r\nwave2 = 0.6 * np.sin(2 * np.pi * 5 * t + np.pi\/3)  # 5 Hz with phase\r\n\r\ncombined = wave1 + wave2\r\n\r\nplt.plot(t, wave1, lw=1.2, alpha=0.7, label=\"3 Hz\")\r\nplt.plot(t, wave2, lw=1.2, alpha=0.7, label=\"5 Hz + phase\")\r\nplt.plot(t, combined, lw=2.5, color='darkblue', label=\"Superposition\")\r\nplt.legend()\r\nplt.title(\"Wave superposition\")\r\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Summary \u2013 Trigonometric ufuncs Quick Reference<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Function<\/th>\n<th data-col-size=\"md\">Input unit<\/th>\n<th data-col-size=\"md\">Output range<\/th>\n<th data-col-size=\"xl\">Most common mistake<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">np.sin<\/td>\n<td data-col-size=\"md\">radians<\/td>\n<td data-col-size=\"md\">[-1, 1]<\/td>\n<td data-col-size=\"xl\">giving degrees<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.cos<\/td>\n<td data-col-size=\"md\">radians<\/td>\n<td data-col-size=\"md\">[-1, 1]<\/td>\n<td data-col-size=\"xl\">same<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.tan<\/td>\n<td data-col-size=\"md\">radians<\/td>\n<td data-col-size=\"md\">(-\u221e, +\u221e)<\/td>\n<td data-col-size=\"xl\">discontinuities at \u03c0\/2<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.arcsin<\/td>\n<td data-col-size=\"md\">[-1,1]<\/td>\n<td data-col-size=\"md\">[-\u03c0\/2, \u03c0\/2]<\/td>\n<td data-col-size=\"xl\">domain error outside [-1,1]<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.arccos<\/td>\n<td data-col-size=\"md\">[-1,1]<\/td>\n<td data-col-size=\"md\">[0, \u03c0]<\/td>\n<td data-col-size=\"xl\">domain error<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.arctan<\/td>\n<td data-col-size=\"md\">any<\/td>\n<td data-col-size=\"md\">(-\u03c0\/2, \u03c0\/2)<\/td>\n<td data-col-size=\"xl\">loses quadrant info<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">np.arctan2<\/td>\n<td data-col-size=\"md\">(y,x)<\/td>\n<td data-col-size=\"md\">(-\u03c0, \u03c0]<\/td>\n<td data-col-size=\"xl\"><strong>always use this for angles<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Final teacher advice (very important)<\/h3>\n<p dir=\"auto\"><strong>Golden rule #1<\/strong> <strong>All trigonometric functions expect radians<\/strong> \u2014 never pass degrees directly. Always convert with np.deg2rad() or np.pi \/ 180.<\/p>\n<p dir=\"auto\"><strong>Golden rule #2<\/strong> <strong>Use np.arctan2(y, x) instead of np.arctan(y\/x)<\/strong> whenever you need the correct angle in the full circle (\u2212180\u00b0 to 180\u00b0).<\/p>\n<p dir=\"auto\"><strong>Golden rule #3<\/strong> <strong>Tan has discontinuities<\/strong> \u2014 be careful when plotting or using np.tan near odd multiples of \u03c0\/2.<\/p>\n<p dir=\"auto\"><strong>Golden rule #4<\/strong> <strong>For signal\/wave work always use radians<\/strong> \u2014 the 2\u03c0 period is natural in radians.<\/p>\n<p dir=\"auto\">Would you like to continue with any of these topics?<\/p>\n<ul dir=\"auto\">\n<li>Trigonometric identities and how NumPy handles them<\/li>\n<li>Phase, amplitude, frequency in signal generation<\/li>\n<li>Realistic mini-project: simulate sound wave or pendulum motion<\/li>\n<li>Common floating-point pitfalls with trig functions<\/li>\n<li>Hyperbolic trig functions (sinh, cosh, tanh)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what you want to focus on next! \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What are trigonometric ufuncs in NumPy? NumPy provides six main trigonometric functions as universal functions (ufuncs): Function Computes Input unit Output range Most common use case np.sin sine radians [-1, 1] oscillations, waves&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-2553","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2553"}],"version-history":[{"count":2,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2553\/revisions"}],"predecessor-version":[{"id":2555,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2553\/revisions\/2555"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}