简单的思考(一):MATLAB实现心形线

今天刷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;

结果:
在这里插入图片描述

相关推荐

  1. 绘制动态MATLAB代码

    2024-03-15 12:58:03       75 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 12:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 12:58:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 12:58:03       82 阅读
  4. Python语言-面向对象

    2024-03-15 12:58:03       91 阅读

热门阅读

  1. 程序员如何选择职业赛道?

    2024-03-15 12:58:03       43 阅读
  2. docker套娃实践(待续)

    2024-03-15 12:58:03       42 阅读
  3. Docker使用及部署流程

    2024-03-15 12:58:03       37 阅读
  4. C++for语句

    2024-03-15 12:58:03       43 阅读
  5. 探索Python人工智能编程之道:从入门到实战

    2024-03-15 12:58:03       39 阅读
  6. docker日志在哪看?怎么在Linux服务器中查看日志

    2024-03-15 12:58:03       48 阅读
  7. Springboot参数分组校验

    2024-03-15 12:58:03       36 阅读
  8. 阿里云服务 安装 Docker

    2024-03-15 12:58:03       43 阅读