# Why do my code takes long to for small inputs or r

5 visualizaciones (últimos 30 días)
okoth ochola el 27 de Feb. de 2024
Comentada: Dyuman Joshi el 29 de Feb. de 2024
Hi, I have the following attached code. Have posted it here a couple of times, this time I only need to ask a simple question, if I put small values of r, such as the hypothetica classical radius of a electron, r=2.38e-15, The code takes forever to run, my laptop 1i 16 Gb Ram, SSD. Is this supposed to be? apparently this is repeating itself even if I put r=2.38e-10
clear
clc
r= 2.38e-15; %input('Enter the radius of each electron\n');
R= 2; %input('Enter the the radius of the electron path\n');
T= 5; %input('Input the desire period\n');
t= (0:0.01:30)';
n=R/r;
h=6.626e-34;
me=9.109e-31;
acc1=900;
f=4e34;
%A=sqrt((h*f)/(R*acc1*me));
for i=1:1:numel(t)
m1=n*(abs(sin(pi*t(i)/T)));
m2=n*sqrt(abs(sin(pi*t(i)/T)));
if m1<2*cos(pi/6)
A=sqrt((h*f)/(R*acc1*me));
f0(i,1)=fix(A*fix(m2));
else
m=1:1:fix((m1*r)/(2*r*cos(pi/6)));
for j=1:1:numel(m)
f1(j,1)=fix(A*fix(m2*2*sin(acos((m(j)*sqrt(3)/m1)))));
if j==numel(m)
mmax=m(end);
f10=m2*2*sin(acos((mmax*sqrt(3)/m1)));
f11=(fix(f10/2))/2;
f12=f11-fix(f11);
if f12==0
Na=1:1:((fix(f10/2)-2)/2);
for a=1:1:((fix(f10/2)-2)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if a==numel(Na)
f14=2*fix(2*((m1*r*sin(acos((r)/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
f15=fix(A*(fix(m2)+f14+sum(f13(1:a))));
f1(j,1)=f15;
end
end
else
Nb=1:1:((fix(f10/2)-1)/2);
for b=1:1:((fix(f10/2)-1)/2)
f16(b,1)=2*fix(2*((m1*r*sin(acos((2*r*Nb(b))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if b==numel(Nb)
f17=fix(2*((m1*r)-(2*mmax*r*cos(pi/6)))/r);
f18=fix(A*(fix(m2)+f17+sum(f16(1:b))));
f1(j,1)=f18;
end
end
end
% move this code outside the if-else statement #############
f2=(sum(f1(1:j)));
f0(i,1)=f2;
end
end
end
if i==numel(t)
plot(t,f0)
end
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
Aquatris el 27 de Feb. de 2024
At a first glance, when you select small r, your
n=R/r
becomes really large value. As a result, your
m1=n*(abs(sin(pi*t(i)/T)));
becomes really large value. Then you use this large value to create a realy large vector
m=1:1:fix((m1*r)/(2*r*cos(pi/6)));
which requires a lot of RAM.
So yeah I think you either need a super computer for this calculation or change somethings in your code :D
Dyuman Joshi el 29 de Feb. de 2024
Let's see the amount of RAM required (for the max/extreme case) -
r= 2.38e-15; %input('Enter the radius of each electron\n');
R= 2; %input('Enter the the radius of the electron path\n');
T= 5; %input('Input the desire period\n');
t= (0:0.01:30)';
n= R/r;
m1=n*(abs(sin(pi*t/T)));
ans = 0
val = fix((max(m1)*r)/(2*r*cos(pi/6)))
val = 4.8517e+14
%m = 1:1:fix((M*r)/(2*r*cos(pi/6)));
So, defining m will require
fprintf('%f GB of RAM', val*8/(1024)^3)
3614785.473322 GB of RAM
And, unfortunately, you do not have that much ram.

Iniciar sesión para comentar.

### Categorías

Más información sobre MATLAB en Help Center y File Exchange.

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by