Clear Filters
Clear Filters

The size of X must match the size of Z or the number of columns of Z.

7 views (last 30 days)
w = 450000;
l = 10;
n = 6;
SOM = 505000000;
max = 8;
min = 2;
diff = (max-min)/n;
FOS1 = zeros(n,n);
FOS2 = zeros(n,n);
i = 1;
j = 1;
Vm = (w*l)/2;
%% FOS@different base&height values
for b = min:diff:max
for h = min:diff:max
c = (h/2);
density=8000;
V=b*h*l;
mass=density*V;
Mmax = ((w*l^2)/8)*c;
I = (b*h^3)/12;
Bm = (Mmax*c)/I;
Tmax=(3*Vm)/2*(b*h);
Stressmax=(Mmax*c)/I;
FOS1(i,j) = (SOM/Tmax);
FOS2(i,j) = (SOM/Stressmax);
j = j+1;
end
i = i+1;
end
%% Plotting
b = min:diff:max;
h = min:diff:max;
[B,H]=meshgrid(b,h);
FOS1(i,j) = (SOM/Tmax);
contour3(b,h,FOS1(i,j)');
hold on
title('Beam Design')
xlabel('Base (meters)')
ylabel('Height (meters)')
close all
clear plot
i keep getting an error saying either The size of X must match the size of Z or the number of columns of Z. or Error using contour3 (line 44)
Z must be at least a 2x2 matrix.
Error in Projectrevised (line 44) contour3(b,h,FOS1(i,j)');

Answers (1)

fred  ssemwogerere
fred ssemwogerere on 6 Feb 2020
Hello, it is best practice to to attach your line of code (with comments), in the code description box provided for by the Matlab website, so that various contributors can easily follow, and be in position to advise appropriately.
Back to your question, when calling the "contour3" plot function, the number of columns of your input matrix, should be equal to the length of your vector, "b", while the number of rows of the same input matrix equal to the length of your vector, "h". However, from your code, there could be misuse of the loop counters, "i" and "j". They seem to create a matrix with a length of 49, based on the length of "min:diff:max". As a result, this will not match the size of arrays generated by "meshgrid". This explains the error in your code. I may not be so sure what you are trying to plot, but based on your code, i propose something like this:
w = 450000;
l = 10;
n = 6;
SOM = 505000000;
max = 8;
min = 2;
diff = (max-min)/n;
FOS1 = zeros(n,n);
FOS2 = zeros(n,n);
b = min:diff:max;
h = min:diff:max;
Vm = (w*l)/2;
%% FOS@different base&height values
for k = 1:size(b,2)
for m = 1:size(h,2)
c = (h(m)/2);
density=8000;
V=b(k)*h(m)*l;
mass=density*V;
Mmax = ((w*l^2)/8)*c;
I = (b(k)*h(m)^3)/12;
Bm = (Mmax*c)/I;
Tmax=(3*Vm)/2*(b(k)*h(m));
Stressmax=(Mmax*c)/I;
FOS1(k,m) = (SOM/Tmax);
FOS2(k,m) = (SOM/Stressmax);
end
end
%% Plotting
[B,H]=meshgrid(b,h);
FOS1(k,m) = (SOM/Tmax);
figure;
% "B" and "H" should be used as inputs in the function below, instead of "b" and "h"
[contplt,handle]=contour3(B,H,FOS1');
hold on
title('Beam Design')
xlabel('Base (meters)')
ylabel('Height (meters)')

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!