[转载]Matlab有趣代码

转自 http://blog.csdn.net/Tonor/article/details/52749891

一、彩蛋

xpbombs; %扫雷

vibes; %振膜

teapotdemo; %茶壶

logo; %Matlab logo

travel; %路线问题

life; %Convay的人生小游戏

makevase; %造花瓶

truss; %弹弹弹

codec; %编码

fifteen; %拼图

xpquad; %超二次曲面

wrldtrv; %坐飞机

spy;  %不加参数就画个狗

earthmap; %地图

cplxdemo; %复杂的XYZ立体图形~黎曼曲面的平面效果

lorenz; %Lorenz吸引子动画显示

二、代码段

% 画爱心
[X, Y] = meshgrid(0.01:0.01:1, 0.01:0.01:1); 
Zfun =@(x,y)12.5*x.*log10(x).*y.*(y-1)+exp(-((25 ... 
*x - 25/exp(1)).^2+(25*y-25/2).^2).^3)./25; 
Z = Zfun(X,Y); 
figure; 
surf(Y,Z,X,'FaceColor',[1 0.75 0.65],'linestyle','none'); 
hold on 
surf(Y+0.98,Z,X,'FaceColor',[1 0.75 0.65],'linestyle','none'); 
axis equal; 
view([116 30]); 
camlight; 
lighting phong; % 设置光照和光照模式
% 说话
function wav = matlabspeech(txt,voice,pace,fs)
% matlabspeech('I Love Maple and Matlab')
% matlabspeech('I Love Maple')
% matlabspeech('I Love Matlab')
if ~ispc, error('要求使用Microsoft Win32 SAPI .'); end
if ~ischar(txt), error('第一个参数是字符串.'); end
SV = actxserver('SAPI.SpVoice');
TK = invoke(SV,'GetVoices');
if nargin > 1

for k = 0:TK.Count-1
if strcmpi(voice,TK.Item(k).GetDescription)
SV.Voice = TK.Item(k);
break;
elseif strcmpi(voice,'list')
disp(TK.Item(k).GetDescription);
end
end

if nargin > 2
if isempty(pace), pace = 0; end
if abs(pace) > 10, pace = sign(pace)*10; end
SV.Rate = pace;
end
end
if nargin < 4 || ~ismember(fs,[8000,11025,12000,16000,22050,24000,32000,...
44100,48000]), fs = 16000; end
if nargout > 0

MS = actxserver('SAPI.SpMemoryStream');
MS.Format.Type = sprintf('SAFT%dkHz16BitMono',fix(fs/1000));
SV.AudioOutputStream = MS;
end
invoke(SV,'Speak',txt);
if nargout > 0

wav = reshape(double(invoke(MS,'GetData')),2,[])';
wav = (wav(:,2)*256+wav(:,1))/32768;
wav(wav >= 1) = wav(wav >= 1)-2;
delete(MS);
clear MS;
end
delete(SV);
clear SV TK;
pause(0.2);
end
% 最炫民族风
fs = 44100; % sample rate
dt = 1/fs;
T16 = 0.125;
t16 = [0:dt:T16];
[temp k] = size(t16);
t4 = linspace(0,4*T16,4*k);
t8 = linspace(0,2*T16,2*k);
[temp i] = size(t4);
[temp j] = size(t8);
% Modification functions
mod4=(t4.^4).*exp(-30*(t4.^0.5));
mod4=mod4*(1/max(mod4));
mod8=(t8.^4).*exp(-50*(t8.^0.5));
mod8=mod8*(1/max(mod8));
mod16=(t16.^4).*exp(-90*(t16.^0.5));
mod16=mod16*(1/max(mod16));
f0 = 2*146.8; % reference frequency
ScaleTable = [2/3 3/4 5/6 15/16 ...
1 9/8 5/4 4/3 3/2 5/3 9/5 15/8 ...
2 9/4 5/2 8/3 3 10/3 15/4 4 ...
1/2 9/16 5/8];
% 1/4 notes
do0f = mod4.*cos(2*pi*ScaleTable(21)*f0*t4);
re0f = mod4.*cos(2*pi*ScaleTable(22)*f0*t4);
mi0f = mod4.*cos(2*pi*ScaleTable(23)*f0*t4);
fa0f = mod4.*cos(2*pi*ScaleTable(1)*f0*t4);
so0f = mod4.*cos(2*pi*ScaleTable(2)*f0*t4);
la0f = mod4.*cos(2*pi*ScaleTable(3)*f0*t4);
ti0f = mod4.*cos(2*pi*ScaleTable(4)*f0*t4);
do1f = mod4.*cos(2*pi*ScaleTable(5)*f0*t4);
re1f = mod4.*cos(2*pi*ScaleTable(6)*f0*t4);
mi1f = mod4.*cos(2*pi*ScaleTable(7)*f0*t4);
fa1f = mod4.*cos(2*pi*ScaleTable(8)*f0*t4);
so1f = mod4.*cos(2*pi*ScaleTable(9)*f0*t4);
la1f = mod4.*cos(2*pi*ScaleTable(10)*f0*t4);
tb1f = mod4.*cos(2*pi*ScaleTable(11)*f0*t4);
ti1f = mod4.*cos(2*pi*ScaleTable(12)*f0*t4);
do2f = mod4.*cos(2*pi*ScaleTable(13)*f0*t4);
re2f = mod4.*cos(2*pi*ScaleTable(14)*f0*t4);
mi2f = mod4.*cos(2*pi*ScaleTable(15)*f0*t4);
fa2f = mod4.*cos(2*pi*ScaleTable(16)*f0*t4);
so2f = mod4.*cos(2*pi*ScaleTable(17)*f0*t4);
la2f = mod4.*cos(2*pi*ScaleTable(18)*f0*t4);
ti2f = mod4.*cos(2*pi*ScaleTable(19)*f0*t4);
do3f = mod4.*cos(2*pi*ScaleTable(20)*f0*t4);
blkf = zeros(1,i);
% 1/8 notes
do0e = mod8.*cos(2*pi*ScaleTable(21)*f0*t8);
re0e = mod8.*cos(2*pi*ScaleTable(22)*f0*t8);
mi0e = mod8.*cos(2*pi*ScaleTable(23)*f0*t8);
fa0e = mod8.*cos(2*pi*ScaleTable(1)*f0*t8);
so0e = mod8.*cos(2*pi*ScaleTable(2)*f0*t8);
la0e = mod8.*cos(2*pi*ScaleTable(3)*f0*t8);
ti0e = mod8.*cos(2*pi*ScaleTable(4)*f0*t8);
do1e = mod8.*cos(2*pi*ScaleTable(5)*f0*t8);
re1e = mod8.*cos(2*pi*ScaleTable(6)*f0*t8);
mi1e = mod8.*cos(2*pi*ScaleTable(7)*f0*t8);
fa1e = mod8.*cos(2*pi*ScaleTable(8)*f0*t8);
so1e = mod8.*cos(2*pi*ScaleTable(9)*f0*t8);
la1e = mod8.*cos(2*pi*ScaleTable(10)*f0*t8);
tb1e = mod8.*cos(2*pi*ScaleTable(11)*f0*t8);
ti1e = mod8.*cos(2*pi*ScaleTable(12)*f0*t8);
do2e = mod8.*cos(2*pi*ScaleTable(13)*f0*t8);
re2e = mod8.*cos(2*pi*ScaleTable(14)*f0*t8);
mi2e = mod8.*cos(2*pi*ScaleTable(15)*f0*t8);
fa2e = mod8.*cos(2*pi*ScaleTable(16)*f0*t8);
so2e = mod8.*cos(2*pi*ScaleTable(17)*f0*t8);
la2e = mod8.*cos(2*pi*ScaleTable(18)*f0*t8);
ti2e = mod8.*cos(2*pi*ScaleTable(19)*f0*t8);
do3e = mod8.*cos(2*pi*ScaleTable(20)*f0*t8);
blke = zeros(1,j);
% 1/16 notes
do0s = mod16.*cos(2*pi*ScaleTable(21)*f0*t16);
re0s = mod16.*cos(2*pi*ScaleTable(22)*f0*t16);
mi0s = mod16.*cos(2*pi*ScaleTable(23)*f0*t16);
fa0s = mod16.*cos(2*pi*ScaleTable(1)*f0*t16);
so0s = mod16.*cos(2*pi*ScaleTable(2)*f0*t16);
la0s = mod16.*cos(2*pi*ScaleTable(3)*f0*t16);
ti0s = mod16.*cos(2*pi*ScaleTable(4)*f0*t16);
do1s = mod16.*cos(2*pi*ScaleTable(5)*f0*t16);
re1s = mod16.*cos(2*pi*ScaleTable(6)*f0*t16);
mi1s = mod16.*cos(2*pi*ScaleTable(7)*f0*t16);
fa1s = mod16.*cos(2*pi*ScaleTable(8)*f0*t16);
so1s = mod16.*cos(2*pi*ScaleTable(9)*f0*t16);
la1s = mod16.*cos(2*pi*ScaleTable(10)*f0*t16);
tb1s = mod16.*cos(2*pi*ScaleTable(11)*f0*t16);
ti1s = mod16.*cos(2*pi*ScaleTable(12)*f0*t16);
do2s = mod16.*cos(2*pi*ScaleTable(13)*f0*t16);
re2s = mod16.*cos(2*pi*ScaleTable(14)*f0*t16);
mi2s = mod16.*cos(2*pi*ScaleTable(15)*f0*t16);
fa2s = mod16.*cos(2*pi*ScaleTable(16)*f0*t16);
so2s = mod16.*cos(2*pi*ScaleTable(17)*f0*t16);
la2s = mod16.*cos(2*pi*ScaleTable(18)*f0*t16);
ti2s = mod16.*cos(2*pi*ScaleTable(19)*f0*t16);
do3s = mod16.*cos(2*pi*ScaleTable(20)*f0*t16);
blks = zeros(1,k);
% Melody by Schau_mal 
part0 = [mi1f la0e la0e do1f mi1f ...
re1e re1s mi1s re1e do1e re1e do1e la0f ...
mi1f la0e la0e do1f mi1f ...
so1e re1s mi1s re1e do1e re1e do1e ti0e so0e ...
mi1f la0e la0e do1f mi1f ...
re1e re1s mi1s re1e do1e re1e do1e la0e so0e ...
mi1f la0e la0e do1f mi1f ...
so1e mi1e blkf blkf blkf ...
];
part1 = [la0f la0e so0e la0f la0e do1e ...
do1f re1e do1e la0f la0f ...
do1f do1e so0e do1e re1e mi1e so1e ...
so1e mi1e re1f mi1f mi1f ...
la1e la1e la1e so1e mi1e mi1f do1e ...
la0e la0e la0e mi1e re1s mi1s re1e re1f ...
mi1e mi1e so1e mi1e re1e mi1e re1e do1e ...
la0f so0f la0f la0f ...
];
part2 = [mi1e mi1e so1e mi1e mi1e so1e so1e la1e ... 
do2e la1e so1f la1s do2s la1e la1f ...
la0f la0e so0e la0f do1f ...
re1e mi1s re1s do1e re1e mi1f mi1f ...
la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...
mi1f mi1f blke blke blkf ...
do1e la0e la0e do1e re1f so0e so0e ...
mi1e so1e mi1e re1e do1f do1f ...
la0e do1e re1e mi1e re1e do1e so0e mi0e ...
la0f la0f blke blke blkf ...
];
part3 = [la0f la0e so0e la0f do1f ...
re1e mi1s re1s do1e re1e mi1f mi1f ...
la0e la1e la1e so1e re1e mi1s re1s do1e re1e ...
mi1f mi1f blke blke blkf ...
do1e la0e la0e do1e re1f so0e so0e ...
mi1e so1e mi1e re1e do1f do1e do1e ...
la0e do1e re1e mi1e so1e mi1e mi1e so1e ...
la1f la1f la1f la1f ...
];
part4 = [la1e la1s la1s la1e la1e la1e la1s so1s mi1e re1e ...
re1e re1s re1s mi1e mi1s so1s mi1e mi1s re1s do1e do1s la0s ...
la0f la0e so0e la0f la0e do1e ...
re1e mi1s re1s do1e re1e mi1f mi1f ...
la1e so1e mi1e re1e so1e mi1e re1e do1e ...
do1f do1f la0s do1s re1s mi1s re1s do1s la0s do1s
];
part5 = [do2e do2s do2s la1e la1s la1s so1e so1s so1s mi1e mi1s mi1s ...
re1e mi1s re1s do1e la0s so0s la0s so0s do1s re1s mi1s so1s la1s re2s ...
do2f do2f blks blks blks blks do1e re1e ...
mi1f mi1f mi1f so1e mi1e ...
la1f la1f la1e do1e so1e mi1e ...
re1f re1e re1s re1s re1e re1e do1e re1e ...
mi1f mi1e mi1s mi1s mi1e re1s do1s ti0e do1s re1s ...
mi1f mi1f mi1f so1e mi1e ...
do2f la1f la1f la1e do1e ...
re1f so1f so1f la1f ...
ti1f ti1f ti1f ti1f ...
];
part6 = [blkf blkf mi1e so1e mi1e so1e ...
mi1f la0e la0s la0s do1f la0e mi1s la0s ...
do1e do1s do1s re1e do1s re1s mi1f mi1f ...
mi1f la0e la0s la0s so1f re1e re1s re1s ...
mi1f mi1f mi1s re1s do1s la0s mi0s re0s mi0s so0s ...
do1f la0e la0s la0s re1f so0e so0s so0s ...
mi0f so0e so0s so0s do1f do1f ...
la0f do1e do1s la0s mi1e mi1s mi1s re1e re1s mi1s ...
];
% Combination, v1 is complete version, v2 is simple version.
v1 = [part0 part1 part1 part2 part3 part4 part0 part1 part1 part2 part3 part5 part3 part6 part3];
v2 = [part0 part1 part1 part2 part3 part5 part3 part6 part3];
% Let's rock ^_^
s = v1;
s = s/max(s);
sound(s,fs);
% 画囧
syms x;
g=1/(x^2+2*x-3);
ezplot(g,-10,10);
% 2D 爱心
[x,y]=meshgrid(-10:0.01:10);
z=-(17*x.^2-16*y.*abs(x)+17.*y.^2);
[c,h]=contourf(z,100);set(h,'linestyle','none')
% 五星红旗
function varargout=saxplaxliu(varargin)
x=[0 0 40 40 0];
y=[0 13 13 0 0];
k=40/13;
fill(x,y,'r')
hold on
plot(x,y,'r')
axis([0 40 0 13])
seta=pi/6;
R=1.5;R2=0.55;x0=7;y0=9;seta=0;
fliu(R,R2,x0,y0,seta,k)
R=0.6;R2=0.23;x0=15;y0=11.6;seta=-pi/20;
fliu(R,R2,x0,y0,seta,k)
R=0.6;R2=0.23;x0=20;y0=10;seta=pi/20;
fliu(R,R2,x0,y0,seta,k)
R=0.6;R2=0.23;x0=19;y0=7;seta=pi/25;
fliu(R,R2,x0,y0,seta,k)
R=0.6;R2=0.23;x0=16;y0=5;seta=-pi/10;
fliu(R,R2,x0,y0,seta,k)
axis off 
function fliu(R,R2,x0,y0,seta,k)
x=zeros(1,10);
y=x;
for i=1:5
    x(2*(i-1)+1)=R*cos(pi/2+2*pi/5*(i-1)+seta);
    x(2*i)=R2*cos(pi/2+2*pi/5*(i-1)+pi/5+seta);
    y(2*(i-1)+1)=R*sin(pi/2+2*pi/5*(i-1)+seta);
    y(2*i)=R2*sin(pi/2+2*pi/5*(i-1)+pi/5+seta);
end
x(11)=x(1);
y(11)=y(1);
x=x*k+x0;
y=y+y0;
fill(x,y,[1 0.7 0])
plot(x,y,'y')
% 光盘
t = (0:.02:2)*pi;
r = 0:.02:1; %r = 0.3:.02:1; 
pcolor(cos(t)'*r,sin(t)'*r,t'*(r==r))
colormap(hsv(256)), shading interp, axis image off
%ezsurf('r*cos(t)','r*sin(t)','t',[0 1 0 2*pi])
%colormap(hsv(256)),shading interp,view(2), axis image off
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容