• (转载)Python在数学建模中的简单应用


    1.方程求导

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import scipy.misc
     
    def f(x): return 2*x*x + 3*x + 1
    print(sp.misc.derivative(f, 2))</pre>

    2.求不定积分

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import scipy.integrate
     
    f = lambda x : x**2
    print(sp.integrate.quad(f, 0, 2))
    print(sp.integrate.fixed_quad(f, 0, 2))

    3.求解非线性方程组

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import scipy.optimize
     
    def f(x):
        return [5*x[1] + 3, 4*x[0]*x[0], x[1]*x[2] - 1.5]
    ans = sp.optimize.fsolve(f, [0, 0, 0])
    print(ans)
    print(f(ans))

    4.求解线性方程组

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import matplotlib.pylab as plt
    import scipy.linalg
     
    a = np.array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
    b = np.array([10, 8, 3])
    print(sp.linalg.solve(a, b))
    # print(sp.linalg.inv(a).dot(b))

    5.画3D马鞍面

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import matplotlib.pylab as plt
    from pylab import *
    from mpl_toolkits.mplot3d import Axes3D
     
    fig = figure()
    ax = Axes3D(fig)
    X = np.linspace(-10, 10, 30)
    Y = np.linspace(-10, 10, 30)
    X, Y = np.meshgrid(X, Y)
    Z = (X*X)/3 - (Y*Y)/3
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=&#39;hot&#39;)
    show()

    6.画三叶玫瑰线

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import matplotlib.pylab as plt
     
    t = np.linspace(0, 2*np.pi, 1000, endpoint=True)
    p = 3 * np.sin(3 * t)
    plt.subplot(111, polar=True)
    plt.plot(t, p, lw=3, color=&#39;g&#39;)
    plt.grid(True)
    plt.show()

    7.画星形线

    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import matplotlib.pylab as plt
     
    t = np.linspace(-10, 10, 1024)
    X = np.cos(t) ** 3
    Y = np.sin(t) ** 3
    plt.plot(X, Y, label=&#39;My&#39;, lw=3, color=&#39;g&#39;)
    plt.xlim(X.min()*1.2, X.max()*1.2)
    plt.ylim(Y.min()*1.2, Y.max()*1.2)
    plt.grid(True)
    plt.legend()
    plt.show()
     8.画随机散点图
    from __future__ import print_function
    from __future__ import division
    import numpy as np
    import scipy as sp
    import numpy.random
    import matplotlib.pylab as plt
     
    X = np.random.rand(50)
    Y = np.random.rand(50)
    colors = np.random.rand(50)
    area = np.pi * (15 * np.random.rand(50)) ** 2
     
    plt.scatter(X, Y, s=area, c=colors)
    plt.show()
  • 相关阅读:
    nginx部分功能配置备忘
    mysql 截取数据 组合排序
    java实现排列组合
    每日记载内容总结48
    http请求转换为https请求
    每日记载内容总结47
    rabbitmq 基本操作
    redis免密登录被入侵解决方式
    支付宝支付sign_type从RSA到RSA2遇到的问题
    Java面试题2
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/12190245.html
Copyright © 2020-2023  润新知