Fit Plots to A Given Slope
5 views (last 30 days)
Hi, I have a set of data that I have plotted using MATLAB. The data I have is supposed to fit to a line with a slope of either 0.5, 1, or 2. Which line the data best fits to tells me what type of system the data is from. So if the data best fits to a line of slope 0.5 I know I am working with system A, if it best fits to a line of slope 1 I am working with system B, and if it best fits to a line of slope 2 I am working with system C. The intercept of the lines does not matter. What I need is a way to fit my data to a line of each of the above mentioned slopes and then to find the regression coefficient to tell which line best fits to the data. So in short I need to know how to fit my data to a line of a slope of 0.5, 1, or 2. Please note that I am working in a log-log plot and that I cannot have an intercept of less than zero. Thank you for your help.
Walter Roberson on 21 Aug 2013
x = [....]; %the vector of x values that "data" exist at
sd_half = std(data - 0.5 * x);
sd_one = std(data - x);
sd_two = std(data - 2 * x);
I would suggest, though, trying
coeffs = polyfit(x, data, 2);
slope = coeffs(1);
You might to use the three-output version of polyfit to have centering and scaling done.