I am having trouble getting the Best fit line through the data points in these two codes can you please help me out where I am messing up the code?

5 次查看(过去 30 天)
%PS253 Lab 7 Analysis
close all
clear all
clc
figure(1)
%Analysis Stretch
x = [.015 .018 .025 .027 .033 .04 .044 .049 .055 .058 .065 .07 .075 .081 .087];
y = [.637 .686 .882 .98 1.176 1.3906 1.47 1.66502 1.86004 1.95804 2.15404 2.35004 2.44608 2.64306 2.83808];
err = .025*ones(size(y));
errorbar(x,y,err,'.')
p = polyfit(x,y,1);
%Find the slope in th graph
slope = (15*sum(x.*y)-sum(x).*sum(y))./(15*sum(x).^2.-(sum(x)).^2);
hold on
Q = slope.*x;
plot (x,Q,'r-')
legend('Data Points','Best Fit')
title ('Stretch')
xlabel ('Length (m)')
ylabel('Force (N)')
hold off
figure
%Analysis Part 2 Oscillations
M = [.14990 .19980 .24970 .26970 .2897 .2996 .31950 .33950 .34950 .36950 .38950 .39950 .4094 .41940 .54950];
T = [24.0 31.8 32.8 42 46 47.6 50.7 53.8 55.4 58.5 61.7 63.3 64.8 66.4 86.8];
err = .08*ones(size(M));
errorbar(M,T,err,'.')
p = polyfit(M,T,1);
%Find the slope in th graph
slope = (15*sum(M.*T)-sum(M).*sum(T))/(15*sum(M).^2.-(sum(M)).^2)
Q = slope.*M;
plot (M,Q,'r-')
hold on
legend('Data Points','Best Fit')
title ('Expected T')
xlabel ('Mass (kg)')
ylabel('T (s)')
hold off
%PS253 Lab 9 Analysis
% T equals change of temp. J equals change of L K equals original L. L
% equals 1L/2L.
close all
clear all
clc
figure(1)
%Analysis Brass
T = [52 51 49 45 38 26 0];
J = [.1 .15 .16 .19 .26 .35 .7];
K = 518.0;
L = J./K;
err = .0005*ones(size(L));
errorbar (T,L,err,'.')
slope = (sum(T.*L)./sum(T.^2));
dslope = (1./(sqrt(sum(T.^2))));
hold on
Q = slope.*L;
R = dslope.*L;
plot (T,Q,'r-')
plot (T,R,'b-')
legend('Data Points','Equation 4','Equation 5')
title ('Steel Thermal Expansion')
xlabel ('Temp Change Celcius')
ylabel('Length')
hold off
figure
clc
%Analysis Steel
T = [0 20.0 35.0 44.0 48.0 51.0 52.0];
J = [.340 .220 .160 .110 .090 .060 .040];
K = 518.0;
L = J./K;
err = .0005*ones(size(L));
errorbar(T,L,err,'.')
%Find the slope in th graph
slope = (sum(T.*L)./sum(T.^2));
dslope = (1./(sqrt(sum(T.^2))));
hold on
Q = slope.*L;
R = dslope.*L;
plot (T,Q,'r-')
plot (T,R,'b-')
legend('Data Points','Equation 4','Equation 5')
title ('Steel Thermal Expansion')
xlabel ('Temp Change Celcius')
ylabel('Length')
hold off

回答(1 个)

KSSV
KSSV 2021-12-2
Use polyval to evaluate the data points of fit curve.
figure(1)
%Analysis Stretch
x = [.015 .018 .025 .027 .033 .04 .044 .049 .055 .058 .065 .07 .075 .081 .087];
y = [.637 .686 .882 .98 1.176 1.3906 1.47 1.66502 1.86004 1.95804 2.15404 2.35004 2.44608 2.64306 2.83808];
err = .025*ones(size(y));
errorbar(x,y,err,'.')
p = polyfit(x,y,1);
%Find the slope in th graph
slope = (15*sum(x.*y)-sum(x).*sum(y))./(15*sum(x).^2.-(sum(x)).^2);
hold on
% Q = slope.*x;
Q = polyval(p,x) ;
plot (x,Q,'r-')
legend('Data Points','Best Fit')
title ('Stretch')
xlabel ('Length (m)')
ylabel('Force (N)')
hold off

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by