{"id":985363,"date":"2024-12-27T07:35:59","date_gmt":"2024-12-26T23:35:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985363.html"},"modified":"2024-12-27T07:36:29","modified_gmt":"2024-12-26T23:36:29","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sin","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985363.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528sin"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212551\/d1c6366d-f4cc-49d8-ac45-4f4e6ceb2a39.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528sin\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u4f7f\u7528<code>sin<\/code>\u51fd\u6570\u8ba1\u7b97\u6b63\u5f26\u503c\u4e3b\u8981\u901a\u8fc7<code>math<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<strong><code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u6807\u91cf\u64cd\u4f5c\uff1b\u800c<code>numpy<\/code>\u5e93\u5219\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u7ec4\u6216\u77e9\u9635\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97\u7684\u573a\u666f\u3002<code>math.sin<\/code>\u548c<code>numpy.sin<\/code>\u662f\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5<\/strong>\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>sin<\/code>\u51fd\u6570\uff0c\u5305\u62ec\u5176\u5e94\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATH\u6a21\u5757\u4e2d\u7684SIN\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ec<code>sin<\/code>\u51fd\u6570\u3002<code>math.sin<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u7ed9\u5b9a\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\uff0c\u89d2\u5ea6\u4ee5\u5f27\u5ea6\u4e3a\u5355\u4f4d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528<code>math.sin<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>math.sin<\/code>\u51fd\u6570\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u3002\u8ba1\u7b97\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\u65f6\uff0c\u9700\u786e\u4fdd\u89d2\u5ea6\u4e3a\u5f27\u5ea6\u5236\u3002\u5982\u679c\u89d2\u5ea6\u4e3a\u5ea6\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.radians<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5f27\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u5c06\u89d2\u5ea6\u4ece\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6<\/strong><\/h2>\n<p>angle_in_degrees = 30<\/p>\n<p>angle_in_radians = math.radians(angle_in_degrees)<\/p>\n<h2><strong>\u8ba1\u7b97\u6b63\u5f26\u503c<\/strong><\/h2>\n<p>sine_value = math.sin(angle_in_radians)<\/p>\n<p>print(sine_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c0630\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\uff0c\u7136\u540e\u8ba1\u7b97\u5176\u6b63\u5f26\u503c\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a<code>math.sin<\/code>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8ba1\u7b97\u573a\u666f\uff0c\u4f8b\u5982\u5355\u4e2a\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\u8ba1\u7b97\uff0c\u7279\u522b\u662f\u5728\u51e0\u4f55\u3001\u7269\u7406\u548c\u5de5\u7a0b\u9886\u57df\u4e2d\u5e38\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u89d2\u5ea6\u4e3a\u5f27\u5ea6\u5236\u3002\u6b64\u5916\uff0c<code>math.sin<\/code>\u7684\u8f93\u5165\u503c\u4e3a\u5355\u4e2a\u6d6e\u70b9\u6570\u6216\u6574\u6570\uff0c\u4e0d\u652f\u6301\u6570\u7ec4\u6216\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001NUMPY\u5e93\u4e2d\u7684SIN\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u548c\u77e9\u9635\u8fdb\u884c\u9ad8\u6548\u8ba1\u7b97\u7684\u51fd\u6570\u3002<code>numpy.sin<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8ba1\u7b97\u6b63\u5f26\u503c\uff0c\u975e\u5e38\u9002\u5408\u6279\u91cf\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528<code>numpy.sin<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.sin<\/code>\u65f6\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5e76\u5bfc\u5165<code>numpy<\/code>\u5e93\u3002<code>numpy.sin<\/code>\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u89d2\u5ea6\u7684\u6570\u7ec4\uff08\u5f27\u5ea6\u5236\uff09<\/strong><\/h2>\n<p>angles_in_radians = np.array([0, np.pi\/6, np.pi\/4, np.pi\/3, np.pi\/2])<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u89d2\u5ea6\u7684\u6b63\u5f26\u503c<\/strong><\/h2>\n<p>sine_values = np.sin(angles_in_radians)<\/p>\n<p>print(sine_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u89d2\u5ea6\u7684\u6570\u7ec4\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u4e2a\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong>\uff1a<code>numpy.sin<\/code>\u7279\u522b\u9002\u5408\u9700\u8981\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u6279\u91cf\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u5982\u6570\u636e\u5206\u6790\u3001\u4fe1\u53f7\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u4e3a<code>numpy<\/code>\u6570\u7ec4\uff0c\u5e76\u4e14\u89d2\u5ea6\u4e3a\u5f27\u5ea6\u5236\u3002<code>numpy<\/code>\u5e93\u7684\u5b89\u88c5\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy<\/code>\u547d\u4ee4\u8fdb\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001SIN\u51fd\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51e0\u4f55\u548c\u7269\u7406\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u6b63\u5f26\u51fd\u6570\u5728\u51e0\u4f55\u548c\u7269\u7406\u5b66\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u4e09\u89d2\u51fd\u6570\u4e2d\uff0c\u6b63\u5f26\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u76f4\u89d2\u4e09\u89d2\u5f62\u4e2d\u8fb9\u7684\u6bd4\u4f8b\u5173\u7cfb\u3002\u5728\u7269\u7406\u5b66\u4e2d\uff0c\u6b63\u5f26\u51fd\u6570\u7528\u4e8e\u63cf\u8ff0\u632f\u52a8\u548c\u6ce2\u52a8\u73b0\u8c61\uff0c\u5982\u7b80\u8c10\u8fd0\u52a8\u548c\u6ce2\u7684\u4f20\u64ad\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u51e0\u4f55\u8ba1\u7b97\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>math.sin<\/code>\u6216<code>numpy.sin<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u5404\u79cd\u4e09\u89d2\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u4e2a\u7269\u4f53\u5728\u659c\u5761\u4e0a\u7684\u6295\u5f71\u957f\u5ea6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5f26\u51fd\u6570\u8ba1\u7b97\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\uff0c\u7136\u540e\u4e58\u4ee5\u659c\u5761\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fe1\u53f7\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4fe1\u53f7\u5904\u7406\u9886\u57df\uff0c\u6b63\u5f26\u51fd\u6570\u7528\u4e8e\u751f\u6210\u548c\u5206\u6790\u4fe1\u53f7\u3002\u6b63\u5f26\u6ce2\u662f\u6700\u57fa\u672c\u7684\u5468\u671f\u4fe1\u53f7\uff0c\u5176\u7279\u6027\u53ef\u4ee5\u901a\u8fc7\u6b63\u5f26\u51fd\u6570\u6765\u63cf\u8ff0\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408<code>numpy<\/code>\u5e93\u751f\u6210\u6b63\u5f26\u6ce2\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u65f6\u95f4\u5e8f\u5217<\/strong><\/h2>\n<p>t = np.linspace(0, 1, 500)<\/p>\n<h2><strong>\u751f\u6210\u6b63\u5f26\u6ce2\u4fe1\u53f7<\/strong><\/h2>\n<p>frequency = 5  # \u9891\u7387\u4e3a5Hz<\/p>\n<p>sine_wave = np.sin(2 * np.pi * frequency * t)<\/p>\n<h2><strong>\u7ed8\u5236\u6b63\u5f26\u6ce2\u4fe1\u53f7<\/strong><\/h2>\n<p>plt.plot(t, sine_wave)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&#39;Time (s)&#39;)<\/p>\n<p>plt.ylabel(&#39;Amplitude&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u751f\u6210\u5e76\u7ed8\u5236\u4e86\u9891\u7387\u4e3a5Hz\u7684\u6b63\u5f26\u6ce2\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>sin<\/code>\u51fd\u6570\u8ba1\u7b97\u6b63\u5f26\u503c\u662f\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u7684\u57fa\u672c\u64cd\u4f5c\u3002<strong>\u9009\u62e9<code>math.sin<\/code>\u8fd8\u662f<code>numpy.sin<\/code>\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff1a<code>math.sin<\/code>\u9002\u7528\u4e8e\u5355\u4e00\u503c\u7684\u8ba1\u7b97\uff0c\u800c<code>numpy.sin<\/code>\u9002\u7528\u4e8e\u6279\u91cf\u8ba1\u7b97\u3002<\/strong>\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u4fe1\u53f7\u5904\u7406\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\uff0c\u6b63\u5f26\u51fd\u6570\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u7ed3\u5408Python\u7684\u5f3a\u5927\u5e93\u548c\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528sin\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165math\u6a21\u5757\u6765\u4f7f\u7528sin\u51fd\u6570\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\nresult = math.sin(math.radians(30))  # 30\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\nprint(result)  # \u8f93\u51fa0.5\n<\/code><\/pre>\n<p>\u786e\u4fdd\u8f93\u5165\u7684\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\uff0c\u56e0\u4e3asin\u51fd\u6570\u63a5\u53d7\u7684\u53c2\u6570\u662f\u5f27\u5ea6\u800c\u975e\u89d2\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528sin\u51fd\u6570\u8ba1\u7b97\u54ea\u4e9b\u7c7b\u578b\u7684\u89d2\u5ea6\uff1f<\/strong><br \/>sin\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u4efb\u610f\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\uff0c\u5305\u62ec\u6b63\u89d2\u548c\u8d1f\u89d2\u3002\u8d1f\u89d2\u5bf9\u5e94\u7684\u662f\u9006\u65f6\u9488\u65b9\u5411\u7684\u89d2\u5ea6\u3002\u4f8b\u5982\uff0csin(-30)\u5c06\u8fd4\u56de\u4e0esin(330)\u76f8\u540c\u7684\u7ed3\u679c\u3002\u4f7f\u7528math.radians()\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528numpy\u5e93\u6765\u8ba1\u7b97\u6b63\u5f26\u503c\uff1f<\/strong><br \/>\u9664\u4e86math\u6a21\u5757\uff0cnumpy\u5e93\u4e5f\u662f\u8ba1\u7b97\u6b63\u5f26\u503c\u7684\u70ed\u95e8\u9009\u62e9\u3002\u4f7f\u7528numpy\uff0c\u53ef\u4ee5\u5904\u7406\u6570\u7ec4\u5e76\u8fdb\u884c\u5411\u91cf\u5316\u8ba1\u7b97\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nangles = np.array([0, 30, 45, 60, 90])\nradians = np.radians(angles)  # \u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\nsine_values = np.sin(radians)  # \u8ba1\u7b97\u6b63\u5f26\u503c\nprint(sine_values)  # \u8f93\u51fa\u5bf9\u5e94\u89d2\u5ea6\u7684\u6b63\u5f26\u503c\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u591a\u4e2a\u89d2\u5ea6\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528sin\u51fd\u6570\u8ba1\u7b97\u6b63\u5f26\u503c\u4e3b\u8981\u901a\u8fc7math\u6a21\u5757\u6216numpy\u5e93\u6765\u5b9e\u73b0\u3002math\u6a21\u5757\u63d0\u4f9b\u57fa\u672c\u7684\u6570\u5b66 [&hellip;]","protected":false},"author":3,"featured_media":985418,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985363"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=985363"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985363\/revisions"}],"predecessor-version":[{"id":985422,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985363\/revisions\/985422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985418"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}