polynomials to fit price1 vs index in matlab

You can fit a polynomial curve to your data using the "polyfit" and "polyval" functions in Matlab. Here's an example code snippet that fits a 4th degree polynomial curve to the "price1" vs "index" data:

main.m
% Generate example data
index = [1 2 3 4 5];
price1 = [10 30 50 80 110];

% Fit a 4th degree polynomial curve
p = polyfit(index, price1, 4);

% Evaluate the polynomial at new x values
x = linspace(1, 5);
y = polyval(p, x);

% Plot the original data and fitted curve
plot(index, price1, 'o', x, y);
xlabel('Index');
ylabel('Price 1');
legend('Original data', 'Fitted curve');
375 chars
17 lines

In this example, we first generate some example data for "index" and "price1". We then use the "polyfit" function to fit a 4th degree polynomial curve to the data. The resulting polynomial coefficients are stored in the "p" vector. We then use the "polyval" function to evaluate the polynomial at a range of new "x" values, and store the resulting "y" values in the "y" vector. Finally, we plot both the original data and the fitted polynomial curve using the "plot" function.

Note that the degree of the polynomial you choose should depend on the complexity of your data and how well you want the polynomial to fit. You may need to experiment with different polynomial degrees to find the one that works best for your data.

gistlibby LogSnag