![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196969/image.png)
How do I fix this error, please need help!
4 views (last 30 days)
Show older comments
ERROR:
Warning: Rank deficient, rank = 0, tol = inf.
> In Lab4_k_s (line 46)
Warning: Rank deficient, rank = 0, tol = inf.
> In Lab4_k_s (line 50)
Maximum variable size allowed by the program is exceeded.
Error in Lab4_k_s (line 69)
X1 = [min(x1):1e-3:max(x1)]';
>>
CODE:
%These commands clear the workspace and command window, in that order.
clear
clc
% Measured masses of spring 1, 2 and 3 measured in meters(m).
M1=[0 .1 .2 .3 .4 .5 .6 .7 .8 .9]';
M2=[0 .05 .1 .15 .2 .25 .3 .35 .4 .45]';
M3=[.05 .07 .09 .11 .13 .15 .17 .19 .21 .23]';
% Measured distances of spring 1, 2, and 3 measured in kilograms(kg).
L1=[.075 .085 .095 .105 .115 .125 .135 .145 .155 .165]';
L2=[.11 .145 .185 .225 .265 .305 .345 .375 .415 .455]';
L3=[.365 .455 .515 .585 .655 .735 .785 .885 .945 .995]';
g=(9.81);
L01=.19
L02=.08
L03=.1
x1 = ((1) ./ (M1 .* g));
y1 =((1) ./ (L1-L01));
x2 = ((1) ./ (M2 .*g));
y2 =((1) ./ (L2-L02));
x3 = ((1) ./ (M3 .* g));
y3 =((1) ./ (L3-L03));
G1= ones(length(x1), 1);
% G(:, 1) = b;
G1(:, 2) = x1;
k1 = G1 \ y1
G2 = ones(length(x2), 1);
G2(:, 2) = x2;
k2 = G2 \ y2
G3= ones(length(x3), 1);
% G(:, 1) = b;
G3(:, 2) = x3;
k3 = G3 \ y3
% uncertainties
sigma1 = sqrt(sum((y1-G1*k1).^2)./(length(x1)-2));
sigma2 = sqrt(sum((y2-G2*k2).^2)./(length(x2)-2));
sigma3 = sqrt(sum((y3-G3*k3).^2)./(length(x3)-2));
se11 = sigma1.*sqrt(1./((length(x1)+mean(x1)^2/((length(x1)-1)*std(x1)^2))));
se12 = sigma2.*sqrt(1./((length(x2)+mean(x2)^2/((length(x2)-1)*std(x2)^2))));
se13 = sigma3.*sqrt(1./((length(x3)+mean(x3)^2/((length(x3)-1)*std(x3)^2))));
se21 = sigma1.*sqrt(1./((length (x1)-1)*std(x1)^2));
se22 = sigma2.*sqrt(1./((length (x2)-1)*std(x2)^2));
se23 = sigma3.*sqrt(1./((length (x3)-1)*std(x3)^2));
% creating a line of best fit
X1 = [min(x1):1e-3:max(x1)]';
Y1 = k1(1,1) + k1(2,1).*X1;
X2 = [min(x2):1e-3:max(x2)]';
Y2 = k2(1,1) + k2(2,1).*X2;
X3 = [min(x3):1e-3:max(x3)]';
Y3 = k3(1,1) + k3(2,1).*X3;
SE1=sigma1.*sqrt(1/length(x1)+(X1-mean(x1)).^2./((length(x1)-1)*std(x1).^2));
Ci1=tinv(.975,length(x1)-2).*SE1;
SE2=sigma2.*sqrt(1/length(x2)+(X2-mean(x2)).^2./((length(x2)-1)*std(x2).^2));
Ci2=tinv(.975,length(x2)-2).*SE2;
SE3=sigma3.*sqrt(1/length(x3)+(X3-mean(x3)).^2./((length(x3)-1)*std(x3).^2));
Ci3=tinv(.975,length(x3)-2).*SE3;
figure(1)
clf
plot(x1, y1, 'ob')
hold on
% plotting the conidence intervals
plot (X1, Y1, 'g')
plot(X1,Y1+Ci1,'g--')
plot(X1,Y1-Ci1,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$', 'interpreter','latex')
title('Spring Constant')
print(figure(1), '-dpng', 'figure1')
figure(2)
clf
plot(x2, y2, 'ob')
hold on
% plotting the conidence intervals
plot (X2, Y2, 'R')
plot(X2,Y2+Ci2,'g--')
plot(X2,Y2-Ci2,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$','interpreter','latex')
title('Spring Constant')
print(figure(2), '-dpng', 'figure2')
figure(3)
clf
plot(x3, y3, 'ob')
hold on
% plotting the conidence intervals
plot (X3, Y3, 'y')
plot(X3,Y3+Ci3,'g--')
plot(X3,Y3-Ci3,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$','interpreter','latex')
title('Spring Constant')
print(figure(3), '-dpng', 'figure3')
0 Comments
Accepted Answer
Stephan
on 26 Nov 2018
Edited: Stephan
on 26 Nov 2018
Hi,
you are a little to precise. Your code produces an Inf for max(x1), due to your values of M1 and M2, use a value near zero for the first entry instead of zeros to avoid this:
% Measured masses of spring 1, 2 and 3 measured in meters(m).
M1=[1e-6 .1 .2 .3 .4 .5 .6 .7 .8 .9]';
M2=[1e-6 .05 .1 .15 .2 .25 .3 .35 .4 .45]';
The code takes some time to execute, but it runs without errors if you fix this and shows the figures as expected:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196969/image.png)
Best regards
Stephan
4 Comments
Stephan
on 27 Nov 2018
Edited: Stephan
on 27 Nov 2018
%These commands clear the workspace and command window, in that order.
clear
clc
% Measured masses of spring 1, 2 and 3 measured in meters(m).
M1=[.1 .2 .3 .4 .5 .6 .7 .8 .9]';
M2=[.05 .1 .15 .2 .25 .3 .35 .4 .45]';
% M1=[0 .1 .2 .3 .4 .5 .6 .7 .8 .9]';
% M2=[0 .05 .1 .15 .2 .25 .3 .35 .4 .45]';
M3=[.05 .07 .09 .11 .13 .15 .17 .19 .21 .23]';
% Measured distances of spring 1, 2, and 3 measured in kilograms(kg).
L1=[.085 .095 .105 .115 .125 .135 .145 .155 .165]';
L2=[.145 .185 .225 .265 .305 .345 .375 .415 .455]';
% L1=[.075 .085 .095 .105 .115 .125 .135 .145 .155 .165]';
% L2=[.11 .145 .185 .225 .265 .305 .345 .375 .415 .455]';
L3=[.365 .455 .515 .585 .655 .735 .785 .885 .945 .995]';
g=(9.81);
L01=.19;
L02=.08;
L03=.1;
x1 = ((1) ./ (M1 .* g));
y1 =((1) ./ (L1-L01));
x2 = ((1) ./ (M2 .*g));
y2 =((1) ./ (L2-L02));
x3 = ((1) ./ (M3 .* g));
y3 =((1) ./ (L3-L03));
G1= ones(length(x1), 1);
% G(:, 1) = b;
G1(:, 2) = x1;
k1 = G1 \ y1
G2 = ones(length(x2), 1);
G2(:, 2) = x2;
k2 = G2 \ y2
G3= ones(length(x3), 1);
% G(:, 1) = b;
G3(:, 2) = x3;
k3 = G3 \ y3
% uncertainties
sigma1 = sqrt(sum((y1-G1*k1).^2)./(length(x1)-2));
sigma2 = sqrt(sum((y2-G2*k2).^2)./(length(x2)-2));
sigma3 = sqrt(sum((y3-G3*k3).^2)./(length(x3)-2));
se11 = sigma1.*sqrt(1./((length(x1)+mean(x1)^2/((length(x1)-1)*std(x1)^2))));
se12 = sigma2.*sqrt(1./((length(x2)+mean(x2)^2/((length(x2)-1)*std(x2)^2))));
se13 = sigma3.*sqrt(1./((length(x3)+mean(x3)^2/((length(x3)-1)*std(x3)^2))));
se21 = sigma1.*sqrt(1./((length (x1)-1)*std(x1)^2));
se22 = sigma2.*sqrt(1./((length (x2)-1)*std(x2)^2));
se23 = sigma3.*sqrt(1./((length (x3)-1)*std(x3)^2));
% creating a line of best fit
X1 = [min(x1):1e-2:max(x1)]';
Y1 = k1(1,1) + k1(2,1).*X1;
X2 = [min(x2):1e-2:max(x2)]';
Y2 = k2(1,1) + k2(2,1).*X2;
X3 = [min(x3):1e-2:max(x3)]';
Y3 = k3(1,1) + k3(2,1).*X3;
SE1=sigma1.*sqrt(1/length(x1)+(X1-mean(x1)).^2./((length(x1)-1)*std(x1).^2));
Ci1=tinv(.975,length(x1)-2).*SE1;
SE2=sigma2.*sqrt(1/length(x2)+(X2-mean(x2)).^2./((length(x2)-1)*std(x2).^2));
Ci2=tinv(.975,length(x2)-2).*SE2;
SE3=sigma3.*sqrt(1/length(x3)+(X3-mean(x3)).^2./((length(x3)-1)*std(x3).^2));
Ci3=tinv(.975,length(x3)-2).*SE3;
figure(1)
clf
plot(x1, y1, 'ob')
hold on
% plotting the conidence intervals
plot (X1, Y1, 'g')
plot(X1,Y1+Ci1,'g--')
plot(X1,Y1-Ci1,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$', 'interpreter','latex')
title('Spring Constant')
print(figure(1), '-dpng', 'figure1')
figure(2)
clf
plot(x2, y2, 'ob')
hold on
% plotting the conidence intervals
plot (X2, Y2, 'R')
plot(X2,Y2+Ci2,'g--')
plot(X2,Y2-Ci2,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$','interpreter','latex')
title('Spring Constant')
print(figure(2), '-dpng', 'figure2')
figure(3)
clf
plot(x3, y3, 'ob')
hold on
% plotting the conidence intervals
plot (X3, Y3, 'y')
plot(X3,Y3+Ci3,'g--')
plot(X3,Y3-Ci3,'g--')
hold off
xlabel('$$\frac{1}{mg}$$','interpreter','latex')
ylabel('$$\frac{1}{l_1-l_0}$$','interpreter','latex')
title('Spring Constant')
print(figure(3), '-dpng', 'figure3')
This is the whole code i used @Matlab Online and it worked for me - maybe a copy paste error...
More Answers (0)
See Also
Categories
Find more on Smoothing 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!