#### 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
```

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 条评论

1. LS说道：

😛 小锟同学好6666啊

2. 你好，我也是刚参加数学建模，老师说语言不限，就用了Python。我觉得关键还是熟悉哪些基本库中的API吧，需要绘图时够用就行，对于python数模来说算法和数据结构只是一部分。

3. ZmiL说道：

666

4. 同学，你好。我现在在学Python，准备参加去数模比赛，你能在学习Python的时候针对数模有哪些地方需要注意的方面给我一些建议吗？因为是文科生，数学不太好，算法数据结构方面脑子感觉有点跟不上。