1 Approximation of functions by polynomials
Let the function f(x) be given by the following:
f(x) = 1/ 1 + x^2
Use polyfit to approximate f(x) by polynomials of degree k = 2, 4, and 6. Plot the approximating polynomials and f(x) on the same plot over an appropriate domain. Also, plot the approximation error for each case. Note that you also will need polyval to evaluate the approximating polynomial.
Submit your code and both plots. Make sure each of these plots is distinguishable and that a legend is included.
MATLAB Code:
close all
clear
clc
x = 0:0.1:10;
y = 1./(1 + x.^2);
p2 = polyfit(x, y, 2);
p4 = polyfit(x, y, 4);
p6 = polyfit(x, y, 6);
plot(x, y, x, polyval(p2, x), x, polyval(p4, x), x, polyval(p6,
x)), xlabel('x')
legend('f(x)', 'Degree 2 Polynomial', 'Degree 4 Polynomial',
'Degree 6 Polynomial')
xlabel('x'), title('Function Plots')
figure, plot(x, abs(y - polyval(p2, x)), x, abs(y - polyval(p4,
x)), x, abs(y - polyval(p6, x)))
legend('Degree 2 Polynomial', 'Degree 4 Polynomial', 'Degree 6
Polynomial')
xlabel('x'), title('Error Plots')
Plots:
Get Answers For Free
Most questions answered within 1 hours.