Hi coders! i'm facing a error. Error using contourf Z must be at least a 2x2 matrix.

조회 수: 3 (최근 30일)
clc;
clear all;
%-----Geometry,Number of simulations------%
d = 10;
N = 1000;
a11 = 250*(d/10);
a12= 100*(d/10);
%-----Mesh grid boundary definition--------%
xms=a11/2 - 2*(d);
xmf=a11/2 + 2*(d);
yms=a12/2 - 2*(d);
ymf=a12/2 + 2*(d);
%----------------Grid size----------------%
g = 0.085 ;
xq1=xms:g:xmf;
xq = xq1';
yq1=yms:g:ymf;
yq = yq1';
%-----Mesh gird around the hole----------%
[K1,K2] = meshgrid(xq, yq);
a= size(xq1,2);
b= size(yq1,2);
for k = 0:b
for j=0:a
e=((k*g)-2*d)^2+((j*g)-2*d)^2-(.5*d)^2;
if e<0
K1(j,k)= NaN;
K2(j,k)= NaN;
end
end
end
zz1=0;
zz2= 0;
%----Importing files from abaqus---------%
for i =1:N
fx =[ 'd:/babar/Abaqus/S2000/aax' num2str(i) '.txt'];
fy =[ 'd:/babar/Abaqus/S2000/aay' num2str(i) '.txt'];
fu =[ 'd:/babar/Abaqus/S2000/aau1' num2str(i) '.txt'];
fv =[ 'd:/babar/Abaqus/S2000/aau2' num2str(i) '.txt'];
x = textfile(fx);
y = textfile(fy);
z1 = textfile(fu);
z2 = textfile(fv);
U1 = griddata(x,y,z1,K1,K2);
U2 = griddata(x,y,z2,K1,K2);
zz1 = U1+zz1;
zz2 = U2+zz2;
i ;
end
%--------Averaging the displacements-------%
zpk1 = (zz1)./N;
zpk2 = (zz2)./N;
xlswrite('zpk1.xlsx',zpk1)
xlswrite('zpk2.xlsx',zpk2)
%------Creating a strain matrix-----------%
E = zeros(2*(size(K1,1)-1),2*(size(K1,1)-1))
for j = 1: (size(K1,1)-1)
for i = 1: (size(K1,1)-1)
v = [zpk1(j,i);zpk2(j,i);zpk1(j,i+1);zpk2(j,i+1);zpk1(j+1,i+1);zpk2(j+1,i+1);zpk1(j+1,i);zpk2(j+1,i)];
e1= strain1(v);
e2= strain2(v);
e3= strain3(v);
e4= strain4(v);
E((2*j-1),(2*i-1)) = e1(1);
E((2*j-1),2*i) = e2(1);
E(2*j,(2*i-1)) = e4(1);
E(2*j,2*i) = e3(1);
end
end
su = round(4*d/g)+1 ;
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
st = (p+r)./(1.5*(10)^(-4));
m = g*(size(r)-1);
H = 0:g:(m);
h = H./10;
writematrix(st)
type 'st.txt'
writematrix(h)
type 'h.txt'
plot(h,st)
xlim([0 1.5])
xlabel("X2/d",'fontsize',14)
ylabel("Normalised Strain along vertical path", 'fontsize',13)
legend("d/w = 0.5")
saveas(gcf,'plot.png')
contourf(st);
colorbar;
saveas(gcf, 'contour.png');
function E1 = strain1(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=-1/sqrt(3);eta1=-1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain2(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=1/sqrt(3);eta1=-1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain3(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=1/sqrt(3);eta1=1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain4(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 4
x1=-1/sqrt(3);eta1=1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function B21 = B2()
B21 = [0.05/0.0025 0 0 0;0 0.05/0.0025 0 0;0 0 0.05/0.0025 0;0 0 0 0.05/0.0025];
end
function B31 = B3(x1,eta1)
B31 = [-(1-eta1)*0.25 0 (1-eta1)*0.25 0 (1+eta1)*0.25 0 -(1+eta1)*0.25 0;...
-(1-x1)*0.25 0 -(1+x1)*0.25 0 (1+x1)*0.25 0 (1-x1)*0.25 0;...
0 -(1-eta1)*0.25 0 (1-eta1)*0.25 0 (1+eta1)*0.25 0 -(1+eta1)*0.25;...
0 -(1-x1)*0.25 0 -(1+x1)*0.25 0 (1+x1)*0.25 0 (1-x1)*0.25];
end
function value = textfile(filename)
fileID = fopen(filename, 'r');
if fileID == -1
error('Failed to open file: %s', filename);clc;
clear all;
%-----Geometry,Number of simulations------%
d = 10;
N = 1000;
a11 = 250*(d/10);
a12= 100*(d/10);
%-----Mesh grid boundary definition--------%
xms=a11/2 - 2*(d);
xmf=a11/2 + 2*(d);
yms=a12/2 - 2*(d);
ymf=a12/2 + 2*(d);
%----------------Grid size----------------%
g = 0.085 ;
xq1=xms:g:xmf;
xq = xq1';
yq1=yms:g:ymf;
yq = yq1';
%-----Mesh gird around the hole----------%
[K1,K2] = meshgrid(xq, yq);
a= size(xq1,2);
b= size(yq1,2);
for k = 0:b
for j=0:a
e=((k*g)-2*d)^2+((j*g)-2*d)^2-(.5*d)^2;
if e<0
K1(j,k)= NaN;
K2(j,k)= NaN;
end
end
end
zz1=0;
zz2= 0;
%----Importing files from abaqus---------%
for i =1:N
fx =[ 'd:/babar/Abaqus/S2000/aax' num2str(i) '.txt'];
fy =[ 'd:/babar/Abaqus/S2000/aay' num2str(i) '.txt'];
fu =[ 'd:/babar/Abaqus/S2000/aau1' num2str(i) '.txt'];
fv =[ 'd:/babar/Abaqus/S2000/aau2' num2str(i) '.txt'];
x = textfile(fx);
y = textfile(fy);
z1 = textfile(fu);
z2 = textfile(fv);
U1 = griddata(x,y,z1,K1,K2);
U2 = griddata(x,y,z2,K1,K2);
zz1 = U1+zz1;
zz2 = U2+zz2;
i ;
end
%--------Averaging the displacements-------%
zpk1 = (zz1)./N;
zpk2 = (zz2)./N;
xlswrite('zpk1.xlsx',zpk1)
xlswrite('zpk2.xlsx',zpk2)
%------Creating a strain matrix-----------%
E = zeros(2*(size(K1,1)-1),2*(size(K1,1)-1))
for j = 1: (size(K1,1)-1)
for i = 1: (size(K1,1)-1)
v = [zpk1(j,i);zpk2(j,i);zpk1(j,i+1);zpk2(j,i+1);zpk1(j+1,i+1);zpk2(j+1,i+1);zpk1(j+1,i);zpk2(j+1,i)];
e1= strain1(v);
e2= strain2(v);
e3= strain3(v);
e4= strain4(v);
E((2*j-1),(2*i-1)) = e1(1);
E((2*j-1),2*i) = e2(1);
E(2*j,(2*i-1)) = e4(1);
E(2*j,2*i) = e3(1);
end
end
su = round(4*d/g)+1 ;
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
st = (p+r)./(1.5*(10)^(-4));
m = g*(size(r)-1);
H = 0:g:(m);
h = H./10;
writematrix(st)
type 'st.txt'
writematrix(h)
type 'h.txt'
plot(h,st)
xlim([0 1.5])
xlabel("X2/d",'fontsize',14)
ylabel("Normalised Strain along vertical path", 'fontsize',13)
legend("d/w = 0.5")
saveas(gcf,'plot.png')
contourf(st);
colorbar;
saveas(gcf, 'contour.png');
function E1 = strain1(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=-1/sqrt(3);eta1=-1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain2(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=1/sqrt(3);eta1=-1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain3(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 1
x1=1/sqrt(3);eta1=1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function E1 = strain4(v)
B1 = [1 0 0 0;0 0 0 1;0 1 1 0];
%% for node 4
x1=-1/sqrt(3);eta1=1/sqrt(3);
B21 = B2();
B31 = B3(x1,eta1);
B12 = B1*B21;
B = B12*B31;
E1 =B*v;
end
function B21 = B2()
B21 = [0.05/0.0025 0 0 0;0 0.05/0.0025 0 0;0 0 0.05/0.0025 0;0 0 0 0.05/0.0025];
end
function B31 = B3(x1,eta1)
B31 = [-(1-eta1)*0.25 0 (1-eta1)*0.25 0 (1+eta1)*0.25 0 -(1+eta1)*0.25 0;...
-(1-x1)*0.25 0 -(1+x1)*0.25 0 (1+x1)*0.25 0 (1-x1)*0.25 0;...
0 -(1-eta1)*0.25 0 (1-eta1)*0.25 0 (1+eta1)*0.25 0 -(1+eta1)*0.25;...
0 -(1-x1)*0.25 0 -(1+x1)*0.25 0 (1+x1)*0.25 0 (1-x1)*0.25];
end
function value = textfile(filename)
fileID = fopen(filename, 'r');
if fileID == -1
error('Failed to open file: %s', filename);
end
value = fscanf(fileID, '%f');
fclose(fileID);
end
end
value = fscanf(fileID, '%f');
fclose(fileID);
end
  댓글 수: 2
Torsten
Torsten 2024년 8월 19일
fx =[ 'd:/babar/Abaqus/S2000/aax' num2str(i) '.txt'];
fy =[ 'd:/babar/Abaqus/S2000/aay' num2str(i) '.txt'];
fu =[ 'd:/babar/Abaqus/S2000/aau1' num2str(i) '.txt'];
fv =[ 'd:/babar/Abaqus/S2000/aau2' num2str(i) '.txt'];
We don't have access to your computer :-)
Voss
Voss 2024년 8월 19일
@Mohd Babar: All of your functions are defined two times, once in the main script and once inside the textfile function - it looks like you accidentally copied your script into the textfile function.

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2024년 8월 19일
d and g are both scalars
d = 10;
% ...
g = 0.085 ;
so su is a scalar
su = round(4*d/g)+1 ;
so r and p are both column vectors
r = E((((su-1)- round(d/g)):-1:1),(su-1));
p = E((((su-1)- round(d/g)):-1:1),(su));
so st is a column vector
st = (p+r)./(1.5*(10)^(-4));
That is, su is of size n-by-1 for some n, but contourf requires the size of its argument to be p-by-q for some p>1, q>1. That's the reason for the error.

카테고리

Help CenterFile Exchange에서 Genomics and Next Generation Sequencing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by