MATLAB Answers

Showing the equation of the best fit lines per graph

7 ビュー (過去 30 日間)
Cody Anderson
Cody Anderson 2021 年 9 月 12 日
コメント済み: Cody Anderson 2021 年 9 月 12 日
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')

採用された回答

Dave B
Dave B 2021 年 9 月 12 日
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')
  1 件のコメント
Cody Anderson
Cody Anderson 2021 年 9 月 12 日
It was to get the best fit line to display the equation of it. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by