Once I've found a custom equation for a scatter plot with the curve fitting tool, how do I apply that line to the scatter plot in live editor?

2 views (last 30 days)
I have a scatter plot, and have rearranged for the custom equation but I do not know how to apply this equation to my scatter plot on my livescript.

Accepted Answer

Pawel Jastrzebski
Pawel Jastrzebski on 2 Jan 2018
Edited: Pawel Jastrzebski on 3 Jan 2018
IF you're working on a one-off case, use the in-built tools:
Once you're happy with the plot settings, you can always get Matlab to generate the code of the plot for you - this way you can learn how certain things get done and develop your own code, which will be more human-friendly:
But if you're working on a re-usable code, read these:
And check out the code below:
clear all;
clc;
% DATA
x = 1:20;
y = randn(1,20);
figure
plot(x,y,'bo');
hold on;
% CF - CURVE FITTING DATA
CFcoeff = polyfit(x,y,1); % coeeficients of the fit | 1 for linear fit
xLimits = xlim; % limits of the current plot
xCF = linspace(xLimits(1),xLimits(2),50); % xData for the fit line
yCF = polyval(CFcoeff,xCF);
plot(xCF,yCF,'r-');
% ADD EQUATION ANNOTATION TO THE PLOT:
equationText = ['y = ' sprintf('%.2f',CFcoeff(1)) 'x' sprintf('%+.2f',CFcoeff(2))];
text(x(1), yCF(1)+0.2,equationText,...
'Color','r');

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!