use 1st, 2nd and 3rd degree of polynomials to fit price1 vs index in matlab

To fit price1 vs index using different degrees of polynomials with MATLAB, you can use the polyfit and polyval functions.

Here's an example code using 1st, 2nd, and 3rd degree polynomials:

main.m
% Sample data
index = [1, 2, 3, 4, 5];
price1 = [10, 12, 14, 16, 18];

% Plot original data
plot(index, price1, 'o');

hold on;

% Fit first degree polynomial
p1 = polyfit(index, price1, 1);
f1 = polyval(p1, index);
plot(index, f1);

% Fit second degree polynomial
p2 = polyfit(index, price1, 2);
f2 = polyval(p2, index);
plot(index, f2);

% Fit third degree polynomial
p3 = polyfit(index, price1, 3);
f3 = polyval(p3, index);
plot(index, f3);

% Add plot labels and title
xlabel('Index')
ylabel('Price1')
title('Polynomial Regression')

% Add plot legend
legend('Data', '1st Degree Polynomial', '2nd Degree Polynomial', '3rd Degree Polynomial')
646 chars
32 lines

This code will plot the data points and the regression lines for 1st, 2nd, and 3rd degree polynomials.

Note: It's important to use caution when fitting higher degree polynomials as they can be prone to overfitting and may not generalize well to new data.

gistlibby LogSnag