I have an error that im not sure how to fix

2 views (last 30 days)
My code is producing the error of matrix dimensions must agree. Im not sure how to fix it. Can anyone help me?
clear % clears assigned values from memory
clc % clears command window
m = [0.15 0.14 0.13 0.12 0.11 0.1 0.09 0.08 0.07 0.06 0.05];
% constant mass used for all of the spring trials, measued in kg.
L1 = [0.63 0.65 0.605 0.58 0.54 0.49 0.47 0.43 0.395 0.36 0.32];
% lengths obtained from the 1st long spring trial, measured in meters.
L2 = [0.62 0.63 0.54 0.51 0.42 0.41 0.39 0.38 0.36 0.32 0.28];
% lengths obtained from the 2nd long spring tiral, measured in meters.
L3 = [0.18 0.17 0.16 0.15 0.12 0.122 0.11 0.1 0.1 0.09 0.09];
% lengths obtained from shorter spring trial, measured in meters.
L_01 = (0.15);
% relaxed length of the 1st long spring, measured in meters.
L_02 = (0.12);
% relaxed length of the 2nd long spring, measured in meters.
L_03 = (0.095);
% relaxed length of the short spring, measured in meters.
g = 9.8;
% gravitational constant, measured in meters per second^2
%Model Equations
x1 = ((1) ./ (m.*g));
y1 = ((1) ./ (L1) - (L_01));
x2 = ((1) ./ (m.*g));
y2 = ((1) ./ (L2 - L_02));
x3 = ((1) ./ (m.*g));
y3 = (11) ./ (L3) - (L_03);
%Sets up G matrix
G1=x1;
G1 = ones(length(x1),1);
G1(:,2)=x1;
G2=x2;
G2 = ones(length(x2),1);
G2(:,2)=x2;
G3 = x3;
G3 = ones(length(x3),1);
%spring constants
k1=G1\y1;
k2=G2\y2;
k3=G3\y3;
%creates line
X1 = [min(x1): 1e-3: max(x1)]';
X2 = [min(x2): 1e-3: max(x2)]';
X3 = [min(x3): 1e-3: max(x3)]';
Y1 = k1 (1,1)+k1(2,1).*X1;
Y2 = k2 (1,1)+k2(2,1).*X2;
Y3 = k3 (1,1)+k2(2,1).*X3;
%Uncertainties
sig1 = sqrt(sum((y1-G1*k1).^2/(length(x1)-1)));
sig2 = sqrt(sum((y2-G2*k2).^2/(length(x2)-1)));
sig3 = sqrt(sum((y3-G3*k3).^2/(length(x3)-1)));
se1_1 = sig1.*sqrt(1./((length(x1)+(X1)-mean(x1)^2)/((length(x1)-1)*std(x1)^2))); % error for k1
se1_2 = sig2.*sqrt(1./((length(x_2)+mean(x_2)^2)/((length(x_2)-1)*std(x_2)^2))); % error for k1
se2_1 = sig1.*sqrt(1./((length(x1)-1)*std(x1)^2)); % error for k2
se2_2 = sig2.*sqrt(1./((length(x2)-1)*std(x2)^2)); % error for k2
%ci1 = tinv(0.975, length(x1)-2).*se11;
%ci2 = tinv(0.975, length(x2)-2).*se12;
SE1 = sig1.*sqrt(1/length(x1) + (X1 - mean(x1)).^2/((length(x1) - 1)*std(x1).^2));
SE2 = sig2.*sqrt(1/length(x2) + (X2 - mean(x2)).^2/((length(x2) - 1)*std(x2).^2));
SE3 = sig3.*sqrt(1/length(x3) + (X3 - mean(x3)).^2/((length(x3) - 1)*std(x3).^2));
CI1 = tinv(0.975, length(x1) - 1).*SE1;
CI2 = tinv(0.975, length(x2) - 1).*SE2;
CI3 = tinv(0.975, length(x3) - 1).*SE3;
figure(1) %this is a certain type of figure Matlab will draw
clf %clears existing graphs
plot(x1,y1,'ob') %plot command
%plots solid blue circles of size "5"
hold on %Prompt to matlab that more things will be added(?)
plot(X1,Y1, 'r-', 'LineWidth', 2)
plot(X1,Y1+CI1, 'g--', 'LineWidth', 1)
plot(X1,Y1-CI1, 'g--', 'LineWidth', 1)
hold off %Finished plotting
xlabel('$$\frac{1} {mg} $$ ','Interpreter','Latex')
ylabel('$$\frac{1} {l_1-l_0}$$ ','Interpreter','Latex')
print(figure(1),'-dpng', 'lab4fig1') %displays graph
% set(figure(1),'color','white')
figure(2) %this is a certain type of figure Matlab will draw
clf %clears existing graphs
plot(x2,y2,'ob') %plot command
%plots solid blue circles of size "5"
hold on %Prompt to matlab that more things will be added
plot(X2, Y2, 'r-', 'LineWidth', 2)
plot(X2, (Y2+CI2), 'g--', 'LineWidth', 1)
plot(X2, (Y2-CI2), 'g--', 'LineWidth', 1)
hold off %Finished plotting
xlabel('$$\frac{1} {mg} $$ ','Interpreter','Latex')
ylabel('$$\frac{1} {l_1-l_0}$$ ','Interpreter','Latex')
print(figure(2),'-dpng', 'lab4fig2') %displays graph
% set(figure(2),'color','white')
figure(3) %this is a certain type of figure Matlab will draw
clf %clears existing graphs
plot(x3,y3,'ob') %plot command
%plots solid blue circles of size "5"
hold on %Prompt to matlab that more things will be added
plot(X3, Y3, 'r-', 'LineWidth', 2)
plot(X3, (Y3+CI2), 'g--', 'LineWidth', 1)
plot(X3, (Y3-CI2), 'g--', 'LineWidth', 1)
hold off %Finished plotting
xlabel('$$\frac{1} {mg} $$ ','Interpreter','Latex')
ylabel('$$\frac{1} {l_1-l_0}$$ ','Interpreter','Latex')
print(figure(3),'-dpng', 'lab4fig3') %displays graph
% set(figure(3),'color','white')

Answers (1)

Walter Roberson
Walter Roberson on 17 Nov 2018
k1=G1\y1.';
probably.

Categories

Find more on Programming 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!