Given x = [0,0.05, 0.01, 0.15, 0.2, 0.25, … , 0.95, 1] and f(x) = [1, 1.0043, 1.0172, 1.0387, 1.0687, 1.1074, 1.1546, 1.2105, 1.2749, 1.3479, 1.4296, 1.5198, 1.6186, 1.7260, 1.8420, 1.9665, 2.0997, 2.2415, 2.3918, 2.5507, 2.7183], write a MATLAB script that approximates the coefficients of f(x).
MATLAB Code:
close all
clear
clc
x = 0:0.05:1;
f = [1, 1.0043, 1.0172, 1.0387, 1.0687, 1.1074, 1.1546, 1.2105,
1.2749, 1.3479, 1.4296, 1.5198, 1.6186, 1.7260, 1.8420, 1.9665,
2.0997, 2.2415, 2.3918, 2.5507, 2.7183];
p = polyfit(x,f,5); % Degree 5 polynomial fit
fprintf('Polynomial Fit of f(x):\n\tp(x) = (%.4f)*x^5 + (%.4f)*x^4
+ (%.4f)*x^3 + (%.4f)*x^2 + (%.4f)*x + (%.4f)\n', p)
% Plotting
plot(x,f,'o',x,polyval(p,x))
xlabel('x'), ylabel('f(x)'), title('Degree 5 Polynomial Fit')
legend('Data Points', 'Polynomial Fit', 'Location',
'northwest')
Output:
Polynomial Fit of f(x):
p(x) = (0.0089)*x^5 + (-0.0224)*x^4 + (0.0194)*x^3 +
(1.7116)*x^2 + (0.0007)*x + (1.0000)
Plot:
Get Answers For Free
Most questions answered within 1 hours.