Efficiently Use polyfit() on Arrays for Accurate Curve Fitting | MATLAB Guide

  • MATLAB
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Arrays
In summary, the polyfit() function in MATLAB is used for fitting a polynomial curve to a set of data points. It requires the x and y values of the data points, as well as the desired degree of the polynomial. The function returns the coefficients of the polynomial curve. The syntax for using polyfit() is "coeffs = polyfit(x, y, n)", where x is the vector of x values, y is the vector of y values, and n is the desired degree. To ensure accurate curve fitting, an appropriate degree should be chosen and the data points should be evenly spaced. Polyfit() cannot be used for non-linear curve fitting, but other functions such as lsqcurvefit() or fminsearch() can
  • #1
Saladsamurai
3,020
7
I have 2 arrays:

Code:
vxb = 
    0.0001    0.0001    0.0001    0.0002    0.0002
    0.0003    0.0003    0.0003    0.0003    0.0003
    0.0003    0.0004    0.0005    0.0005    0.0005
    0.0005    0.0006    0.0006    0.0006    0.0006
    0.0006    0.0006    0.0007    0.0007    0.0007
.
.
.
and
Code:
vtime = 
    0.2570    0.4500    0.6420    0.8350    1.0280
    1.2210    1.4130    1.6060    1.7990    1.9920
    2.1840    2.3770    2.5700    2.7630    2.9550
    3.1480    3.3410    3.5340    3.7260    3.9190
    4.1120    4.3050    4.4970    4.6900    4.8830

.
.
.

I would like to use polyfit to return an array p(,i,j) in which each row of p is the polyfit of
p(i,:) = polyfit(vxb(i,:), vtime(i,:), 4).

I know that I can do it with a loop, but I figured MATLAB is smart and there is a syntax I could use to do this.
 
Physics news on Phys.org

FAQ: Efficiently Use polyfit() on Arrays for Accurate Curve Fitting | MATLAB Guide

How do I use the polyfit() function in MATLAB for curve fitting?

The polyfit() function in MATLAB is used for fitting a polynomial curve to a set of data points. To use it, you need to provide the x and y values of your data points, as well as the desired degree of the polynomial. The function will then return the coefficients of the polynomial curve that best fits your data.

What is the syntax for using polyfit() in MATLAB?

The syntax for using the polyfit() function in MATLAB is as follows:
coeffs = polyfit(x, y, n)
where x is the vector of x values, y is the vector of y values, and n is the desired degree of the polynomial.

How do I ensure accurate curve fitting with polyfit()?

To ensure accurate curve fitting with polyfit(), it is important to choose an appropriate degree for the polynomial. This can be done by trying different degrees and comparing the resulting curves to the original data points. It is also important to make sure that the data points are evenly spaced and that there are enough data points to accurately represent the curve.

Can polyfit() be used for non-linear curve fitting?

No, polyfit() is specifically designed for fitting polynomial curves to data points. It cannot be used for non-linear curve fitting. For non-linear curve fitting, you can use other functions in MATLAB such as lsqcurvefit() or fminsearch().

How can I plot the curve fit using the coefficients from polyfit()?

To plot the curve fit using the coefficients from polyfit(), you can use the polyval() function in MATLAB. This function takes in the coefficients as well as a range of x values and returns the corresponding y values for the curve. You can then plot these values along with your original data points to visualize the curve fit.

Back
Top