MATLAB Answers

Error using contour (line 48) Z must be at least a 2x2 matrix.

조회 수: 18(최근 30일)
Oscar Hales
Oscar Hales 24 Mar 2020
댓글: Rena Berman 14 May 2020 16:23
Hello i hope im not missing something obvious by putting this here but ive been stuck for hours. I am trying to solve this problem, basically i need to fill a zero matrix with different values for von mises stress and then plot it using a contour plot. I continue to encounter the error in the title, i am also not sure if maybe the problem is in my loop rather than Z.
function Hertz2DContour (P,R,ECM,nu,mu,N)
% Hertz2DContour (P,R,ECM,nu,mu,N)
% Plots contours of von Mises equivalent stress
% P Load per unit length
% R Equivalent radius
% ECM Elastic contact modulus
% mu Friction coefficient
% N Number of points
% (1) Calculate and display semi-contact width, a
a = ((4*P*R)/(pi*ECM))^0.5
% (2) Calculate and display peak pressure, p0
p0 = (2*P/pi*a)
% Create vector of x values from -1.5a to 1.5a
X = linspace (-1.5*a,1.5*a,N);
% Create vector of z values fzom 0 to 3a
Z = ([1:N] - 0.5) * 3 * a / N;
% Create empty matrix for von Mises stress values
sv = zeros (N,N);
% (3) Loop through all X and Z values and calculate sv for each pair
for X=1:N
for Z=1:N
[sx,sz,tzx]=Hertz2DStress(P,R,ECM,nu,N)
sy=nu*(sx+sz);
I1=sx+sy+sz;
I2=sx*sy+sy*sz+sz*sx-(tzx^2);
I3=sx*sy*sz-(sy*tzx^2);
J2=I2-((I1^2)/3);
sv=sqrt(-3*J2);
end
end
% (4) Create a contour plot of the X,Z,sv
[X,Z]=meshgrid(X,Z)
contour(Z,sv,X)
Any help would be greatly appricated, thank you.

  댓글 수: 1

Rena Berman
Rena Berman 14 May 2020 16:23
(Answers Dev) Restored edit

로그인 to comment.

답변(1개)

Star Strider
Star Strider 24 Mar 2020
I have no idea what the arguments to your function are, so I cannot run your code.
Try this:
% Create empty matrix for von Mises stress values
sv = zeros (N,N);
% (3) Loop through all X and Z values and calculate sv for each pair
for X=1:N
for Z=1:N
[sx,sz,tzx]=Hertz2DStress(P,R,ECM,nu,N)
sy=nu*(sx+sz);
I1=sx+sy+sz;
I2=sx*sy+sy*sz+sz*sx-(tzx^2);
I3=sx*sy*sz-(sy*tzx^2);
J2=I2-((I1^2)/3);
sv(X,Z)=sqrt(-3*J2);
end
end
% (4) Create a contour plot of the X,Z,sv
[X,Z]=meshgrid(1:N)
contour(Z,sv,X)
I cannot test this. It should work, although ‘sv(Z,X)’ mighty be correct instead. Experiment!

  댓글 수: 3

Oscar Hales
Oscar Hales 24 Mar 2020
I get the same error message, ive put the other function bellow. Thank you.
function [sx,sz,tzx] = Hertz2DStress (p0,a,mu,x,z)
% [sx,sz,tzx] = str2d(p0,a,mu,x,z)
% Routine to calculate stresses for Hertz 2D cylindrical contact
% p0 Peak contact pressure
% a Semi-contact width
% mu Friction coefficient
% x Horizontal coordinate
% z Vertical coordinate (+ve is down)
% need to normalize x & z w.r.t. semi-contact width a
x = x / a;
z = z / a;
k1 = (1+x).^2 + z^2;
k2 = (1-x).^2 + z^2;
k = sqrt(k2./k1);
s = (pi./k1)./k./sqrt(2*k + (k1 + k2 - 4)./k1);
si = s.*(1-k);
sib = s.*(1+k);
sx = - z/pi * ((1 + 2*x.^2 + 2*z^2).*sib - 2*pi - 3*x.*si);
sx = sx - (2*mu/pi)*((x.^2 - 1 - 1.5*z^2).*si + pi*x);
sx = sx - (2*mu/pi)*((1 - x.^2 - z^2).*x.*sib);
sz = - (z/pi).*(sib - x.*si) - (mu/pi).*si*z^2;
tzx = - (z^2/pi).*si;
tzx = tzx - (mu/pi)*((1 + 2*x.^2 + 2*z^2)*z.*sib - 2*pi*z - 3*x*z.*si);
sx = p0 * sx;
sz = p0 * sz;
tzx = p0 * tzx;
Star Strider
Star Strider 24 Mar 2020
First, it does not appear that you have included the loop that I posted. That appears to create the matrix that contour requires.
Second, without the necessary arguments, I cannot test the code I post. If you provide them, I may be able to solve the problem.
Star Strider
Star Strider 24 Mar 2020
Creating ‘sv’ as a matrix eliminiated the error that contour threw, however ‘sv’ is not changing. This is most readily apparent if you calculate:
cv_lims = min(sv(:))-max(sv(:))
producing:
cv_lims =
0
I am not certain what the problem is.
You will need to find that and fix it, since materials science is far from my areas of expertise.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by