Vous êtes sur la page 1sur 7

1.

% Polgono regular de n lados


2. function dibujar_poligono
3. n = input('Cuntos lados tiene el polgono? ');
4. angulo = (n-2)*pi/n;
5. alfa = 2*pi/n;
6. beta = (2*pi-angulo)/2;
7. coseno = cos(alfa);
8. x = zeros(1,n);
9. y = zeros(1,n);
10. x(1) = (coseno+sqrt(coseno^2+2))/2;
11. y(1) = 0;
12. for i=2:n
13. x(i) = cos(beta) + x(i-1);
14. y(i) = sin(beta) + y(i-1);
15. beta = beta + pi - angulo;
16. end
17. for i=1:n
18. if i==n
19. line([x(i),x(1)],[y(i),y(1)]);
20. else
21. line([x(i),x(i+1)],[y(i),y(i+1)]);
22. end
23. end
24. end

1. % Circunferencia como polgono de 200 lados


2. function dibujar_circunferencia
3. n=200; % Nmero de lados
4. r=1; % Radio
5. alfa=2*pi/n;
6. termino=alfa;
7. for i=1:n
8. x(i)=r*cos(alfa);
9. y(i)=r*sin(alfa);
10. alfa=alfa+termino;
11. end
12. for i=1:n
13. if i==n
14. line([x(i),x(1)],[y(i),y(1)]);
15. else
16. line([x(i),x(i+1)],[y(i),y(i+1)]);
17. end
18. end
19. end

1. % Elipse como polgono de 200 lados


2. function dibujar_elipse
3. n=200; % Nmero de lados
4. u=1; % Semieje en OX
5. v=input('Introduce el valor del semieje en OY: '); % Semieje en OY
6. alfa=2*pi/n;
7. termino=alfa;
8. for i=1:n
9. x(i)=u*cos(alfa);
10. y(i)=v*sin(alfa);
11. alfa=alfa+termino;
12. end
13. for i=1:n
14. if i==n
15. line([x(i),x(1)],[y(i),y(1)]);
16. else
17. line([x(i),x(i+1)],[y(i),y(i+1)]);
18. end
19. end
20. end

1. function game_life
2. n=10; % tamao del mundo
3. t=10; % vida del mundo
4. world=condiciones_ini(n);
5. fprintf('Origen del mundo:\n');
6. disp(world);
7. for i=1:t
8. world=actualizar_mundo(world);
9. fprintf('Mundo en t=%d\n',i);
10. disp(world);
11. end
12. end
13.
14. function enviroment=condiciones_ini(n)
15. enviroment=randi(2,n)-1;
16. end
17.
18. function world = actualizar_mundo(world)
19. [m,n] = size(world);
20. for i=1:m
21. for j=1:n
22. if n_adyacentes(world,i,j) == 0
23. world(i,j)=0;
24. end
25. if n_adyacentes(world,i,j) == 3
26. world(i,j)=1;
27. end
28. if n_adyacentes(world,i,j) > 3
29. world(i,j)=0;
30. end
31. end
32. end
33. end
34.
35. function cont = n_adyacentes(world,i,j)
36. [m,n] = size(world);
37. cont=0;
38. if i>1 && j>1 && world(i-1,j-1) == 1
39. cont = cont+1;
40. end
41. if i>1 && world(i-1,j) == 1
42. cont = cont+1;
43. end
44. if i>1 && j<n && world(i-1,j+1) == 1
45. cont = cont+1;
46. end
47. if j>1 && world(i,j-1) == 1
48. cont = cont+1;
49. end
50. if j<n && world(i,j+1) == 1
51. cont = cont+1;
52. end
53. if i<m && j>1 && world(i+1,j-1) == 1
54. cont = cont+1;
55. end
56. if i<m && world(i+1,j) == 1
57. cont = cont+1;
58. end
59. if i<m && j<n && world(i+1,j+1) == 1
60. cont = cont+1;
61. end
62. end

1. function game_life_v2
2. n=10; % tamao del mundo
3. persistent world
4. persistent cont
5. if isempty(cont)
6. cont=0;
7. else
8. cont=cont+1;
9. end
10. if isempty(world)
11. world=condiciones_ini(n);
12. fprintf('Origen del mundo:\n');
13. contourf(world);
14. colormap(hot(8));
15. else
16. world=actualizar_mundo(world);
17. fprintf('Mundo en t=%d\n',cont);
18. contourf(world);
19. colormap(hot(8));
20. end
21. end
22.
23. function enviroment=condiciones_ini(n)
24. enviroment=randi(2,n)-1;
25. end
26.
27. function world = actualizar_mundo(world)
28. [m,n] = size(world);
29. for i=1:m
30. for j=1:n
31. if n_adyacentes(world,i,j) == 0
32. world(i,j)=0;
33. end
34. if n_adyacentes(world,i,j) == 3
35. world(i,j)=1;
36. end
37. if n_adyacentes(world,i,j) > 3
38. world(i,j)=0;
39. end
40. end
41. end
42. end
43.
44. function cont = n_adyacentes(world,i,j)
45. [m,n] = size(world);
46. cont=0;
47. if i>1 && j>1 && world(i-1,j-1) == 1
48. cont = cont+1;
49. end
50. if i>1 && world(i-1,j) == 1
51. cont = cont+1;
52. end
53. if i>1 && j<n && world(i-1,j+1) == 1
54. cont = cont+1;
55. end
56. if j>1 && world(i,j-1) == 1
57. cont = cont+1;
58. end
59. if j<n && world(i,j+1) == 1
60. cont = cont+1;
61. end
62. if i<m && j>1 && world(i+1,j-1) == 1
63. cont = cont+1;
64. end
65. if i<m && world(i+1,j) == 1
66. cont = cont+1;
67. end
68. if i<m && j<n && world(i+1,j+1) == 1
69. cont = cont+1;
70. end
71. end
1. % Codificar a nmeros romanos
2. function num_romanos
3. ok = 0;
4. while ok == 0
5. num=input('Escribe un nmero (mx. 3 cifras): ');
6. if num < 1000 && num > 0
7. ok = 1;
8. end
9. end
10. v=separar(num);
11. for i=3:-1:1
12. if v(i) ~= 0
13. texto=conversor(v(i),i);
14. fprintf('%s',texto);
15. end
16. end
17. fprintf('\n');
18. end
19.
20.
21. function v = separar(num)
22. if num < 10
23. l = 1;
24. elseif num < 100
25. l = 2;
26. else
27. l = 3;
28. end
29. v=zeros(1,3);
30. for i=1:l
31. v(i) = mod(num,10);
32. num=fix(num/10);
33. end
34. end
35.
36. function texto = conversor(num,orden)
37. texto = '';
38. if orden == 1
39. if num == 1
40. texto = 'I';
41. end
42. if num == 2
43. texto = 'II';
44. end
45. if num == 3
46. texto = 'III';
47. end
48. if num == 4
49. texto = 'IV';
50. end
51. if num == 5
52. texto = 'V';
53. end
54. if num == 6
55. texto = 'VI';
56. end
57. if num == 7
58. texto = 'VII';
59. end
60. if num == 8
61. texto = 'VIII';
62. end
63. if num == 9
64. texto = 'IX';
65. end
66. end
67. if orden == 2
68. if num == 1
69. texto = 'X';
70. end
71. if num == 2
72. texto = 'XX';
73. end
74. if num == 3
75. texto = 'XXX';
76. end
77. if num == 4
78. texto = 'XL';
79. end
80. if num == 5
81. texto = 'L';
82. end
83. if num == 6
84. texto = 'LX';
85. end
86. if num == 7
87. texto = 'LXX';
88. end
89. if num == 8
90. texto = 'LXXX';
91. end
92. if num == 9
93. texto = 'XC';
94. end
95. end
96. if orden == 3
97. if num == 1
98. texto = 'C';
99. end
100. if num == 2
101. texto = 'CC';
102. end
103. if num == 3
104. texto = 'CCC';
105. end
106. if num == 4
107. texto = 'CD';
108. end
109. if num == 5
110. texto = 'D';
111. end
112. if num == 6
113. texto = 'DC';
114. end
115. if num == 7
116. texto = 'DCC';
117. end
118. if num == 8
119. texto = 'DCCC';
120. end
121. if num == 9
122. texto = 'CM';
123. end
124. end
125. end

Vous aimerez peut-être aussi