Linear fitting is a fundamental technique to model the relationship between two variables. In MATLAB, a linear fit finds the best straight line through a set of data points, minimizing the difference between the observed values and predicted values.
The linear model is represented as:
y=m∗x+cy = m*x + cy=m∗x+c
where mmm is the slope and ccc is the intercept.
Linear fitting is widely used in engineering, data science, and research applications.
Suppose you have the following dataset:
x = [1 2 3 4 5 6]; y = [2.1 2.9 3.7 4.0 5.2 5.8]; Here, x is the independent variable and y is the dependent variable.
MATLAB provides multiple ways to perform a linear fit.
Method 1: Using polyfit()
coeff = polyfit(x, y, 1); % 1 = linear slope = coeff(1); intercept = coeff(2); fprintf('Slope: %.2f, Intercept: %.2f ', slope, intercept); Method 2: Using Matrix Approach (Least Squares)
X = [x' ones(length(x),1)]; % Design matrix Y = y'; theta = (X'*X)(X'*Y); % Least squares solution slope = theta(1); intercept = theta(2); fprintf('Slope: %.2f, Intercept: %.2f ', slope, intercept); Method 3: Using fit() Function
ft = fittype('poly1'); % Linear fit f = fit(x', y', ft); plot(f, x, y) Once the slope and intercept are determined, you can calculate predicted values:
y_fit = slope*x + intercept; Plot the data and linear fit:
scatter(x, y, 'filled') hold on plot(x, y_fit, 'r-', 'LineWidth', 2) title('Linear Fit in MATLAB') xlabel('X') ylabel('Y') legend('Data points', 'Linear fit') grid on hold off Compute the R-squared value to check the goodness of fit:
SS_res = sum((y - y_fit).^2); SS_tot = sum((y - mean(y)).^2); R_squared = 1 - SS_res/SS_tot; fprintf('R-squared: %.4f ', R_squared); A value close to 1 indicates a strong linear relationship between x and y.
Quick computation of slope and intercept.
Easy visualization with plot and scatter.
Handles large datasets efficiently.
Can extend to polynomial and nonlinear fits using polyfit or fit.
Linear fitting in MATLAB provides a simple, effective, and visual way to model relationships between variables. Whether you are analyzing trends, predicting outcomes, or performing data analysis, MATLAB’s built-in functions make linear regression fast and reliable.
By mastering linear fitting, you can analyze data trends and make accurate predictions with confidence.
Real feedback from students across top engineering universities worldwide.
“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”
“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”
Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.
Operating a modern microgrid is an ongoing balancing act. Between changing solar output, shifting wind speeds, volatile electricity pricing, and unpredictable consumer demand, a...
Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...