AI编程正在让小白瞬间成为高手(ai软件编程)
AI编程太让人亮眼了。
看到一个好玩的MATLAB程序,可以生成游动的水母,
公式如下:
给出的MATLAB程序如下:
- x=0:1:14*2*pi; y=0:1:30*2*pi;
- [x y]=meshgrid(x, y); x=x(:); y=y(:);
- k=5*cos(x/14).*cos(y/30);
- e=y/8-13;
- a=atan2(k,e);
- d=(k.^2+e.^2)/59+4;
- for t=0:1:120
- c=d/2+e/99-t/18;
- q=60-sin(a.*e)+k.*(3+4./d.*sin(d.^2-2*t));
- X=q.*sin(c); Y=(q+9*d).*cos(c);
- h=plot(X, Y, '.'); set(h, 'markersize', 1); set(gcf,'color','w');
- xlim([-150 150]); ylim([-150 150]); axis off;
- % if(t==0) pars={'LoopCount', inf}; else pars={'WriteMode', 'append'};end
- % frame=getframe(1); im=frame2im(frame); [img, map]=rgb2ind(im, 256);
- % imwrite(img, map, 'jellyfish.gif', 'gif', pars{:}, 'DelayTime', 0.1);
- pause(0.1);
- end
程序用豆包AI编程可以转成python:
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 生成x和y数据
x = np.arange(0, 14 * 2 * np.pi + 1, 1) # 0到14*2π,步长为1
y = np.arange(0, 30 * 2 * np.pi + 1, 1) # 0到30*2π,步长为1
# 创建网格并展平为一维数组
x_grid, y_grid = np.meshgrid(x, y)
x_flat = x_grid.flatten()
y_flat = y_grid.flatten()
# 计算初始参数
k = 5 * np.cos(x_flat / 14) * np.cos(y_flat / 30)
e = y_flat / 8 - 13
a = np.arctan2(k, e)
d = (k ** 2 + e ** 2) / 59 + 4
# 设置图形
fig, ax = plt.subplots(figsize=(8, 8))
fig.patch.set_facecolor('white') # 设置背景为白色
scat = ax.scatter([], [], s=1, marker='.') # 初始散点图
ax.set_xlim(-150, 150)
ax.set_ylim(-150, 150)
ax.axis('off') # 关闭坐标轴
# 动画更新函数
def update(t):
c = d / 2 + e / 99 - t / 18
# 处理d为0的情况,避免除零错误
with np.errstate(divide='ignore', invalid='ignore'):
q = 60 - np.sin(a * e) + k * (3 + 4 / d * np.sin(d ** 2 - 2 * t))
X = q * np.sin(c)
Y = (q + 9 * d) * np.cos(c)
# 更新散点图数据
scat.set_offsets(np.column_stack((X, Y)))
return scat,
# 创建动画
ani = FuncAnimation(fig, update, frames=np.arange(0, 121),
interval=100, blit=True)
plt.show()
# 如果需要保存为GIF,请取消下面的注释
#ani.save(r'e:\jellyfish.gif', writer='pillow', fps=10)
如果人工比照 MATLAB程序写python,高手也要半个小时以上,慢一点要一天,AI几乎瞬间完成。