For loop in matlab

조회 수: 1 (최근 30일)
Rana Önem
Rana Önem 2021년 10월 28일
댓글: Rana Önem 2021년 10월 28일
I write a code about a fission process and i want to repeat this 10^6 times i can use for loop right? But i get an error like this: Conversion to logical from sym is not possible.
What is the problem? Can you help me?
Error in Untitled58 (line 34)
if d1<ds
n=10^6;
for i=1:n
R=0.30; %in mm
l=0.1; %in mm
d=0.05; %in mm
x1=rand;
x2=rand;
x3=rand;
x4=rand;
x5=rand;
ksi1=x1;
ksi2=x2;
ksi3=x3;
ksi4=x4;
ksi5=x5;
r=(x1)^1/3* R;
mu=2*x2-1;
fi=2*pi*x3;
ds1=-r*mu+((r^2*mu^2)+(R^2-r^2))^1/2;
ds2=-r*mu-(r^2*mu^2+R^2-r^2)^1/2;
if ds1>0
ds=ds1;
else
ds=ds2;
end
syms s;
syms d1;
s=d1; %d*=d1
if x4<0.3
d=d1;
elseif x4>0.3
d1=(-l*log(exp(x5)));
end
if d1<ds
disp('fission product is not reached to surface of sphere');
else
disp('fission product is reached to surface of sphere');
end
end

채택된 답변

David Hill
David Hill 2021년 10월 28일
Not sure what you are trying to do. You don't use several variables. Why use symbolic?
n=10^6;
R=0.30;
l=0.1;
d=0.05;
for i=1:n
x=rand(1,5);
r=x(1)^1/3*R;
mu=2*x(2)-1;
fi=2*pi*x(3);
a=(r^2*mu^2+R^2-r^2)^1/2;
ds1=-r*mu+a;
ds2=-r*mu-a;
if ds1>0
ds=ds1;
else
ds=ds2;
end
if x(4)<0.3
d1=d;
else
d1=(-l*log(exp(x(5))));
end
end
if d1<ds
disp('fission product is not reached to surface of sphere');
else
disp('fission product is reached to surface of sphere');
end
  댓글 수: 1
Rana Önem
Rana Önem 2021년 10월 28일
There was an algorthim, there are random numbers that affect my calculations and i was trying to repeat this calculations for 10^6 times. The code you write is working so thank you very much. :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by