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