Using Matlab to solve the problem below
Given
X=[-2 -1 0 1 2]
Y=[1.5 3.2 4.5 3.4 2]
a). Plot a scatter plot of the data
b). Determine the coefficients of the polynomial ?0 + ?1? + ?2?2 which best fits the data
c). Plot this function on the same plot as in part ‘a’.
USE MATLAB CODE ONLY!
USE MATLAB CODE ONLY!
THANK YOU
MATLAB Code:
close all
clear
clc
% To get the coefficients of the quadratic function,
% y = a0 + a1*x + a2*x^2;
% We need to solve the system AX = y,
% where:
% A = [1 x x^2] and X = [a0 a1 a2]
% Data points
x = [-2 -1 0 1 2]';
y = [1.5 3.2 4.5 3.4 2]';
A = [ones(size(x)) x x.^2];
X = A\y; % Solve the system
fprintf('Quadratic Function, y = (%.4f) + (%.4f)*x + (%.4f)*x^2\n',
X)
% Plotting
xq = linspace(min(x), max(x), 100); % Bunch of new samples for a
smoother plot
yq = X(1) + X(2)*xq + X(3)*xq.^2;
plot(x, y, 'o', xq, yq)
xlabel('x'), ylabel('y')
title('Polynomial Curve Fitting')
legend('Data Points', 'Polynomial Fit', 'Location', 'south')
Output:
Quadratic Function, y = (4.1486) + (0.1200)*x + (-0.6143)*x^2
Plot:
Get Answers For Free
Most questions answered within 1 hours.