Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear
- % Create the coordinates vector
- x = -15 : 0.1 : 15;
- % f is the result vector of applying func to each cell in x
- f = func(x).';
- % Add a random noise to vector f, and store the result in y
- % We'll use the same y vector for all sections in this assignment.
- y = f + 0.4*randn(size(f));
- % Genetare each column of matrix A.
- col4 = ones(size(x));
- col3 = x;
- col2 = x.^2;
- col1 = x.^3;
- % A is the appropriate Vandermonde matrix for this LS problem
- A = ([col1; col2; col3; col4]).';
- % Calculate A transpose
- A_t = A.';
- % Calculate K matrix
- K = A_t*A;
- % Calculate the inverse matrix of K
- K_inv = inv(K);
- % Find the optimal solution for the LS problem
- % This vector is the coefficient vector for the approximation polynomial
- x_opt = KA_t*y;
- % Calculate the polynomial from the coefficients vector x_opt, denoted P,
- % and apply it to the coordinates vector x, i.e, res_vector = P(x).
- res_vector = polyval(x_opt, x);
- % Show P(x), y
- plot(x,res_vector, 'g');
- hold on;
- plot(x,y, 'b');
- hold off;
- legend('P(x)', 'y');
- % Calculate the remainder vector in respect to the distorted values, i.e,
- % vector y
- r1 = y - res_vector;
- % Calculate the remainder vector in respect to the original function values
- r2 = f - res_vector;
- % Calculate the L2 norm
- n1 = norm(r1)^2;
- n2 = norm(r2)^2;
- % Function definitions
- function ret = func(x)
- % This is function we want to approximate in this exercise.
- ret = sin(0.3*x) - cos(0.5*x);
- end
- % Clear the workspace, except y vector
- clearvars -except y
- % Create the coordinates vector
- x = -15 : 0.1 : 15;
- % f is the result vector of applying func to each cell in x
- f = func(x).';
- % Create a result vector
- res_vector = zeros(size(x));
- % This for loop calculates the approximation of the function for each
- % coordinate:
- window_size = 3;
- for i = 1:length(x)
- % Calculate the lower and higher index bounds
- L = max([i-window_size 1]);
- H = min([i+window_size length(x)]);
- % Calculate the height of matrix A
- n = H - L + 1;
- % Calculate each column of the Vandermonde matrix
- col4 = ones(1,n);
- % Reduce the x vector to the relevant indices
- x_r = x(L:H);
- col3 = x_r;
- col2 = x_r.^2;
- col1 = x_r.^3;
- % Create A and A transpose
- A_t = ([col1; col2; col3; col4]);
- A = A_t.';
- % Create K and K inverse
- K = A_t*A;
- % Reduce the y vector to the relevant indices
- y_r = y(L:H);
- % Calculate the optimal solution, which is the coefficient vector for
- % the approximation polynomial
- x_opt = KA_t*y_r;
- % Calculate the value of the polynomial in the coordinate x(i), and
- % store the result in res_vector(i)
- t = A(max([i-L 1]), :);
- res_vector(i) = t*x_opt;
- end
- % Show P(x), y
- plot(x,res_vector, 'g');
- hold on;
- plot(x,y, 'b');
- hold off;
- legend('P(x)', 'y');
- % Calculate the remainder vector in respect to the distorted values, i.e,
- % vector y
- r1 = y - res_vector;
- % Calculate the remainder vector in respect to the original function values
- r2 = f - res_vector;
- % Calculate the L2 norm
- n1 = norm(r1)^2;
- n2 = norm(r2)^2;
- % Function definitions
- function ret = func(x)
- % This is function we want to approximate in this exercise.
- ret = sin(0.3*x) - cos(0.5*x);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement