- Do not multiply a 2x1 array with a 4x1 array.
- Check your code as you write it.
My matlab script is throwing an error for array dimensionality. "Arrays have incompatible sizes for this operation."
10 views (last 30 days)
Show older comments
Mohammad Nazmus Saquib
on 26 Feb 2022
The error I'm getting is, "Arrays have incompatible sizes for this operation.
Error in deflection_calculation>@(x)sum(F.*max(x-sort([x_f;x_r]),0),1) (line 16)
bm = cell2mat(arrayfun(@(x) sum(F.*max(x-sort([x_f;x_r]),0),1),xcp,'UniformOutput',false)); % (lbf*inch)"
Here's the code I was working with. You can play with the numbers and let me know which dimension is actually causing the problem and what change can solve the problem. Thanks.
function Shaft_deflection_calculation(E,F,x_f,x_r,x_s,d)
E = 30*10^6;
F = [20; 45];
x_f = [5.25; 5.25];
x_r = [0.5; 10];
x_s = [1;2;3;4;5;6];
d = [1;1;1;1;1;1;1];
% calculations %%%%%%%%%%%%%%%%%%%
xcp = sort([x_f; x_r; x_s]);
xp_l = sort(cell2mat(arrayfun(@(x) (find(xcp==x)),[x_f;x_r],'UniformOutput',false)));
for i = 1:length(xp_l)
if xp_l(i) >1 && xp_l(i) < length(xcp), d = [d(1:xp_l(i)-1); d(xp_l(i)-1); d(xp_l(i):end)]; end
end
I = repmat(pi*d.^4/64,1,size(F,2)); % calculate the second moment of area (inch^4)
% calculation of the bending moment distribution using singularity functions
bm = cell2mat(arrayfun(@(x) sum(F.*max(x-sort([x_f;x_r]),0),1),xcp,'UniformOutput',false)); % (lbf*inch)
if x_f(end) == xcp(end) || x_r(end) == xcp(end), F = F(1:end-1,:); xp_l = xp_l(1:end-1); end
if(~isempty(x_s))
xs_l = sort(cell2mat(arrayfun(@(x) (find(xcp==x)),x_s,'UniformOutput',false)));
step = bm(xs_l,:)./I(xs_l,:) - bm(xs_l,:)./I(xs_l-1,:); % (lbf/inch^3)
slope2 = ((bm(xs_l+1,:)-bm(xs_l,:))./I(xs_l,:))./(xcp(xs_l+1)-xcp(xs_l)); slope1 = ((bm(xs_l,:)-bm(xs_l-1,:))./I(xs_l-1,:))./(xcp(xs_l)-xcp(xs_l-1));
delta_slope = slope2-slope1; % (lbf/inch^4)
% apply boundary conditions
c1 = -((1/6)*sum((F./I(xp_l,:)).*max(x_r(end)-xcp(xp_l,:),0).^3,1) + (1/2)*sum(step.*max(x_r(end)-x_s,0).^2,1) + (1/6)*sum(delta_slope.*max(x_r(end)-x_s,0).^3,1))/x_r(end); % (lbf/inch^2)
% define functions to calculate the deflection and the slope
theta = @(x) (1/E)*((1/2)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^2,1) + sum(step.*max(x-x_s,0),1) + (1/2)*sum(delta_slope.*max(x-x_s,0).^2,1)+c1); % (radians)
delta = @(x) (1/E)*((1/6)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^3,1) + (1/2)*sum(step.*max(x-x_s,0).^2,1) + (1/6)*sum(delta_slope.*max(x-x_s,0).^3,1)+c1*x); % (inches)
else
c1 = -((1/6)*sum((F./I(xp_l,:)).*max(x_r(end)-xcp(xp_l,:),0).^3,1))/x_r(end); % (lbf/inch^2)
theta = @(x) (1/E)*((1/2)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^2,1)+c1); % (radians)
delta = @(x) (1/E)*((1/6)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^3,1)+c1*x); % (inches)
end
delta_r = cell2mat(arrayfun(delta,sort([x_f;x_r]),'UniformOutput',false));
theta_r = cell2mat(arrayfun(theta,sort([x_f;x_r]),'UniformOutput',false));
str = 'deflection in the xy plane (inches):'; fprintf('%s \n\n',str); fprintf('%+5.3e \n',delta_r(:,1)');
str = 'slope about the z axis (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',theta_r(:,1)');
str = 'deflection in the xz plane (inches):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',delta_r(:,2)');
str = 'slope about the y axis (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',theta_r(:,2)');
str = 'resultant deflection (inches):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',(sqrt(sum(delta_r.^2,2)))');
str = 'resultant slope (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',(sqrt(sum(theta_r.^2,2)))');
end
0 Comments
Accepted Answer
Stephen23
on 26 Feb 2022
In lieu of actual information, assuming that all input arguments are scalar:
Shaft_deflection_calculation(1,2,3,4,5,6)
The cause is the anonymous function on that line, in particular this part:
F.*max(x-sort([x_f;x_r]),0)
^ has size 2x1
^^^^^^^^^^^^^^^^^^^^^^^^ has size 4x1
"what change can solve the problem"
function Shaft_deflection_calculation(E,F,x_f,x_r,x_s,d)
E = 30*10^6;
F = [20; 45];
x_f = [5.25; 5.25];
x_r = [0.5; 10];
x_s = [1;2;3;4;5;6];
d = [1;1;1;1;1;1;1];
% calculations %%%%%%%%%%%%%%%%%%%
xcp = sort([x_f; x_r; x_s]);
xp_l = sort(cell2mat(arrayfun(@(x) (find(xcp==x)),[x_f;x_r],'UniformOutput',false)));
for i = 1:length(xp_l)
if xp_l(i) >1 && xp_l(i) < length(xcp), d = [d(1:xp_l(i)-1); d(xp_l(i)-1); d(xp_l(i):end)]; end
end
I = repmat(pi*d.^4/64,1,size(F,2)); % calculate the second moment of area (inch^4)
% calculation of the bending moment distribution using singularity functions
bm = cell2mat(arrayfun(@(x) sum(F.*max(x-sort([x_f;x_r]),0),1),xcp,'UniformOutput',false)); % (lbf*inch)
if x_f(end) == xcp(end) || x_r(end) == xcp(end), F = F(1:end-1,:); xp_l = xp_l(1:end-1); end
if(~isempty(x_s))
xs_l = sort(cell2mat(arrayfun(@(x) (find(xcp==x)),x_s,'UniformOutput',false)));
step = bm(xs_l,:)./I(xs_l,:) - bm(xs_l,:)./I(xs_l-1,:); % (lbf/inch^3)
slope2 = ((bm(xs_l+1,:)-bm(xs_l,:))./I(xs_l,:))./(xcp(xs_l+1)-xcp(xs_l)); slope1 = ((bm(xs_l,:)-bm(xs_l-1,:))./I(xs_l-1,:))./(xcp(xs_l)-xcp(xs_l-1));
delta_slope = slope2-slope1; % (lbf/inch^4)
% apply boundary conditions
c1 = -((1/6)*sum((F./I(xp_l,:)).*max(x_r(end)-xcp(xp_l,:),0).^3,1) + (1/2)*sum(step.*max(x_r(end)-x_s,0).^2,1) + (1/6)*sum(delta_slope.*max(x_r(end)-x_s,0).^3,1))/x_r(end); % (lbf/inch^2)
% define functions to calculate the deflection and the slope
theta = @(x) (1/E)*((1/2)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^2,1) + sum(step.*max(x-x_s,0),1) + (1/2)*sum(delta_slope.*max(x-x_s,0).^2,1)+c1); % (radians)
delta = @(x) (1/E)*((1/6)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^3,1) + (1/2)*sum(step.*max(x-x_s,0).^2,1) + (1/6)*sum(delta_slope.*max(x-x_s,0).^3,1)+c1*x); % (inches)
else
c1 = -((1/6)*sum((F./I(xp_l,:)).*max(x_r(end)-xcp(xp_l,:),0).^3,1))/x_r(end); % (lbf/inch^2)
theta = @(x) (1/E)*((1/2)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^2,1)+c1); % (radians)
delta = @(x) (1/E)*((1/6)*sum((F./I(xp_l,:)).*max(x-xcp(xp_l,:),0).^3,1)+c1*x); % (inches)
end
delta_r = cell2mat(arrayfun(delta,sort([x_f;x_r]),'UniformOutput',false));
theta_r = cell2mat(arrayfun(theta,sort([x_f;x_r]),'UniformOutput',false));
str = 'deflection in the xy plane (inches):'; fprintf('%s \n\n',str); fprintf('%+5.3e \n',delta_r(:,1)');
str = 'slope about the z axis (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',theta_r(:,1)');
str = 'deflection in the xz plane (inches):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',delta_r(:,2)');
str = 'slope about the y axis (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',theta_r(:,2)');
str = 'resultant deflection (inches):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',(sqrt(sum(delta_r.^2,2)))');
str = 'resultant slope (radians):'; fprintf('\n %s \n\n',str); fprintf('%+5.3e \n',(sqrt(sum(theta_r.^2,2)))');
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!