How can I accelerate my Matlab program

2 次查看(过去 30 天)
Erkan
Erkan 2022-5-14
评论: Erkan 2022-5-17
Hi everybody, the working of the my matlab program durate very long, and i want to short the operation time. I write the outline of the my program below.
f=(400:1000);
q=5;
m=5;
rk=4096;
f1=length(f);
for a=1:f1
for b=1:q
for c=1:m
for d=1:rk
end
end
end
end
Also, how can be shorted the operation time of the any matlab program?
  2 个评论
Voss
Voss 2022-5-14
Here's an outline of a solution:
f=(400:1000);
q=5;
m=5;
rk=4096;
f1=length(f);
% use vectorized operations instead of for loops
It's possible that the operations inside the for loops can be vectorized, which will very likely speed up the program. However, without knowing what those operations are, it's not possible to say the extent to which vectorization is possible or how to do it. Please share the full code.
Erkan
Erkan 2022-5-17
thanks for your advise. I tried to vectorize the codes that being one part of my main program, but i cannot do because i know a few on the vectorization. I written those codes below. Also, in those codes, there is the coupled differential equations (4th Runge-Kutta method).
clc, clear all, close all
q=1.6e-19;
Beta=1e-4;
Gama=0.025;
Alfa_gs=4.6e-14;
Alfa_es=9.2e-14;
h=6.62e-34;
Lamda=1.55e-4;
Epsilon=0;
R1=0.95;
R2=0.05;
Kb=(1.38e-23)/q;
T=300;
Aw=1.35e10;
Cw=5e-9;
Ae=1.5e10;
Ce=9e-8;
ue=4;
uw=10;
ug=2;
Ees=0.84;
Egs=0.792;
Ewl=1.05;
twr=500e-12;
ter=500e-12;
tgr=1.2e-9;
tp=8.92e-12;
Length=2.45e-1;
Width=12e-4;
Thickness=2e-7;
Va=(Length*Width*Thickness*3);
N0=6e16;
Nr=3.27;
c=3e10;
Vg=c/Nr;
Loss_m=((log(1/(R1*R2)))/(2*Length*Nr));
Loss_i=6;
Loss=Loss_m+Loss_i;
Pcon=((Vg*h*Va*Loss_m*c)/(Lamda*Gama));
f=1000e6;
I=(0:200)*1e-3;
loop1=length(I);
t=linspace(0,1/f,3000);
step=(1/f)/3000;
loop2=length(t);
power1=zeros(1,loop1);
power2=zeros(1,loop1);
power3=zeros(1,loop1);
power4=zeros(1,loop1);
power5=zeros(1,loop1);
power6=zeros(1,loop1);
Gain_es=zeros(1,loop1);
Gain_gs=zeros(1,loop1);
Gain_total=zeros(1,loop1);
Nw=zeros(1,loop2);
Ng=zeros(1,loop2);
Ne=zeros(1,loop2);
Se=zeros(1,loop2);
Sg=zeros(1,loop2);
for j=1:loop1
% starting the 4th Runge-Kutta method at this for loop
for i=1:loop2
fes(i)=1-(Ne(i)/(4*N0));
fgs(i)=1-(Ng(i)/(2*N0));
twe(i)=1/(Aw+Cw*Nw(i));
teg(i)=1/(Ae+Ce*Nw(i));
twg(i)=twe(i);
tge(i)=(ug/ue)*teg(i)*exp((Ees-Egs)/(Kb*T));
tew(i)=(ue/uw)*twe(i)*exp((Ewl-Ees)/(Kb*T));
fNw=@(t,Nw,Ne) I(j)/(q*Va)-fes(i)*(Nw/twe(i))-fgs(i)*(Nw/twg(i))-(Nw/twr)+(Ne/tew(i));
fNe=@(t,Nw,Ne,Ng,Se) fes(i)*(Nw/twe(i))+fes(i)*(Ng/tge(i))-fgs(i)*(Ne/teg(i))-(Ne/tew(i))-(Ne/ter)-Vg*Gama*Alfa_es*((Ne/2)-N0)*(Se/(1+(Epsilon*Se)));
fNg=@(t,Nw,Ne,Ng,Sg) fgs(i)*(Nw/twg(i))+fgs(i)*(Ne/teg(i))-fes(i)*(Ng/tge(i))-(Ng/tgr)-Vg*Gama*Alfa_gs*(Ng-N0)*(Sg/(1+(Epsilon*Sg)));
fSe=@(t,Ne,Se) Vg*Gama*Alfa_es*((Ne/2)-N0)*(Se/(1+(Epsilon*Se)))+(Gama*Beta*(Ne/ter))-(Se/tp);
fSg=@(t,Ng,Sg) Vg*Gama*Alfa_gs*(Ng-N0)*(Sg/(1+(Epsilon*Sg)))+(Gama*Beta*(Ng/tgr))-(Sg/tp);
k1=fNw(t(i),Nw(i),Ne(i));
m1=fNe(t(i),Nw(i),Ne(i),Ng(i),Se(i));
n1=fNg(t(i),Nw(i),Ne(i),Ng(i),Sg(i));
p1=fSe(t(i),Ne(i),Se(i));
r1=fSg(t(i),Ng(i),Sg(i));
k2=fNw(t(i)+step/2,Nw(i)+step/2*k1,Ne(i)+step/2*m1);
m2=fNe(t(i)+step/2,Nw(i)+step/2*k1,Ne(i)+step/2*m1,Ng(i)+step/2*n1,Se(i)+step/2*p1);
n2=fNg(t(i)+step/2,Nw(i)+step/2*k1,Ne(i)+step/2*m1,Ng(i)+step/2*n1,Sg(i)+step/2*r1);
p2=fSe(t(i)+step/2,Ne(i)+step/2*m1,Se(i)+step/2*p1);
r2=fSg(t(i)+step/2,Ng(i)+step/2*n1,Sg(i)+step/2*r1);
k3=fNw(t(i)+step/2,Nw(i)+step/2*k2,Ne(i)+step/2*m2);
m3=fNe(t(i)+step/2,Nw(i)+step/2*k2,Ne(i)+step/2*m2,Ng(i)+step/2*n2,Se(i)+step/2*p2);
n3=fNg(t(i)+step/2,Nw(i)+step/2*k2,Ne(i)+step/2*m2,Ng(i)+step/2*n2,Sg(i)+step/2*r2);
p3=fSe(t(i)+step/2,Ne(i)+step/2*m2,Se(i)+step/2*p2);
r3=fSg(t(i)+step/2,Ng(i)+step/2*n2,Sg(i)+step/2*r2);
k4=fNw(t(i)+step,Nw(i)+step*k3,Ne(i)+step*m3);
m4=fNe(t(i)+step,Nw(i)+step*k3,Ne(i)+step*m3,Ng(i)+step*n3,Se(i)+step*p3);
n4=fNg(t(i)+step,Nw(i)+step*k3,Ne(i)+step*m3,Ng(i)+step*n3,Sg(i)+step*r3);
p4=fSe(t(i)+step,Ne(i)+step*m3,Se(i)+step*p3);
r4=fSg(t(i)+step,Ng(i)+step*n3,Sg(i)+step*r3);
Nw(i+1)=Nw(i)+step/6*(k1+2*k2+2*k3+k4);
Ne(i+1)=Ne(i)+step/6*(m1+2*m2+2*m3+m4);
Ng(i+1)=Ng(i)+step/6*(n1+2*n2+2*n3+n4);
Se(i+1)=Se(i)+step/6*(p1+2*p2+2*p3+p4);
Sg(i+1)=Sg(i)+step/6*(r1+2*r2+2*r3+r4);
end
Gain_e=Gama*Alfa_es*((Ne/2)-N0);
Gain_g=Gama*Alfa_gs*(Ng-N0);
Gain=(Gain_e+Gain_g);
Gain_es(j)=mean(Gain_e);
Gain_gs(j)=mean(Gain_g);
Gain_total(j)=mean(Gain);
power1(j)=mean(Nw);
power2(j)=mean(Ne);
power3(j)=mean(Ng);
power4(j)=mean(Se);
power5(j)=mean(Sg);
power6(j)=mean(Se+Sg);
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by