• ()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()
  • 相关阅读:
    4、numpy——创建数组
    3、NumPy 数组属性
    2、NumPy 数据类型
    windos常见命令操作
    PHP操作MongoDB学习笔记
    MongoDB(八)Mongodb——GridFS存储
    MongoDB(七)MongoDb数据结构
    MongoDB(五)mongo语法和mysql语法对比学习
    MongoDB可视化工具RoboMongo----Windows安装 1
    MongoDB(四)mongodb设置用户访问权限
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270557.html
Copyright © 2020-2023  润新知