# How to Fit a line to scatter data on scatter plot?

11 views (last 30 days)
Chetan Badgujar on 16 Sep 2020
Commented: Chetan Badgujar on 17 Sep 2020
I have 3 variables speed, dp,eff and I used the following code to 3d scatter plot the data. The scatter plot is attached. My question is how to find the best fit line for the scatter data on scatter plot. I tried curve fitting tool box but not working great. Help me find out the solution. i.e to find out the best fit line( eqn) and draw that line of scatter plot.
Thank you
scatter3(speed,dp,eff)
xlabel('Speed')
ylabel('Dp')
zlabel('Eff')

Alex Sha on 17 Sep 2020
Hi, Chetan, if you don't mind the length of the fitting equation, refer to follow:
x1=Speed, x2=Dp, y=Eff
y = b0+b1*x1+b2*x1^2+b3*x2+b4*x2^2+b5*(exp(-sqr(x1/100)))+b6*(exp(-sqr(x1/100)))^2+b7*(exp(-sqr(x2/1.4)))+b8*(exp(-sqr(x2/1.4)))^2+b9*x1*x2*(exp(-sqr(x1/100)))+b10*x1*x2*(exp(-sqr(x2/1.4)))^2+b11*x1*x2^2*(exp(-sqr(x2/1.4)))+b12*x1*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))^2+b13*x1^2*x2*(exp(-sqr(x1/100)))+b14*x1^2*x2*(exp(-sqr(x2/1.4)))^2+b15*x1^2*x2^2*(exp(-sqr(x2/1.4)))+b16*x1^2*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))^2+b17*x2*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))+b18*x2*(exp(-sqr(x1/100)))^2*(exp(-sqr(x2/1.4)))^2+b19*x2^2*(exp(-sqr(x1/100)))^2*(exp(-sqr(x2/1.4)))+b20*x1*x2*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))^2+b21*x1*x2^2*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))+b22*x1*x2^2*(exp(-sqr(x1/100)))^2*(exp(-sqr(x2/1.4)))^2+b23*x1^2*x2*(exp(-sqr(x1/100)))^2*(exp(-sqr(x2/1.4)))+b24*x1^2*x2^2*(exp(-sqr(x1/100)))*(exp(-sqr(x2/1.4)))^2
Sum of Squared Residual: 0.0118621810316654
Correlation Coef. (R): 0.962793830997943
R-Square: 0.926971961007696
Parameter Best Estimate
---------- -------------
b0 15.310755982188
b1 -0.132958264716742
b2 -0.00156128206992743
b3 10.4211565559291
b4 4.65796095967851
b5 -48.3481150500531
b6 -22.8688433790466
b7 36.3660070659581
b8 19.7901689881374
b9 0.059559167138516
b10 0.387826626201331
b11 -0.0133030524719128
b12 0.107006174136916
b13 -0.00233928422236141
b14 -0.00258023255196725
b15 0.000880324623195984
b16 -0.00696504324693559
b17 -19.2013564945072
b18 8.53997897435642
b19 34.4260175482625
b20 -0.410840827168776
b21 -0.0126076476622779
b22 0.0759466046214045
b23 0.00332686217055514
b24 -0.000562931147662027
Chetan Badgujar on 17 Sep 2020
Hi Alex,
Yes it does. I have something for you, which fits the same data with 2nd degree polynomial thats the stop condition for me.
1) I did standardiation of variables to get mean 0 and std 1.
2) Fit the lasso eqn to the standadized data in SAS
3) plot the data with standardization.
4) converted that data to actual values and plot again
this works best for me.
%% scan for nan and throw away
eff=rmmissing(eff);
speed = rmmissing(speed);
dp = rmmissing(dp);
%% Standardizaion of varibles {(x-mean)/std} and 2nd degree poly term
y = zscore(eff);
x1 = zscore(speed);
x2 = zscore(dp);
x3=x1.^2;
x4=x2.^2;
x5=x1.*x2;
%% Eqn I got from SAS Lasso procedure R2=.78
y1=1.02+0.32*x1-0.16*x3-0.86*x4; %lasso gave me this eqn
actualeff= (y.*std(eff)+mean(eff));% data back to actual values from standardization i.e y*Std+mean= y'
actualy= (y1.*std(eff)+mean(eff));
%% plot with standization of variable
scatter3(x1,x2,y)
xlabel('Speed')
ylabel('Dp')
zlabel('Eff')
hold on
plot3(x1,x2,y1,'*', 'MarkerSize',2,'LineWidth', 0.1)
hold off
%% fittied curve to data
plot(actualeff)
hold on
actualy(actualy<0)=0;
plot(actualy)
%% plot of converted data from standardiation to actual
scatter3(speed,dp,actualeff)
xlabel('Speed')
ylabel('Dp')
zlabel('Eff')
hold on
plot3(speed,dp,actualy,'*', 'MarkerSize',2,'LineWidth', 0.1)
hold off
%%text(20, 1.2,.14, txt1);
%% This works great for me so far..I am still open for suggestions.
Do you have any ways to present the scatter plot better way for data vizulazation like tail data is not visible also can we make solid fitted line instead of dots. and eqn on plot.

### Categories

Find more on Interpolation in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!