{"id":953007,"date":"2024-12-27T01:38:38","date_gmt":"2024-12-26T17:38:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953007.html"},"modified":"2024-12-27T01:38:40","modified_gmt":"2024-12-26T17:38:40","slug":"python%e4%b8%ad%e5%af%bc%e6%95%b0%e5%a6%82%e4%bd%95%e8%a1%a8%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953007.html","title":{"rendered":"python\u4e2d\u5bfc\u6570\u5982\u4f55\u8868\u8ff0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090637\/4e98d8b0-77e7-4a21-bf8b-c2e80cfadc1e.webp\" alt=\"python\u4e2d\u5bfc\u6570\u5982\u4f55\u8868\u8ff0\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5bfc\u6570\u7684\u8868\u8ff0\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u7b26\u53f7\u8ba1\u7b97\u5e93\u3001\u6570\u503c\u8ba1\u7b97\u5e93\u7b49\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528SymPy\u5e93\u8fdb\u884c\u7b26\u53f7\u5bfc\u6570\u8ba1\u7b97\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u503c\u5bfc\u6570\u8ba1\u7b97\u3001\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u9ad8\u7ea7\u6570\u503c\u5bfc\u6570\u8ba1\u7b97<\/strong>\u3002SymPy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u9002\u5408\u4e8e\u9700\u8981\u7cbe\u786e\u8ba1\u7b97\u5bfc\u6570\u7684\u573a\u5408\uff1bNumPy\u5e93\u9002\u5408\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\uff0c\u8fdb\u884c\u7b80\u5355\u7684\u6570\u503c\u5bfc\u6570\u8ba1\u7b97\uff1bSciPy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u9ad8\u7ea7\u7684\u6570\u503c\u5bfc\u6570\u5de5\u5177\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SYMPY\u8fdb\u884c\u7b26\u53f7\u5bfc\u6570\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2aPython\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u8fdb\u884c\u7b26\u53f7\u5bfc\u6570\u7684\u8ba1\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u5de5\u5177\u7528\u4e8e\u6570\u5b66\u7b26\u53f7\u8fd0\u7b97\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u7cbe\u786e\u8ba1\u7b97\u5bfc\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>SymPy\u5e93\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528SymPy\u4e4b\u524d\uff0c\u9700\u8981\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8ba1\u7b97\u7b26\u53f7\u5bfc\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, diff<\/p>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<p>f = x2 + 3*x + 2<\/p>\n<p>derivative = diff(f, x)<\/p>\n<p>print(derivative)  # \u8f93\u51fa\u4e3a2*x + 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>symbols<\/code>\u51fd\u6570\u7528\u4e8e\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<code>x<\/code>\uff0c<code>diff<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u8868\u8fbe\u5f0f<code>f<\/code>\u5bf9<code>x<\/code>\u7684\u5bfc\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9ad8\u9636\u5bfc\u6570\u4e0e\u504f\u5bfc\u6570<\/strong><\/p>\n<\/p>\n<p><p>SymPy\u4e0d\u4ec5\u652f\u6301\u4e00\u9636\u5bfc\u6570\u7684\u8ba1\u7b97\uff0c\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u9ad8\u9636\u5bfc\u6570\u548c\u504f\u5bfc\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51fd\u6570<code>f(x) = x&lt;strong&gt;3<\/code>\u7684\u4e8c\u9636\u5bfc\u6570\u548c\u51fd\u6570<code>f(x, y) = x&lt;\/strong&gt;2 + y2<\/code>\u5bf9<code>x<\/code>\u7684\u504f\u5bfc\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, diff<\/p>\n<p>x, y = symbols(&#39;x y&#39;)<\/p>\n<p>f1 = x3<\/p>\n<p>second_derivative = diff(f1, x, 2)<\/p>\n<p>print(second_derivative)  # \u8f93\u51fa\u4e3a6*x<\/p>\n<p>f2 = x&lt;strong&gt;2 + y&lt;\/strong&gt;2<\/p>\n<p>partial_derivative = diff(f2, x)<\/p>\n<p>print(partial_derivative)  # \u8f93\u51fa\u4e3a2*x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06<code>diff<\/code>\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a2\uff0c\u53ef\u4ee5\u8ba1\u7b97\u4e8c\u9636\u5bfc\u6570\u3002\u5bf9\u4e8e\u504f\u5bfc\u6570\uff0c\u53ea\u9700\u8981\u6307\u5b9a\u5bf9\u54ea\u4e2a\u53d8\u91cf\u6c42\u5bfc\u5373\u53ef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528NUMPY\u8fdb\u884c\u6570\u503c\u5bfc\u6570\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u3002\u867d\u7136NumPy\u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u7684\u5bfc\u6570\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6709\u9650\u5dee\u5206\u7684\u65b9\u6cd5\u8fd1\u4f3c\u8ba1\u7b97\u5bfc\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6709\u9650\u5dee\u5206\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u9650\u5dee\u5206\u6cd5\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u503c\u5fae\u5206\u65b9\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u51fd\u6570\u503c\u7684\u5dee\u5546\u6765\u8fd1\u4f3c\u5bfc\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u8ba1\u7b97\u4e00\u7ef4\u51fd\u6570\u6570\u503c\u5bfc\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>dx = x[1] - x[0]<\/p>\n<p>df = np.diff(f(x)) \/ dx<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>np.diff<\/code>\u7528\u4e8e\u8ba1\u7b97\u51fd\u6570\u503c\u7684\u5dee\u5206\uff0c<code>dx<\/code>\u4e3a\u6b65\u957f\uff0c\u901a\u8fc7\u5dee\u5206\u5546\u8ba1\u7b97\u5bfc\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5411\u91cf\u5316\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u652f\u6301\u5411\u91cf\u5316\u8ba1\u7b97\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u8fd0\u7b97\u3002\u5728\u8ba1\u7b97\u6570\u503c\u5bfc\u6570\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u52a0\u901f\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def vectorized_derivative(f, x, dx=1e-5):<\/p>\n<p>    return (f(x + dx) - f(x - dx)) \/ (2 * dx)<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>df = vectorized_derivative(f, x)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e2d\u5fc3\u5dee\u5206\u516c\u5f0f\u6765\u63d0\u9ad8\u8ba1\u7b97\u7cbe\u5ea6\u3002<code>vectorized_derivative<\/code>\u51fd\u6570\u53ef\u4ee5\u9ad8\u6548\u5730\u8ba1\u7b97\u5bfc\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528SCIPY\u8fdb\u884c\u9ad8\u7ea7\u6570\u503c\u5bfc\u6570\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u7684\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u503c\u8ba1\u7b97\u5de5\u5177\uff0c\u5305\u62ec\u4f18\u5316\u3001\u63d2\u503c\u3001\u79ef\u5206\u7b49\u3002SciPy\u7684<code>misc<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6570\u503c\u5bfc\u6570\u8ba1\u7b97\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528scipy.misc.derivative<\/strong><\/p>\n<\/p>\n<p><p><code>scipy.misc.derivative<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4e00\u7ef4\u51fd\u6570\u7684\u6570\u503c\u5bfc\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.misc import derivative<\/p>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<p>x = 5<\/p>\n<p>df = derivative(f, x, dx=1e-6)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>derivative<\/code>\u51fd\u6570\u901a\u8fc7\u6307\u5b9a\u6b65\u957f<code>dx<\/code>\u8ba1\u7b97\u51fd\u6570<code>f<\/code>\u5728\u70b9<code>x<\/code>\u5904\u7684\u5bfc\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u591a\u7ef4\u6570\u503c\u5bfc\u6570<\/strong><\/p>\n<\/p>\n<p><p>SciPy\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u591a\u7ef4\u51fd\u6570\u7684\u6570\u503c\u5bfc\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51fd\u6570<code>f(x, y) = x&lt;strong&gt;2 + y&lt;\/strong&gt;2<\/code>\u5728\u70b9<code>(3, 4)<\/code>\u5904\u7684\u504f\u5bfc\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.misc import derivative<\/p>\n<p>import numpy as np<\/p>\n<p>def f(x, y):<\/p>\n<p>    return x&lt;strong&gt;2 + y&lt;\/strong&gt;2<\/p>\n<p>def partial_x(x):<\/p>\n<p>    return f(x, 4)<\/p>\n<p>def partial_y(y):<\/p>\n<p>    return f(3, y)<\/p>\n<p>df_dx = derivative(partial_x, 3, dx=1e-6)<\/p>\n<p>df_dy = derivative(partial_y, 4, dx=1e-6)<\/p>\n<p>print(df_dx, df_dy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u8f85\u52a9\u51fd\u6570<code>partial_x<\/code>\u548c<code>partial_y<\/code>\uff0c\u5206\u522b\u8ba1\u7b97\u504f\u5bfc\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7ed3\u5408\u4f7f\u7528NUMPY\u548cSCIPY\u8fdb\u884c\u590d\u6742\u6570\u503c\u5bfc\u6570\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528NumPy\u548cSciPy\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u503c\u5bfc\u6570\u8ba1\u7b97\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u7684\u68af\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u8fdb\u884c\u5411\u91cf\u5316\u8ba1\u7b97\uff0c\u5e76\u7ed3\u5408SciPy\u7684\u4f18\u5316\u5de5\u5177\u8fdb\u884c\u68af\u5ea6\u4e0b\u964d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68af\u5ea6\u8ba1\u7b97\u4e0e\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u68af\u5ea6\u8ba1\u7b97\u662f\u4f18\u5316\u7b97\u6cd5\u7684\u6838\u5fc3\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u8fdb\u884c\u68af\u5ea6\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u5e76\u7ed3\u5408SciPy\u7684\u4f18\u5316\u51fd\u6570\u8fdb\u884c\u6c42\u89e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.optimize import minimize<\/p>\n<p>def loss_function(params):<\/p>\n<p>    x, y = params<\/p>\n<p>    return (x - 1)&lt;strong&gt;2 + (y - 2)&lt;\/strong&gt;2<\/p>\n<p>def gradient(params):<\/p>\n<p>    x, y = params<\/p>\n<p>    return np.array([2*(x - 1), 2*(y - 2)])<\/p>\n<p>initial_params = np.array([0, 0])<\/p>\n<p>result = minimize(loss_function, initial_params, jac=gradient)<\/p>\n<p>print(result.x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>loss_function<\/code>\u4e3a\u76ee\u6807\u51fd\u6570\uff0c<code>gradient<\/code>\u4e3a\u68af\u5ea6\u51fd\u6570\uff0c<code>minimize<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u89e3\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u52a8\u5fae\u5206\u4e0e\u68af\u5ea6\u68c0\u67e5<\/strong><\/p>\n<\/p>\n<p><p>\u81ea\u52a8\u5fae\u5206\u662f\u8ba1\u7b97\u5bfc\u6570\u7684\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\uff0c\u80fd\u591f\u7cbe\u786e\u8ba1\u7b97\u590d\u5408\u51fd\u6570\u7684\u5bfc\u6570\u3002NumPy\u548cSciPy\u6ca1\u6709\u76f4\u63a5\u652f\u6301\u81ea\u52a8\u5fae\u5206\uff0c\u4f46\u53ef\u4ee5\u501f\u52a9\u5916\u90e8\u5e93\u5982JAX\u6216Autograd\u5b9e\u73b0\u3002\u5728\u4f7f\u7528\u6570\u503c\u5bfc\u6570\u65f6\uff0c\u8fdb\u884c\u68af\u5ea6\u68c0\u67e5\u662f\u9a8c\u8bc1\u5bfc\u6570\u8ba1\u7b97\u6b63\u786e\u6027\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import autograd.numpy as np<\/p>\n<p>from autograd import grad<\/p>\n<p>def loss_function(x, y):<\/p>\n<p>    return (x - 1)&lt;strong&gt;2 + (y - 2)&lt;\/strong&gt;2<\/p>\n<p>gradient = grad(loss_function)<\/p>\n<p>x, y = 0.0, 0.0<\/p>\n<p>df_dx, df_dy = gradient(x, y)<\/p>\n<p>print(df_dx, df_dy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528Autograd\u5e93\u8fdb\u884c\u81ea\u52a8\u5fae\u5206\uff0c\u8ba1\u7b97\u68af\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u7528\u4e8e\u5bfc\u6570\u7684\u8ba1\u7b97\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u7684\u6548\u7387\u548c\u7cbe\u5ea6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u7b26\u53f7\u8ba1\u7b97\u548c\u6570\u503c\u8ba1\u7b97\uff0c\u4ee5\u53ca\u5229\u7528\u81ea\u52a8\u5fae\u5206\u6280\u672f\uff0c\u53ef\u4ee5\u89e3\u51b3\u5404\u79cd\u590d\u6742\u7684\u5bfc\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u51fd\u6570\u7684\u5bfc\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SymPy\u5e93\u6765\u8ba1\u7b97\u51fd\u6570\u7684\u5bfc\u6570\u3002SymPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u5904\u7406\u6570\u5b66\u8868\u8fbe\u5f0f\u7684\u5bfc\u6570\u3001\u79ef\u5206\u7b49\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5SymPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install sympy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\u5e76\u4f7f\u7528<code>diff()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5bfc\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import sympy as sp\n\nx = sp.symbols(&#39;x&#39;)\nf = x**2 + 3*x + 2\nderivative = sp.diff(f, x)\nprint(derivative)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\u51fd\u6570f\u5728x\u7684\u5bfc\u6570\uff0c\u53732x + 3\u3002<\/p>\n<p><strong>Python\u4e2d\u8ba1\u7b97\u5bfc\u6570\u662f\u5426\u652f\u6301\u9ad8\u9636\u5bfc\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u7684SymPy\u5e93\u652f\u6301\u9ad8\u9636\u5bfc\u6570\u7684\u8ba1\u7b97\u3002\u5728\u4f7f\u7528<code>diff()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bfc\u6570\u7684\u9636\u6570\u6765\u8ba1\u7b97\u9ad8\u9636\u5bfc\u6570\u3002\u4f8b\u5982\uff0c<code>sp.diff(f, x, 2)<\/code>\u5c06\u8ba1\u7b97\u51fd\u6570f\u7684\u4e8c\u9636\u5bfc\u6570\u3002\u8fd9\u4f7f\u5f97\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u5b66\u95ee\u9898\u65f6\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u83b7\u53d6\u6240\u9700\u7684\u5bfc\u6570\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u53ef\u89c6\u5316\u51fd\u6570\u53ca\u5176\u5bfc\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u53ef\u89c6\u5316\u51fd\u6570\u53ca\u5176\u5bfc\u6570\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u5b89\u88c5\u547d\u4ee4\u4e3a<code>pip install matplotlib<\/code>\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u4e00\u7ec4x\u503c\uff0c\u5e76\u8ba1\u7b97\u76f8\u5e94\u7684\u51fd\u6570\u503c\u548c\u5bfc\u6570\u503c\uff0c\u6700\u540e\u901a\u8fc7Matplotlib\u7ed8\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\nx_vals = np.linspace(-10, 10, 100)\nf = x_vals**2 + 3*x_vals + 2\nderivative = 2*x_vals + 3\n\nplt.plot(x_vals, f, label=&#39;f(x)&#39;)\nplt.plot(x_vals, derivative, label=&quot;f&#39;(x)&quot;, linestyle=&#39;--&#39;)\nplt.legend()\nplt.title(&#39;Function and its Derivative&#39;)\nplt.xlabel(&#39;x&#39;)\nplt.ylabel(&#39;y&#39;)\nplt.grid()\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u7ed8\u5236\u51fa\u51fd\u6570\u53ca\u5176\u5bfc\u6570\u7684\u56fe\u50cf\uff0c\u4fbf\u4e8e\u76f4\u89c2\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bfc\u6570\u7684\u8868\u8ff0\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u7b26\u53f7\u8ba1\u7b97\u5e93\u3001\u6570\u503c\u8ba1\u7b97\u5e93\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528SymPy [&hellip;]","protected":false},"author":3,"featured_media":953012,"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\/953007"}],"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=953007"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953007\/revisions"}],"predecessor-version":[{"id":953014,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953007\/revisions\/953014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953012"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}