Assuming you have your x-axis data in a variable t
and y-axis data in a variable y
, you can perform linear regression using MATLAB's polyfit
function and add the resulting line to the plot using plot
function.
Here's the code to add a linear fit to the plot for time (tfit) from 0 to 10:
main.m444 chars17 lines
The polyfit
function returns the coefficients of the linear fit in p
. The first coefficient is the slope and second is the intercept. The polyval
function is used to evaluate the fitted line for the time points in tfit
. Finally, the plot
function is used to add both the original data and linear fit line to the same plot.
gistlibby LogSnag