0% found this document useful (0 votes)
5 views

CF

Uploaded by

omkulkarni412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CF

Uploaded by

omkulkarni412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

% Input Section - Taking sample data from the user

n = input('Enter the number of data points: ');

x = zeros(1, n);
y = zeros(1, n);

disp('Enter the sample data (x, y):');


for i = 1:n
x(i) = input(['Enter x' num2str(i) ': ']);
y(i) = input(['Enter y' num2str(i) ': ']);
end

% Degree of the polynomial


degree = 2;

% Creating the Vandermonde matrix


A = zeros(length(x), degree + 1);
for i = 1:length(x)
for j = 1:(degree + 1)
A(i, j) = x(i)^(j - 1);
end
end

% Solving the least squares problem


coefficients = (A' * A) \ (A' * y');

% Generating the fitted curve


x_fit = linspace(min(x), max(x), 100);
y_fit = zeros(size(x_fit));
for i = 1:(degree + 1)
y_fit = y_fit + coefficients(i) * x_fit.^(i - 1);
end

% Plotting the data and the fitted curve


plot(x, y, 'o', x_fit, y_fit, '-');
xlabel('x');
ylabel('y');
title('Curve Fitting using Least Squares Method');
legend('Data', 'Fitted Curve');
grid on;

You might also like