今天刷B站的时候看见了:
于是想着自己能不能也做出来
clc;clear;
% 定义x的范围
x = -2:0.01:2;
% 初始化图形
figure;
set(gcf,'position',[0,0,800,600],'color','w');
h1 = plot(x,abs(x).^(2/3) + (0.9*sqrt((3.3-x.^2))).*sin(0.1*pi*x),'r','LineWidth',3);
hold on
xlim([-2 2]);
ylim([-1.5 2.5]);
title('\color{red}f(x)=x^2^/^3+e/3*(π-x^2)^1^/^2*sin(a*π*x)','fontsize',20)
str_title= strcat ('\color{red}a=',num2str(0.1));
text(-0.2,2.3,str_title,'FontName','Times New Roman', 'FontSize',20);
filename = '心形线.gif'; % GIF文件名
% 创建动画
for a = 0.2:0.1:20
% 更新数据
y1 = abs(x).^(2/3) + (0.9*sqrt((3.3-x.^2))).*sin(a*pi*x);
% 删除旧的文本对象
delete(findobj('Type','text'));
% 添加新的文本
str_title= strcat ('\color{red}a=',num2str(a));
text(-0.2,2.3,str_title,'FontName','Times New Roman', 'FontSize',20);
% 更新图形
set(h1, 'YData', real(y1));
% 更新图形窗口
drawnow;
% 捕获当前帧
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% 写入GIF文件
if a == 0.2
imwrite(imind,cm,filename,'gif', 'Loopcount',inf, 'DelayTime',0.01);
else
imwrite(imind,cm,filename,'gif','WriteMode','append', 'DelayTime',0.01);
end
% 暂停
pause(0.01);
end
% 关闭hold状态
hold off;
结果: