当前位置:   article > 正文

MATLAB迭代

MATLAB迭代

目录

黄金分割比

习题

符号运算

固定点

WHY

hello world

Goldrect


黄金分割比


format

 

 for循环

  1. %% For loop
  2. x = 42
  3. for k = 1:12
  4. x = sqrt(1+x);
  5. disp(x)
  6. end

while循环

  1. %% While loop
  2. x = 42;
  3. k = 1;
  4. while abs(x-sqrt(1+x)) > 5e-5
  5. x = sqrt(1+x);
  6. k = k+1;
  7. end
  8. k

绘图语句

  1. %% Plot
  2. x = -pi: pi/256: pi;
  3. y = tan(sin(x)) - sin(tan(x));
  4. z = 1 + tan(1);
  5. plot(x,y,'-', pi/2,z,'ro') %pi/2,z为点坐标
  6. xlabel('x')
  7. ylabel('y')
  8. title('tan(sin(x)) - sin(tan(x))')

黄金螺旋线

  1. function golden_spiral
  2. % GOLDEN_SPIRAL Explosion of golden rectangles.
  3. % GOLDEN_SPIRAL Constructs a continuously expanding sequence
  4. % of golden rectangles and inscribed quarter circles.
  5. % Copyright 2020 The MathWorks, Inc.
  6. %
  7. % Text used in the show_quote function:
  8. % Jones, Robert Bishop. 揂ristotle METAPHYSICA Book 13 Part 3.� Aristotle METAPHYSICA Book 13 Part 3
  9. % Mathematics, Harmonics, Optics, Good and Beauty, 25 Nov. 1996,
  10. % www.rbjones.com/rbjpub/philos/classics/aristotl/m11303c.htm.
  11. % Initialize_variables
  12. klose = [];
  13. % Golden ratio
  14. phi = (1+sqrt(5))/2;
  15. % Control speed of zooming in控制缩放速度
  16. n = 48;
  17. f = phi^(1/n);
  18. % Scaling
  19. a = 1;
  20. s = phi;
  21. t = 1/(phi+1);
  22. % Centers
  23. x = 0;
  24. y = 0;
  25. % A square
  26. us = [-1 1 1 -1 -1];
  27. vs = [-1 -1 1 1 -1];
  28. % Four quarter circles四分之一圆
  29. theta = 0:pi/20:pi/2;
  30. u1 = 2*cos(theta) - 1;
  31. v1 = 2*sin(theta) - 1;
  32. u2 = 2*cos(theta+pi/2) + 1;
  33. v2 = 2*sin(theta+pi/2) - 1;
  34. u3 = 2*cos(theta+pi) + 1;
  35. v3 = 2*sin(theta+pi) + 1;
  36. u4 = 2*cos(theta-pi/2) - 1;
  37. v4 = 2*sin(theta-pi/2) + 1;
  38. initialize_graphics
  39. % Loop
  40. k = 0;
  41. while get(klose,'Value') == 0
  42. if k <= 285
  43. if mod(k,n) == 0
  44. scaled_power
  45. switch mod(k/n,4)
  46. case 0, up
  47. case 1, left
  48. case 2, down
  49. case 3, right
  50. end
  51. end
  52. zoom_in
  53. k = k+1;
  54. else
  55. break
  56. end
  57. end
  58. pause(1)
  59. clf
  60. show_quote
  61. % ------------------------------------
  62. function scaled_power
  63. a = s;
  64. s = phi*s;
  65. t = phi*t;
  66. end % scaled_power
  67. % ------------------------------------
  68. function zoom_in
  69. axis(f*axis)
  70. drawnow
  71. end % zoom_in
  72. % ------------------------------------
  73. function right
  74. x = x + s;
  75. y = y + t;
  76. line(x+a*us,y+a*vs,'Color','black')
  77. line(x+a*u4,y+a*v4)
  78. end % right
  79. % ------------------------------------
  80. function up
  81. y = y + s;
  82. x = x - t;
  83. line(x+a*us,y+a*vs,'Color','black')
  84. line(x+a*u1,y+a*v1)
  85. end % up
  86. % ------------------------------------
  87. function left
  88. x = x - s;
  89. y = y - t;
  90. line(x+a*us,y+a*vs,'Color','black')
  91. line(x+a*u2,y+a*v2)
  92. end % left
  93. % ------------------------------------
  94. function down
  95. y = y - s;
  96. x = x + t;
  97. line(x+a*us,y+a*vs,'Color','black')
  98. line(x+a*u3,y+a*v3)
  99. end % down
  100. % ------------------------------------
  101. function initialize_graphics
  102. clf reset
  103. set(gcf,'Color','white','Menubar','none','Numbertitle','off', ...
  104. 'Name','The Golden Spiral')
  105. shg
  106. axes('Position',[0 0 1 1])
  107. axis(3.5*[-1 1 -1 1])
  108. axis square
  109. axis off
  110. line(us,vs,'Color','black')
  111. line(u4,v4)
  112. klose = uicontrol('Units','normal','Position',[.04 .04 .12 .04], ...
  113. 'Style','togglebutton','String','close','Visible','on');
  114. drawnow
  115. end % initialize graphics
  116. function show_quote
  117. large_text = cell(4,1);
  118. large_text{1} = transform('Gur puvrs sbezf bs ornhgl ner');
  119. large_text{2} = transform('beqre naq flzzrgel naq qrsvavgrarff,');
  120. large_text{3} = transform('juvpu gur zngurzngvpny fpvraprf');
  121. large_text{4} = transform('qrzbafgengr va n fcrpvny qrterr.');
  122. medium_text = transform('- Nevfgbgyr');
  123. text(0.5, 0.6, large_text, 'HorizontalAlignment', 'center', 'Color', [0 0.4470 0.7410], 'FontWeight', 'bold', 'FontSize', 14);
  124. text(0.5, 0.4, medium_text, 'HorizontalAlignment', 'center', 'Color', [0 0.4470 0.7410], 'FontWeight', 'bold', 'FontSize', 12);
  125. axis off
  126. drawnow
  127. end % show quote
  128. function s2 = transform(s1)
  129. m25=1:256;i17=97;m25(i17:i17+25)=[i17+13:i17+25 i17:i17+12];
  130. i17=65;m25(i17:i17+25)=[i17+13:i17+25 i17:i17+12];
  131. s2=char(m25(s1));
  132. end % transform
  133. end % golden_spiral


习题


符号运算


  1. x=sym('x'),length(char(x)) %字符数组的长度
  2. for k = 1:10
  3. x=sqrt(1+x),length(char(x))
  4. end


固定点


  1. function fixedpoint(f,xmin,xmax,xstart);
  2. % FIXEDPOINT Illustrate fixed point iteration.
  3. % fixedpoint(f,xmin,xmax,xstart) tries to solve x = f(x).
  4. % Examples
  5. % f = @(x) sqrt(1+x); fixedpoint(f, -1, 4, 0) (Default)
  6. % f = @(x) 1./x+1; fixedpoint(f, .5, 2.5, 1)
  7. % f = @(x) cos(x); fixedpoint(f, -pi/4, pi/2, 0)
  8. % a = sqrt(2); f = @(x) a.^x; fixedpoint(f, 1, 5, 3)
  9. % a = 3+randn, f = @(x) a*x.*(1-x); ...
  10. % fixedpoint(f, 0, 1, .5), title([num2str(a) '*x*(1-x)'])
  11. % Default example
  12. if nargin == 0
  13. f = @(x) sqrt(1 + x);
  14. xmin = -1;
  15. xmax = 4;
  16. xstart = 0;
  17. end
  18. % Iteration
  19. x = xstart;
  20. y = f(x);
  21. n = 1;
  22. while (x(n) ~= y(n)) & (n < 50) & (max(abs(y)) < 100)
  23. n = n+1;
  24. x(n) = y(n-1);
  25. y(n) = f(x(n));
  26. end
  27. % Plot
  28. t = sort([xmin:(xmax-xmin)/256:xmax x]);
  29. x = [x; x];
  30. y = [x(1) y(1:n-1); y];
  31. plot(t,t,'-',t,f(t),'-',x(:),y(:),'k-',x(end),y(end),'ro');
  32. axis tight square
  33. set(zoom(gcf),'ActionPostCallback','zoomer')


WHY


  1. why,help why
  2. for k= 1:40,why,end
  3. type why
  4. edit why

Matlab中why函数(一个无用但有趣的函数)_matlab好玩的函数_gxgdcyy的博客-CSDN博客


hello world


  1. help hello_world
  2. format loose
  3. %% Array operations
  4. h = 'hello world'
  5. h'
  6. fliplr(h)
  7. flipud(h')
  8. diag(h)
  9. sort(h)
  10. %% Characters
  11. x = real(h)
  12. char(x)
  13. char(max(h))
  14. char(x+3)
  15. %% Plots
  16. H = upper(h)
  17. figure(1)
  18. plot(x,'*-')
  19. title(H)
  20. figure(2)
  21. bar(x)
  22. set(gca,'xticklabel',{H(:)})
  23. set(2,'pos',get(1,'pos')+[30 -30 0 0])
  24. figure(3)
  25. p = pie(x);
  26. for k = 1:11
  27. set(p(2*k),'string',H(k))
  28. end
  29. set(3,'pos',get(1,'pos')+[60 -60 0 0])


Goldrect


  1. % GOLDRECT
  2. phi=(1+sqrt(5))/2;
  3. x=[0 phi phi 0 0]; %行数组x和y各包含5个元素;
  4. y=[0 0 1 1 0]; %分别是(0,0)(phi,0),(phi,1),(0,1),(0,0);
  5. u=[1 1];
  6. v=[0 1]; %矩形分割线;
  7. plot(x,y,'b',u,v,'b--')
  8. text(phi/2,1.05,'\phi') %在(phi/2,1.05)处标记;
  9. text((1+phi)/2,-.05,'\phi-1')
  10. text(-.05,.5,'1')
  11. text(.5,-.05,'1')
  12. axis equal %使x和y方向的刻度长短相等;
  13. axis off %使坐标轴消隐;
  14. set(gcf,'color','white') %gcf是指get current figure-将背景色设为白色;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/176864
推荐阅读
相关标签
  

闽ICP备14008679号