Generate code for randomly oriented short fiber RVE?
조회 수: 8 (최근 30일)
이전 댓글 표시
Kindly look into the below code, i am not able to run it, if there is any issue.
function Fiber=Generate_Fiber(x,y,z,L,N)
%input
%x=[x1 x2]: x boundaries of the box
%y=[y1 y2]: y boundaries of the box
%z=[z1 z2]: z boundaries of the box
%L: Length of fibers
%N: Number of fibers
%output
%Fiber: (N,6) matrix of fiber coordinates with (:,1), (:,2) and (:,3) are x,y, and z coordinates of one end
%and (:,4),(:,5) and (:,6) are x,y, and z coordinates of the other end.
for i=1:1:N
while 1
x1=min(x)+(max(x)-min(x))*rand(1);
y1=min(y)+(max(y)-min(y))*rand(1);
z1=min(z)+(max(z)-min(z))*rand(1);
theta=2*pi*rand(1);
v=2*rand(1)-1;
x2=x1+L*sqrt(1-v^2)*cos(theta);
y2=y1+L*sqrt(1-v^2)*sin(theta);
z2=z1+L*v;
if x2<=x(2) && x2>=x(1) && y2<=y(2) && y2>=y(1) && z2<=z(2) && z2>=z(1)
break;
end
end
Fiber(i,1)=x1;
Fiber(i,2)=y1;
Fiber(i,3)=z1;
Fiber(i,4)=x2;
Fiber(i,5)=y2;
Fiber(i,6)=z2;
end
댓글 수: 0
채택된 답변
Constantino Carlos Reyes-Aldasoro
2023년 1월 11일
You are not running your loops correctly. If you ident your code (control-i) you can see better:
for i=1:1:N
while 1
x1=min(x)+(max(x)-min(x))*rand(1);
y1=min(y)+(max(y)-min(y))*rand(1);
z1=min(z)+(max(z)-min(z))*rand(1);
theta=2*pi*rand(1);
v=2*rand(1)-1;
x2=x1+L*sqrt(1-v^2)*cos(theta);
y2=y1+L*sqrt(1-v^2)*sin(theta);
z2=z1+L*v;
if x2<=x(2) && x2>=x(1) && y2<=y(2) && y2>=y(1) && z2<=z(2) && z2>=z(1)
break;
end
end
Fiber(i,1)=x1;
Fiber(i,2)=y1;
Fiber(i,3)=z1;
Fiber(i,4)=x2;
Fiber(i,5)=y2;
Fiber(i,6)=z2;
end
You have a for loop, pressumably to run N times (no need to do 1:1:N, with 1:N will work), then "while 1" does not have a condition to check, it will always enter (https://uk.mathworks.com/help/matlab/ref/while.html) and then an if in which you do not do anything. Finally, I do not think that you need a loop here, you are only adding random numbers to x1,y1,z1, most probably you could do what you want without the for loop.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!