Skip to content

Instantly share code, notes, and snippets.

@cinnamondev
Last active October 30, 2023 12:10
Show Gist options
  • Save cinnamondev/f88da039611fccea544382cc8ba73f8e to your computer and use it in GitHub Desktop.
Save cinnamondev/f88da039611fccea544382cc8ba73f8e to your computer and use it in GitHub Desktop.
MATLAB Helper Function Linear Fit
% Plots a linear fit of a dataset. Multiple data sets can be chained together in an array.
% Pass `true` to confidence parameter to generate a 95% confidence interval for the data.
% Resolution parameter will generate confidence interval with a better resolution.
function output = plotScatterLinearFit(ds_x, ds_y, confidence, resolution)
range = linspace(0, max(ds_x), resolution)
[output, error] = polyfit(ds_x,ds_y,1);
if confidence
[outputline, delta] = polyconf(output, range, error, 'predopt', 'curve')
plot(range, outputline, "-");
hold on
plot(range,outputline+delta,"r--",range,outputline-delta,"r--");
hold off
else
[outputline, delta] = polyval(output,ds_x, s1_e);
plot(range,outputline,"--");
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment