ue1_1.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. %% 1.1.1
  2. A = [1,2,4;2,2,1;3,2,0]
  3. y = [5;4;2]
  4. tic
  5. x_1 = inv(A)*y
  6. toc
  7. tic
  8. x_2 = mldivide(A,y)
  9. toc
  10. norm(A*x_1-y)
  11. norm(A*x_2-y)
  12. %% 1.1.2
  13. A = 10;
  14. n = 1:100;
  15. x = (0:10)
  16. f = zeros(size(x))
  17. hold on
  18. for i = x
  19. f = A*sum((4./(pi*2*n).*sin((2*n-1)*i)))
  20. plot(i,f, '*')
  21. pause(1)
  22. end
  23. hold off
  24. %% 1.1.2 alternativ
  25. A = 10;
  26. n = 1:100;
  27. x = 0:10;
  28. hold on
  29. for x_c = x
  30. f = 0;
  31. for i = n
  32. f = f + A*sum((4/(pi*2*i)*sin((2*i-1)*x_c)));
  33. end
  34. plot(x(x_c+1), f, '*')
  35. end
  36. hold off
  37. %% 1.1.3
  38. t_konsts = 0:10;
  39. [x,y] = meshgrid(0:1:10)
  40. z_0 = sin((x*pi)/10).*sin((y*pi)/10)
  41. for i = t_konsts
  42. hold on;
  43. z = z_0.*abs(sin(i));
  44. surf(x, y, z);
  45. pause(3);
  46. end
  47. hold off;
  48. %% 1.2.1
  49. clear all
  50. close all
  51. T0 = 0.05;
  52. t(1) = 0;
  53. m = 1;
  54. c = 1;
  55. k = 2;
  56. T = 1:5;
  57. for i = 1:5
  58. T(i) = T0*i;
  59. end
  60. for i = 1:5
  61. Ta = T(i);
  62. t = 0:Ta:20;
  63. x = zeros(2, size(t, 2));
  64. for n = 1:size(t,2)-1
  65. x(:,n+1) = x(:,n) + Ta*([0 1; -k/m -c/m]*[x(1,n);x(2,n)] + [0;1/m]);
  66. end
  67. hold on
  68. subplot(2,1,1)
  69. plot(t,x(1,:))
  70. hold on
  71. subplot(2,1,2)
  72. plot(t,x(2,:))
  73. end
  74. legend(num2str(T'))
  75. l1 = (-c+sqrt(c^2-4*m*k))/(2*m);
  76. l2 = (-c-sqrt(c^2-4*m*k))/(2*m);
  77. t = 0:T0:20;
  78. x = zeros(2, size(t, 2));
  79. x(1, :) = (l2*(-1 + exp(l1*t)) - l1*(-1 + exp(l2*t)))/(l1*l2*(-l2 + l1));
  80. x(2, :) = 1/(l1-l2) * (exp(l1*t)-exp(l2*t));
  81. hold on
  82. subplot(2,1,1)
  83. plot(t,x(1, :))
  84. hold on
  85. subplot(2,1,2)
  86. plot(t,x(2, :))
  87. %% 3.
  88. m = 1; % kg
  89. c = 1; % Ns/m
  90. k = 2; % N/m
  91. Ta = .1; % s
  92. % state space representation
  93. A = [0 1; -k/m -c/m];
  94. B = [0;1/m];
  95. C = [1,0];
  96. D = 0;
  97. % construct system
  98. sys = ss(A,B,C,D);
  99. % discretize system
  100. discrete_sys = c2d(sys,Ta,'zoh');
  101. % plot step response
  102. subplot(2,1,1)
  103. step(discrete_sys)