提交 19997345 编写于 作者: S sunshine

🚀 add matlab code

上级 5dba0840
t=[0:0.1:2*pi]
x=16 * sin(t).^3
y=13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)
a=(x - min(x))/(max(x) - min(x))
b=(y - min(y))/(max(y) - min(y))
plot(x,y,'r')
\ No newline at end of file
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/001.jpg"/>
% matlab动态心形曲线(保存GIF格式)
clc;clear all;
a=10;
x=-2:0.01:2;
figure(1);
set(gcf,'position',[0,0,800,600],'color','w');
for i=1:100
str_title = strcat('\color{red}a=',num2str(a));
y = abs(x).^(2/3)+(0.9*sqrt((3.3-x.^2))).*sin(a*pi*x);
a=i/10;
figure(i);
set(gcf,'position',[0,0,800,600],'color','w');
p1 = plot(x,y,'r','LineWidth',3);
hold on;
title('\color{red}f(x)=x^2^/^3+e/3*(π-x^2)^1^/^2*sin(a*π*x)','fontsize',15);
text(-0.2,2.3,str_title,'FontName','Times New Roman','FontSize',20);
xlim([-2 2]);
ylim([-1.5 2.5]);
frame = getframe(gcf);
im = frame2im(frame);
[I,map] = rgb2ind(im,256);
if i==1
imwrite(I,map,'心形图.gif','gif','Loopcount',Inf,'DelayTime',0.05);
else
imwrite(I,map,'心形图.gif','gif','WriteMode','append','DelayTime',0.05);
end
close(figure(i));
end
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/002.jpg"/>
f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-3,3));
val=f(x,y,z);
[p,v]=isosurface(x,y,z,val,0);
patch('faces',p,'vertices',v,'facevertexcdata',jet(size(v,1)),'facecolor','w','edgecolor','flat');
view(3);grid on;axis equal;
\ No newline at end of file
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/003.jpg"/>
[X,Y,Z] = meshgrid(-1.2:.02:1.2,-3:.03:3,-1:.02:1.3);
f = smooth3((X.^2+9*Y.^2/9+Z.^2-1).^3-X.^2.*Z.^3-Y.^2.*Z.^3/20,'gauss');
v = uniquetol(getfield(isosurface(X,Y,Z,f,0),'vertices'),.15,'byrows',1);
n = isonormals(X,Y,Z,f,v);
ar = bsxfun(@rdivide,n*[0 -1 0;1 0 0;0 0 0],sqrt(sum(n.^2,2)));
[x,t] = meshgrid(0:.125:1,(-2:.2:10)*pi);
[p,q] = deal(pi/2*exp(-t/(8*pi)),1-(1-mod(3.6*t,2*pi)/pi).^4/2);
y = 2*(x.^2-x).^2.*sin(p);
[p,q] = deal(q.*(x.*sin(p)+y.*cos(p)),q.*(x.*cos(p)-y.*sin(p)));
figure color w, axis image vis3d off
h = surface(p.*cos(t),p.*sin(t),q,'EdgeColor','n','FaceColor','r');
arrayfun(@(t,i)copyobj(h,hgtransform('Mat',makehgtform('translate',5*v(i,:),...
'axisrotate',ar(i,:),t))),asin(sqrt(sum(ar.^2,2)))',1:size(v,1))
view(32,12), set(camlight('head'),'color',[1 0.84 0.6]), lighting gouraud
\ No newline at end of file
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/004.jpg"/>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/005.jpg"/>
f=@(x,y,z)(x.^2+ (9./4).*y.^2 + z.^2 - 1).^3 - x.^2.*z.^3 - (9./80).*y.^2.*z.^3;
[x,y,z]=meshgrid(linspace(-1.5,1.5));
val=f(x,y,z);
isosurface(x,y,z,val,0);
axis equal;view(3);colormap([1 0.2 0.2])
%跳动爱心
clear; clc; close all;
% NOTICE: Your MATLAB version should be at least R2019a !
% --------------------------- functions -------------------------------
f = @(x, y, z)(x.^2 + 2.25*y.^2 + z.^2 - 1).^3 - ...
x.^2.* z.^3 - 0.1125*y.^2.*z.^3;
g = @(x, y, z)(sqrt(x.^2+y.^2)-2.5).^2 + z.^2 - 0.4^2;
% -------------------------- generate data ----------------------------
t = linspace(-5, 5);
[x1, y1, z1] = meshgrid(t);
[x2, y2, z2] = meshgrid(t);
val1 = f(x1, y1, z1);
val2 = g(x2, y2, z2);
[p1, v1] = isosurface(x1, y1, z1, val1, 0);
[p2, v2] = isosurface(x2, y2, z2, val2, 0);
% --------------------------- basic plot ------------------------------
figure()
subplot(1, 1, 1)
h = patch('faces',p1,'vertices',v1,'facevertexcdata',jet(size(v1,1)),...
'facecolor','w','edgecolor','flat'); hold on;
patch('faces',p2,'vertices',v2,'facevertexcdata',jet(size(v2,1)),...
'facecolor','w','edgecolor','flat');
grid on; axis equal; axis([-3,3,-3,3,-1.5,1.5]); view(3)
title(["$(x^2+\frac{9}{4}y^2+z^2-1)^3-x^2z^3-\frac{9}{80}y^2z^3=0$",...
"$(\sqrt{x^2+y^2}-R)^2 +z^2 = r^2$"],'Interpreter','latex','position',[3.3,4])
warning('off');
% 请在此处进行您的演讲!% 请在此处进行您的演讲!
T = suptitle("$I\ Love\ U\ !$");
% 请在此处进行您的演讲!% 请在此处进行您的演讲!
set(T,'Interpreter','latex','FontSize',24)
% -------------------------- generate gif -----------------------------
pic_num = 1;
for i = 1:20
v1 = 0.98 * v1;
set(h, 'vertices', v1); drawnow;
F = getframe(gcf);
I = frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'BeatingHeart.gif','gif','Loopcount',inf,'DelayTime',0.05);
else
imwrite(I,map,'BeatingHeart.gif','gif','WriteMode','append','DelayTime',0.05);
end
pic_num = pic_num + 1;
end
for i = 1:20
v1 = v1 / 0.98;
set(h, 'vertices', v1); drawnow;
F = getframe(gcf);
I = frame2im(F);
[I,map] = rgb2ind(I,256);
imwrite(I,map,'BeatingHeart.gif','gif','WriteMode','append','DelayTime',0.05);
pic_num = pic_num + 1;
end
% ----------------------------- dynamic -------------------------------
while true
for i = 1:20
v1 = 0.98 * v1;
set(h, 'vertices', v1); drawnow;
end
for i = 1:20
v1 = v1 / 0.98;
set(h, 'vertices', v1); drawnow;
end
end
% ------------------------------- end ---------------------------------
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/006.jpg"/>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/007.jpg"/>
function rose
grid on
[x,t]=meshgrid((0:24)./24,(0:0.5:575)./575.*20.*pi-4*pi);
p=(pi/2)*exp(-t./(8*pi));
change=sin(20*t)/150;
u=1-(1-mod(3.3*t,2*pi)./pi).^4./2+change;
y=2*(x.^2-x).^2.*sin(p);
r=u.*(x.*sin(p)+y.*cos(p)).*1.5;
h=u.*(x.*cos(p)-y.*sin(p));
map=[1.0000 0.6471 0.8275
0.9984 0.6353 0.8130
0.9969 0.6236 0.7985
0.9953 0.6118 0.7840
0.9937 0.6000 0.7695
0.9921 0.5882 0.7550
0.9906 0.5765 0.7404
0.9890 0.5647 0.7259
0.9874 0.5529 0.7114
0.9859 0.5412 0.6969
0.9843 0.5294 0.6824
0.9757 0.5149 0.6730
0.9670 0.5004 0.6636
0.9584 0.4859 0.6541
0.9498 0.4714 0.6447
0.9411 0.4568 0.6353
0.9325 0.4423 0.6259
0.9239 0.4278 0.6165
0.9153 0.4133 0.6070
0.9066 0.3988 0.5976
0.8980 0.3843 0.5882
0.8937 0.3780 0.5756
0.8894 0.3718 0.5631
0.8851 0.3655 0.5505
0.8808 0.3592 0.5380
0.8764 0.3529 0.5254
0.8721 0.3467 0.5129
0.8678 0.3404 0.5003
0.8635 0.3341 0.4878
0.8592 0.3279 0.4752
0.8549 0.3216 0.4627
0.8561 0.3165 0.4596
0.8573 0.3114 0.4564
0.8584 0.3063 0.4533
0.8596 0.3012 0.4502
0.8608 0.2961 0.4471
0.8620 0.2910 0.4439
0.8632 0.2859 0.4408
0.8643 0.2808 0.4377
0.8655 0.2757 0.4345
0.8667 0.2706 0.4314
0.8549 0.2620 0.4165
0.8432 0.2533 0.4016
0.8314 0.2447 0.3867
0.8196 0.2361 0.3718
0.8078 0.2274 0.3569
0.7961 0.2188 0.3420
0.7843 0.2102 0.3271
0.7725 0.2016 0.3122
0.7608 0.1929 0.2973
0.7490 0.1843 0.2824
0.7553 0.1827 0.2855
0.7616 0.1812 0.2887
0.7678 0.1796 0.2918
0.7741 0.1780 0.2949
0.7804 0.1764 0.2980
0.7867 0.1749 0.3012
0.7930 0.1733 0.3043
0.7992 0.1717 0.3074
0.8055 0.1702 0.3106
0.8118 0.1686 0.3137
0.7977 0.1631 0.3023
0.7836 0.1576 0.2910
0.7694 0.1521 0.2796
0.7553 0.1466 0.2682
0.7412 0.1411 0.2569
0.7271 0.1357 0.2455
0.7130 0.1302 0.2341
0.6988 0.1247 0.2227
0.6847 0.1192 0.2114
0.6706 0.1137 0.2000
0.6686 0.1141 0.1996
0.6667 0.1145 0.1992
0.6647 0.1149 0.1988
0.6628 0.1153 0.1984
0.6608 0.1157 0.1981
0.6588 0.1160 0.1977
0.6569 0.1164 0.1973
0.6549 0.1168 0.1969
0.6530 0.1172 0.1965
0.6510 0.1176 0.1961];
set(gca,'CameraPosition',[2 2 2])
hold on
Xset=r.*cos(t);Yset=r.*sin(t);
sf=surface(Xset,Yset,h,'EdgeAlpha',0.1,...
'EdgeColor',[0.5 0.5 0.5],'FaceColor','interp');
colormap(map)
theta=0;
while 1
theta=theta+0.02;
set(sf,'XData',Xset.*cos(theta)-Yset.*sin(theta),...
'YData',Xset.*sin(theta)+Yset.*cos(theta))
pause(0.01)
end
end
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/008.jpg"/>
function roseBall
clear;clc
%曲面数据计算
%==========================================================================
[x,t]=meshgrid((0:24)./24,(0:0.5:575)./575.*20.*pi+4*pi);
p=(pi/2)*exp(-t./(8*pi));
change=sin(15*t)/150;
u=1-(1-mod(3.6*t,2*pi)./pi).^4./2+change;
y=2*(x.^2-x).^2.*sin(p);
r=u.*(x.*sin(p)+y.*cos(p));
h=u.*(x.*cos(p)-y.*sin(p));
%颜色映射表
%==========================================================================
hMap=(h-min(min(h)))./(max(max(h))-min(min(h)));
col=size(hMap,2);
colorList=[0.0200 0.0400 0.3900
0 0.0900 0.5800
0 0.1300 0.6400
0.0200 0.0600 0.6900
0 0.0800 0.7900
0.0100 0.1800 0.8500
0 0.1300 0.9600
0.0100 0.2600 0.9900
0 0.3500 0.9900
0.0700 0.6200 1.0000
0.1700 0.6900 1.0000];
% colorList=[0.2100 0.0900 0.3800
% 0.2900 0.0700 0.4700
% 0.4000 0.1100 0.4900
% 0.5500 0.1600 0.5100
% 0.7500 0.2400 0.4700
% 0.8900 0.3200 0.4100
% 0.9700 0.4900 0.3700
% 1.0000 0.5600 0.4100
% 1.0000 0.6900 0.4900
% 1.0000 0.8200 0.5900
% 0.9900 0.9200 0.6700
% 0.9800 0.9500 0.7100];
% colorList1=[0.2000 0.0800 0.4300
% 0.2000 0.1300 0.4600
% 0.2000 0.2100 0.5000
% 0.2000 0.2800 0.5300
% 0.2000 0.3700 0.5800
% 0.1900 0.4500 0.6200
% 0.2000 0.4800 0.6400
% 0.1900 0.5400 0.6700
% 0.1900 0.5700 0.6900
% 0.1900 0.7500 0.7800
% 0.1900 0.8000 0.8100
% ];
% colorList2=[0.1300 0.1000 0.1600
% 0.2000 0.0900 0.2000
% 0.2800 0.0800 0.2300
% 0.4200 0.0800 0.3000
% 0.5100 0.0700 0.3400
% 0.6600 0.1200 0.3500
% 0.7900 0.2200 0.4000
% 0.8800 0.3500 0.4700
% 0.9000 0.4500 0.5400
% 0.8900 0.7800 0.7900
% ];
% colorList3=[0.3200 0.3100 0.7600
% 0.3800 0.3400 0.7600
% 0.5300 0.4200 0.7500
% 0.6400 0.4900 0.7300
% 0.7200 0.5500 0.7200
% 0.7900 0.6100 0.7100
% 0.9100 0.7100 0.6800
% 0.9800 0.7600 0.6700
% ];
% colorList4=[0.9500 0.2300 0.6600
% 0.7500 0.2100 0.6000
% 0.6200 0.2000 0.5700
% 0.4500 0.1800 0.5200
% 0.3200 0.2100 0.5200
% 0.2700 0.3100 0.6000
% 0.2500 0.3600 0.6400
% 0.1900 0.4800 0.7400
% ];
colorFunc=colorFuncFactory(colorList);
dataMap=colorFunc(hMap');
colorMap(:,:,1)=dataMap(:,1:col);
colorMap(:,:,2)=dataMap(:,col+1:2*col);
colorMap(:,:,3)=dataMap(:,2*col+1:3*col);
function colorFunc=colorFuncFactory(colorList)
xx=(0:size(colorList,1)-1)./(size(colorList,1)-1);
y1=colorList(:,1);y2=colorList(:,2);y3=colorList(:,3);
colorFunc=@(X)[interp1(xx,y1,X,'linear')',interp1(xx,y2,X,'linear')',interp1(xx,y3,X,'linear')'];
end
%曲面旋转及绘制
%==========================================================================
surface(r.*cos(t),r.*sin(t),h+0.35,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMap)
hold on
surface(r.*cos(t),r.*sin(t),-h-0.35,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMap)
Xset=r.*cos(t);
Yset=r.*sin(t);
Zset=h+0.35;
yaw_z=pi*72/180;
roll_x=pi-acos(-1/sqrt(5));
R_z_2=[cos(yaw_z),-sin(yaw_z),0;
sin(yaw_z),cos(yaw_z),0;
0,0,1];
R_z_1=[cos(yaw_z/2),-sin(yaw_z/2),0;
sin(yaw_z/2),cos(yaw_z/2),0;
0,0,1];
R_x_2=[1,0,0;
0,cos(roll_x),-sin(roll_x);
0,sin(roll_x),cos(roll_x)];
[nX,nY,nZ]=rotateXYZ(Xset,Yset,Zset,R_x_2);
surface(nX,nY,nZ,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMap)
for k=1:4
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z_2);
surface(nX,nY,nZ,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMap)
end
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z_1);
for k=1:5
[nX,nY,nZ]=rotateXYZ(nX,nY,nZ,R_z_2);
surface(nX,nY,-nZ,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMap)
end
%--------------------------------------------------------------------------
function [nX,nY,nZ]=rotateXYZ(X,Y,Z,R)
nX=zeros(size(X));
nY=zeros(size(Y));
nZ=zeros(size(Z));
for i=1:size(X,1)
for j=1:size(X,2)
v=[X(i,j);Y(i,j);Z(i,j)];
nv=R*v;
nX(i,j)=nv(1);
nY(i,j)=nv(2);
nZ(i,j)=nv(3);
end
end
end
%axes属性调整
%==========================================================================
ax=gca;
grid on
ax.GridLineStyle='--';
ax.LineWidth=1.2;
ax.XColor=[1,1,1].*0.4;
ax.YColor=[1,1,1].*0.4;
ax.ZColor=[1,1,1].*0.4;
ax.DataAspectRatio=[1,1,1];
ax.DataAspectRatioMode='manual';
ax.CameraPosition=[-6.5914 -24.1625 -0.0384];
end
function LoveFunc
LoveFunchdl=@(x,a)(x.^2).^(1/3)+0.9.*((3.3-x.^2).^(1/2)).*sin(a.*pi.*x);
hold on
grid on
axis([-3 3,-2 4])
x=-1.8:0.005:1.8;
text(0,3.3,'$f(x)=x^{\frac{2}{3}}+0.9(3.3-x^2)^{\frac{1}{2}}\sin(\alpha\pi x)$',...
'FontSize',13,'HorizontalAlignment','center','Interpreter','latex');
txt2=text(-0.35,2.9,'','FontSize',13,'HorizontalAlignment','left','Interpreter','latex','tag','alphadata');
for a=1:0.01:20
delete(findobj('type','line'))
AlphaString=['$\alpha=',num2str(a),'$'];
Color=([1.0000 0.4902 0.6627]-[0.2118 0.4667 0.9961]).*(a/20)+[0.2118 0.4667 0.9961];
set(txt2,'string',AlphaString)
plot(x,LoveFunchdl(x,a),'color',Color,'LineWidth',1.2);
pause(0.003)
end
end
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/009.jpg"/>
......@@ -267,6 +267,74 @@
</tr>
</table>
# MatLab
<table align="center">
<!-- 第一行 -->
<tr>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/001-just_heart">
<p align="center">001-just_heart</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/001.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/002-heart">
<p align="center">002-heart</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/002.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/003-heart_3d">
<p align="center">003-heart_3d</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/003.jpg"/>
</a>
</td>
</tr>
<!-- 第二行 -->
<tr>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/004-my_heart">
<p align="center">004-my_heart</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/004.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/005-red_heart">
<p align="center">005-red_heart</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/005.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/006-love_heart">
<p align="center">006-love_heart</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/006.jpg"/>
</a>
</td>
</tr>
<!-- 第三行 -->
<tr>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/007-rose">
<p align="center">007-rose</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/007.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/008-roseBall">
<p align="center">008-roseBall</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/008.jpg"/>
</a>
</td>
<td valign="top">
<a href="https://github.com/sun0225SUN/Awesome-Love-Code/tree/main/MatLab/009-LoveFunc">
<p align="center">009-LoveFunc</p>
<img src="https://cdn.jsdelivr.net/gh/sun0225SUN/Awesome-Love-Code/assets/img/matlab/009.jpg"/>
</a>
</td>
</tr>
</table>
# C#
<table align="center">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册