second order polynomial in matlab

To fit a second-order polynomial in Matlab, you can use the polyfit function.

Here is an example:

main.m
% Example data 
x = 1:10;
y = [ 3.2, 3.9, 5.6, 7.1, 9.5, 11.6, 13.5, 15.0, 16.4, 17.6 ];

% Fit second-order polynomial
p = polyfit(x, y, 2);

% Evaluate polynomial at x-values
y_fit = polyval(p, x);

% Plot original data and fitted curve
figure;
plot(x, y, 'o', x, y_fit, '-')
legend('Original Data', 'Fitted Curve')
xlabel('x')
ylabel('y')
342 chars
17 lines

The polyfit function takes the x and y data, as well as the degree of the polynomial you want to fit, as input arguments. In this case, we set the degree to 2.

The polyval function is used to evaluate the fitted polynomial at the x-values.

Finally, we use the plot function to plot the original data and the fitted curve.

related categories

gistlibby LogSnag