目录
黄金分割比
习题
符号运算
固定点
WHY
hello world
Goldrect
黄金分割比
format
for循环
%% For loop x = 42 for k = 1:12 x = sqrt(1+x); disp(x) end
while循环
%% While loop x = 42; k = 1; while abs(x-sqrt(1+x)) > 5e-5 x = sqrt(1+x); k = k+1; end k
绘图语句
%% Plot x = -pi: pi/256: pi; y = tan(sin(x)) - sin(tan(x)); z = 1 + tan(1); plot(x,y,'-', pi/2,z,'ro') %pi/2,z为点坐标 xlabel('x') ylabel('y') title('tan(sin(x)) - sin(tan(x))')
黄金螺旋线
function golden_spiral % GOLDEN_SPIRAL Explosion of golden rectangles. % GOLDEN_SPIRAL Constructs a continuously expanding sequence % of golden rectangles and inscribed quarter circles. % Copyright 2020 The MathWorks, Inc. % % Text used in the show_quote function: % Jones, Robert Bishop. 揂ristotle METAPHYSICA Book 13 Part 3.� Aristotle METAPHYSICA Book 13 Part 3 % Mathematics, Harmonics, Optics, Good and Beauty, 25 Nov. 1996, % www.rbjones.com/rbjpub/philos/classics/aristotl/m11303c.htm. % Initialize_variables klose = []; % Golden ratio phi = (1+sqrt(5))/2; % Control speed of zooming in控制缩放速度 n = 48; f = phi^(1/n); % Scaling a = 1; s = phi; t = 1/(phi+1); % Centers x = 0; y = 0; % A square us = [-1 1 1 -1 -1]; vs = [-1 -1 1 1 -1]; % Four quarter circles四分之一圆 theta = 0:pi/20:pi/2; u1 = 2*cos(theta) - 1; v1 = 2*sin(theta) - 1; u2 = 2*cos(theta+pi/2) + 1; v2 = 2*sin(theta+pi/2) - 1; u3 = 2*cos(theta+pi) + 1; v3 = 2*sin(theta+pi) + 1; u4 = 2*cos(theta-pi/2) - 1; v4 = 2*sin(theta-pi/2) + 1; initialize_graphics % Loop k = 0; while get(klose,'Value') == 0 if k <= 285 if mod(k,n) == 0 scaled_power switch mod(k/n,4) case 0, up case 1, left case 2, down case 3, right end end zoom_in k = k+1; else break end end pause(1) clf show_quote % ------------------------------------ function scaled_power a = s; s = phi*s; t = phi*t; end % scaled_power % ------------------------------------ function zoom_in axis(f*axis) drawnow end % zoom_in % ------------------------------------ function right x = x + s; y = y + t; line(x+a*us,y+a*vs,'Color','black') line(x+a*u4,y+a*v4) end % right % ------------------------------------ function up y = y + s; x = x - t; line(x+a*us,y+a*vs,'Color','black') line(x+a*u1,y+a*v1) end % up % ------------------------------------ function left x = x - s; y = y - t; line(x+a*us,y+a*vs,'Color','black') line(x+a*u2,y+a*v2) end % left % ------------------------------------ function down y = y - s; x = x + t; line(x+a*us,y+a*vs,'Color','black') line(x+a*u3,y+a*v3) end % down % ------------------------------------ function initialize_graphics clf reset set(gcf,'Color','white','Menubar','none','Numbertitle','off', ... 'Name','The Golden Spiral') shg axes('Position',[0 0 1 1]) axis(3.5*[-1 1 -1 1]) axis square axis off line(us,vs,'Color','black') line(u4,v4) klose = uicontrol('Units','normal','Position',[.04 .04 .12 .04], ... 'Style','togglebutton','String','close','Visible','on'); drawnow end % initialize graphics function show_quote large_text = cell(4,1); large_text{1} = transform('Gur puvrs sbezf bs ornhgl ner'); large_text{2} = transform('beqre naq flzzrgel naq qrsvavgrarff,'); large_text{3} = transform('juvpu gur zngurzngvpny fpvraprf'); large_text{4} = transform('qrzbafgengr va n fcrpvny qrterr.'); medium_text = transform('- Nevfgbgyr'); text(0.5, 0.6, large_text, 'HorizontalAlignment', 'center', 'Color', [0 0.4470 0.7410], 'FontWeight', 'bold', 'FontSize', 14); text(0.5, 0.4, medium_text, 'HorizontalAlignment', 'center', 'Color', [0 0.4470 0.7410], 'FontWeight', 'bold', 'FontSize', 12); axis off drawnow end % show quote function s2 = transform(s1) m25=1:256;i17=97;m25(i17:i17+25)=[i17+13:i17+25 i17:i17+12]; i17=65;m25(i17:i17+25)=[i17+13:i17+25 i17:i17+12]; s2=char(m25(s1)); end % transform end % golden_spiral
习题
符号运算
x=sym('x'),length(char(x)) %字符数组的长度 for k = 1:10 x=sqrt(1+x),length(char(x)) end
固定点
function fixedpoint(f,xmin,xmax,xstart); % FIXEDPOINT Illustrate fixed point iteration. % fixedpoint(f,xmin,xmax,xstart) tries to solve x = f(x). % Examples % f = @(x) sqrt(1+x); fixedpoint(f, -1, 4, 0) (Default) % f = @(x) 1./x+1; fixedpoint(f, .5, 2.5, 1) % f = @(x) cos(x); fixedpoint(f, -pi/4, pi/2, 0) % a = sqrt(2); f = @(x) a.^x; fixedpoint(f, 1, 5, 3) % a = 3+randn, f = @(x) a*x.*(1-x); ... % fixedpoint(f, 0, 1, .5), title([num2str(a) '*x*(1-x)']) % Default example if nargin == 0 f = @(x) sqrt(1 + x); xmin = -1; xmax = 4; xstart = 0; end % Iteration x = xstart; y = f(x); n = 1; while (x(n) ~= y(n)) & (n < 50) & (max(abs(y)) < 100) n = n+1; x(n) = y(n-1); y(n) = f(x(n)); end % Plot t = sort([xmin:(xmax-xmin)/256:xmax x]); x = [x; x]; y = [x(1) y(1:n-1); y]; plot(t,t,'-',t,f(t),'-',x(:),y(:),'k-',x(end),y(end),'ro'); axis tight square set(zoom(gcf),'ActionPostCallback','zoomer')
WHY
why,help why
for k= 1:40,why,end
type why
edit why
Matlab中why函数(一个无用但有趣的函数)_matlab好玩的函数_gxgdcyy的博客-CSDN博客
hello world
help hello_world
format loose
%% Array operations
h = 'hello world'
h'
fliplr(h)
flipud(h')
diag(h)
sort(h)
%% Characters
x = real(h)
char(x)
char(max(h))
char(x+3)
%% Plots
H = upper(h)
figure(1)
plot(x,'*-')
title(H)
figure(2)
bar(x)
set(gca,'xticklabel',{H(:)})
set(2,'pos',get(1,'pos')+[30 -30 0 0])
figure(3)
p = pie(x);
for k = 1:11
set(p(2*k),'string',H(k))
end
set(3,'pos',get(1,'pos')+[60 -60 0 0])
Goldrect
% GOLDRECT
phi=(1+sqrt(5))/2;
x=[0 phi phi 0 0]; %行数组x和y各包含5个元素;
y=[0 0 1 1 0]; %分别是(0,0)(phi,0),(phi,1),(0,1),(0,0);
u=[1 1];
v=[0 1]; %矩形分割线;
plot(x,y,'b',u,v,'b--')
text(phi/2,1.05,'\phi') %在(phi/2,1.05)处标记;
text((1+phi)/2,-.05,'\phi-1')
text(-.05,.5,'1')
text(.5,-.05,'1')
axis equal %使x和y方向的刻度长短相等;
axis off %使坐标轴消隐;
set(gcf,'color','white') %gcf是指get current figure-将背景色设为白色;