Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. clear
  2. % Create the coordinates vector
  3. x = -15 : 0.1 : 15;
  4.  
  5. % f is the result vector of applying func to each cell in x
  6. f = func(x).';
  7.  
  8. % Add a random noise to vector f, and store the result in y
  9. % We'll use the same y vector for all sections in this assignment.
  10. y = f + 0.4*randn(size(f));
  11.  
  12.  
  13. % Genetare each column of matrix A.
  14. col4 = ones(size(x));
  15. col3 = x;
  16. col2 = x.^2;
  17. col1 = x.^3;
  18.  
  19. % A is the appropriate Vandermonde matrix for this LS problem
  20. A = ([col1; col2; col3; col4]).';
  21.  
  22. % Calculate A transpose
  23. A_t = A.';
  24.  
  25. % Calculate K matrix
  26. K = A_t*A;
  27.  
  28. % Calculate the inverse matrix of K
  29. K_inv = inv(K);
  30.  
  31. % Find the optimal solution for the LS problem
  32. % This vector is the coefficient vector for the approximation polynomial
  33. x_opt = KA_t*y;
  34.  
  35. % Calculate the polynomial from the coefficients vector x_opt, denoted P,
  36. % and apply it to the coordinates vector x, i.e, res_vector = P(x).
  37. res_vector = polyval(x_opt, x);
  38. % Show P(x), y
  39. plot(x,res_vector, 'g');
  40. hold on;
  41. plot(x,y, 'b');
  42. hold off;
  43. legend('P(x)', 'y');
  44.  
  45. % Calculate the remainder vector in respect to the distorted values, i.e,
  46. % vector y
  47. r1 = y - res_vector;
  48. % Calculate the remainder vector in respect to the original function values
  49. r2 = f - res_vector;
  50.  
  51. % Calculate the L2 norm
  52. n1 = norm(r1)^2;
  53. n2 = norm(r2)^2;
  54.  
  55.  
  56. % Function definitions
  57.  
  58. function ret = func(x)
  59. % This is function we want to approximate in this exercise.
  60. ret = sin(0.3*x) - cos(0.5*x);
  61. end
  62.  
  63. % Clear the workspace, except y vector
  64. clearvars -except y
  65. % Create the coordinates vector
  66. x = -15 : 0.1 : 15;
  67.  
  68. % f is the result vector of applying func to each cell in x
  69. f = func(x).';
  70.  
  71. % Create a result vector
  72. res_vector = zeros(size(x));
  73.  
  74. % This for loop calculates the approximation of the function for each
  75. % coordinate:
  76. window_size = 3;
  77. for i = 1:length(x)
  78. % Calculate the lower and higher index bounds
  79. L = max([i-window_size 1]);
  80. H = min([i+window_size length(x)]);
  81. % Calculate the height of matrix A
  82. n = H - L + 1;
  83. % Calculate each column of the Vandermonde matrix
  84. col4 = ones(1,n);
  85. % Reduce the x vector to the relevant indices
  86. x_r = x(L:H);
  87. col3 = x_r;
  88. col2 = x_r.^2;
  89. col1 = x_r.^3;
  90. % Create A and A transpose
  91. A_t = ([col1; col2; col3; col4]);
  92. A = A_t.';
  93. % Create K and K inverse
  94. K = A_t*A;
  95. % Reduce the y vector to the relevant indices
  96. y_r = y(L:H);
  97. % Calculate the optimal solution, which is the coefficient vector for
  98. % the approximation polynomial
  99. x_opt = KA_t*y_r;
  100. % Calculate the value of the polynomial in the coordinate x(i), and
  101. % store the result in res_vector(i)
  102. t = A(max([i-L 1]), :);
  103. res_vector(i) = t*x_opt;
  104. end
  105.  
  106. % Show P(x), y
  107. plot(x,res_vector, 'g');
  108. hold on;
  109. plot(x,y, 'b');
  110. hold off;
  111. legend('P(x)', 'y');
  112.  
  113. % Calculate the remainder vector in respect to the distorted values, i.e,
  114. % vector y
  115. r1 = y - res_vector;
  116. % Calculate the remainder vector in respect to the original function values
  117. r2 = f - res_vector;
  118.  
  119. % Calculate the L2 norm
  120. n1 = norm(r1)^2;
  121. n2 = norm(r2)^2;
  122.  
  123.  
  124. % Function definitions
  125.  
  126. function ret = func(x)
  127. % This is function we want to approximate in this exercise.
  128. ret = sin(0.3*x) - cos(0.5*x);
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement