Created
April 26, 2018 12:46
-
-
Save KuanYuChang/70a93b00ede8fc5f982493f94ea371a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| clear; | |
| %% Data Set | |
| t = [-2; -1; 0; 1; 2]; | |
| y = [ 4; 3; 1; -1; -3]; | |
| %% Degree of approximation | |
| % n = 1; % linear | |
| % n = 2; % quadratic | |
| % n = 3; % cubic | |
| n = 4; % quartic | |
| %% Prepare A | |
| A = zeros(size(t, 1), n+1); | |
| for i = 1:1:n+1 | |
| A(:, i) = t .^ (n - i + 1); | |
| end | |
| %% Solve x & Compute Error | |
| x = ((A' * A) \ A') * y; | |
| e = sum((A * x - y) .^ 2); | |
| %% Compute exact values | |
| tapp = linspace(min(t)-1, max(t)+1); | |
| yapp = ones(size(tapp)) .* x(end); | |
| for i = 1:1:n | |
| yapp = yapp + (tapp .^ (n - i + 1)) .* x(i); | |
| end | |
| %% Plot | |
| hold on; | |
| grid on; | |
| plot(t, y, 'o'); | |
| plot(tapp, yapp); | |
| xlabel('t'); ylabel('y'); | |
| title(['Error ~= ', num2str(e)]); | |
| legend('origin data set', ['approx. n = ', num2str(n)]); | |
| hold off; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment