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

  • Context: MATLAB 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary
SUMMARY

The discussion focuses on efficiently using the polyfit() function in MATLAB for curve fitting on two-dimensional arrays. The user seeks to apply polyfit to each row of the arrays vxb and vtime without using explicit loops. The solution involves leveraging MATLAB's ability to handle matrix operations, as detailed in the linked MathWorks article. The recommended approach allows for obtaining a polynomial coefficient array p(i,:) for each row, enhancing performance and code readability.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of polynomial fitting using polyfit()
  • Knowledge of matrix operations in MATLAB
  • Basic concepts of curve fitting and polynomial regression
NEXT STEPS
  • Explore MATLAB's polyval() function for evaluating polynomial fits
  • Learn about vectorization techniques in MATLAB to optimize performance
  • Investigate advanced polynomial fitting methods, such as lsqcurvefit()
  • Review MATLAB documentation on matrix manipulation and operations
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and engineers who require efficient methods for polynomial curve fitting on multi-dimensional datasets.

Saladsamurai
Messages
3,009
Reaction score
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