0
0
MATLABdata~10 mins

Curve fitting (polyfit, fit) in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Curve fitting (polyfit, fit)
Start with data points x,y
Choose fitting method: polyfit or fit
Calculate coefficients or fit object
Evaluate fitted curve at desired points
Plot original data and fitted curve
End
Start with data points, choose polyfit or fit, compute the fit, evaluate the curve, then plot results.
Execution Sample
MATLAB
x = 1:5;
y = [2.2 2.8 3.6 4.5 5.1];
p = polyfit(x,y,1);
y_fit = polyval(p,x);
Fits a straight line to points (x,y) using polyfit and evaluates fitted values.
Execution Table
StepActionInput/ConditionResult/Output
1Define xx = [1 2 3 4 5]x vector created
2Define yy = [2.2 2.8 3.6 4.5 5.1]y vector created
3Call polyfitpolyfit(x,y,1)p = [0.73 1.49] (coefficients)
4Call polyvalpolyval(p,x)y_fit = [2.22 2.95 3.68 4.41 5.14] (fitted y)
5Plot data and fitplot(x,y,'o',x,y_fit,'-')Graph shows points and fitted line
6EndAll steps doneCurve fitting complete
💡 All steps completed successfully; fitted line computed and plotted.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
yundefinedundefined[2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1]
pundefinedundefinedundefined[0.73 1.49][0.73 1.49][0.73 1.49]
y_fitundefinedundefinedundefinedundefined[2.22 2.95 3.68 4.41 5.14][2.22 2.95 3.68 4.41 5.14]
Key Moments - 3 Insights
Why does polyfit return two numbers for a linear fit?
polyfit returns coefficients for the polynomial starting with highest degree: here p(1) is slope and p(2) is intercept, as shown in step 3 of execution_table.
Why are fitted y values slightly different from original y?
Because polyfit finds the best line that approximates data, not exact points. Step 4 shows fitted y values close but not identical to original y.
What does the number 1 mean in polyfit(x,y,1)?
It means fitting a polynomial of degree 1, which is a straight line. This is the key input to polyfit in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the first number in p represent?
ASlope of the fitted line
BIntercept of the fitted line
CNumber of data points
DDegree of polynomial
💡 Hint
Check step 3 in execution_table where p = [0.73 1.49]; first number is slope.
At which step do we get the fitted y values for the original x points?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at step 4 in execution_table where y_fit is calculated.
If we change polyfit(x,y,1) to polyfit(x,y,2), what changes in the execution_table?
AStep 4 y_fit will be same as before
BStep 3 coefficients p will have 3 values
CStep 2 y vector changes
DStep 5 plot will not show data points
💡 Hint
Degree 2 polynomial means 3 coefficients; check step 3 for p.
Concept Snapshot
polyfit(x,y,n) fits polynomial degree n to data x,y
Returns coefficients p starting with highest degree
polyval(p,x) evaluates polynomial at x
fit function offers more options and fit types
Plot data and fit to visualize curve fitting
Full Transcript
This example shows how to fit a straight line to data points using MATLAB's polyfit and polyval functions. First, vectors x and y hold the data points. Then polyfit(x,y,1) computes the best line coefficients, slope and intercept. Next, polyval evaluates the fitted line at each x. Finally, plotting shows original points and the fitted line. The execution table traces each step, showing variable values and results. Key moments clarify why polyfit returns two numbers, what the degree means, and why fitted values differ slightly from original data. The visual quiz tests understanding of coefficients, evaluation step, and effect of changing polynomial degree. The snapshot summarizes syntax and behavior for quick reference.