Showing the equation of the best fit lines per graph
17 views (last 30 days)
Show older comments
I am trying to get the equations on the best fit lines to be displayed in the graph, I don't have any idea of how to get it to work. I looked up at few ways but nothing worked for me.
Code:
x = [0.54 0.86 0.99 1.19 1.44 1.76 1.97 2.23 2.44]
y = [1.08 1.71 1.98 2.39 2.89 3.52 3.95 4.45 4.89]
x1 = [0.54 0.86 0.99 1.19 1.44 1.76 1.97 2.23 2.44]
y1 = [0.78 1.18 1.57 1.96 2.35 2.74 3.14 3.53 3.92]
close all
figure(1)
scatter(x1,y1)
p = polyfit(x1,y1,1)
f = polyval(p,x1);
p1 = polyfit(x,y,1)
f2 = polyval(p1,x);
hold on
plot(x1,f,'--r',x,f2,'-b')
title('Force vs mu_0 for Sphereical Cup')
xlabel('mu_0 (N)')
ylabel('Force')
hold on
legend('Experimental values','Best Fit Line','Therotical values')
x3 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53]
y3 = [0.39 0.78 1.18 1.57 1.96 2.35 2.74 3.14 3.53 3.92]
x4 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53]
y4 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53]
figure(2)
scatter(x3,y3)
p = polyfit(x3,y3,3)
f = polyval(p,x3);
p2 = polyfit(x4,y4,1)
f2 = polyval(p2,x4);
hold on
plot(x3,f,'--r',x4,f2,'-b')
title('Force vs mu_0 for Flat Plate')
xlabel('mu_0 (N)')
ylabel('Force')
hold on
legend('Experimental values','Best Fit Line','Theoretical values')
0 Comments
Accepted Answer
Dave B
on 12 Sep 2021
Are you struggling with how to get the text? or how to display it on the chart?
Here I put them in the legend, and also added the cubic equation to the chart with the text function. I chose 2 decimal places but you can consider a different precision as you like.
x3 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53];
y3 = [0.39 0.78 1.18 1.57 1.96 2.35 2.74 3.14 3.53 3.92];
x4 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53];
y4 = [0.47 0.68 1.32 1.76 2.23 2.74 3.16 3.61 4.16 4.53];
scatter(x3,y3)
p = polyfit(x3,y3,3);
f = polyval(p,x3);
p2 = polyfit(x4,y4,1);
f2 = polyval(p2,x4);
hold on
plot(x3,f,'--r',x4,f2,'-b')
title('Force vs mu_0 for Flat Plate')
xlabel('mu_0 (N)')
ylabel('Force')
hold on
eq1 = sprintf('f(x) = %0.2fx^3 + %0.2fx^2 + %0.2fx + %0.2f',p);
eq2 = sprintf('f(x) = %0.2fx + %0.2f',p2);
legend('Experimental values' , ['Best Fit Line: ' eq1], ['Theoretical values: ' eq2])
text(max(xlim),min(ylim),eq1,'VerticalAlignment','bottom','HorizontalAlignment','right','Color','r')
More Answers (0)
See Also
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!