How to increase speed of this code

9 views (last 30 days)
ASHA RANI
ASHA RANI on 3 Jul 2021
Answered: VBBV on 12 Feb 2025 at 1:12
clc,clear,close all
format short
syms AX0
e=0.2;
OM=[1:100:20000];
ET=sqrt(-1);
A11 = 17.8*10^10;
A33 = 18.43*10^10;
A13 = 7.59*10^10;
%A56 = 1.89*10^10;
A55 = 4.357*10^10;
%A66 = 4.42*10^10;
A56=A55;
A66=A55;
AL15 = 37;
AL33 = 1.33;
AL31 = 12;
AL35 = 0.23;
GM11= 85.2;
GM33= 28.7;
RO=1.74*10^3;
NON=RO*(e^2).*(OM.^2);
NA55=A55-NON;
NA33=A33-NON;
NA11=A11-NON;
NA66=A66-NON;
AP2=NA11./NA55;
AP3=A33./NA55;
AP4=NA66./NA33;
AP44=A33./NA33;
AP24=(A13+A56);
AP25=AL15+AL31;
B1OKS=AP2-AP3*AX0;
B2OKS=AP4-AP44*AX0;
B3OKS=GM11/(AL33*AL33-GM33);
B1OK=sqrt(B1OKS);
B2OK=sqrt(B2OKS);
B3OK=sqrt(B3OKS);
APT1=AL15-AL33.*B1OKS;
APT2=AL15-AL33.*B2OKS;
APT3=AL15-AL33.*B3OKS;
AZT1=ET*AP25*B1OK./APT1;
AZT2=ET*AP25*B2OK./APT2;
AZT3=ET*AP25*B3OK./APT3;
AXT1=AP24.*AZT1./AP25;
AXT2=AP24.*AZT2./AP25;
AXT3=AP24.*AZT3./AP25;
A1=ET*A13-B1OK.*AZT1*A33-ET*AXT1*AL35+B1OK.*AXT1*AL33;
A2=ET*A13-B2OK.*AZT2*A33-ET*AXT2*AL35+B2OK.*AXT2*AL33;
A3=ET*A13-B3OK.*AZT3*A33-ET*AXT3*AL35+B3OK.*AXT3*AL33;
B1=ET*AZT1*A56-B1OK*A55-ET*AL31*AXT1+B1OK.*AXT1*AL35;
B2=ET*AZT2*A56-B2OK*A55-ET*AL31*AXT2+B2OK.*AXT2*AL35;
B3=ET*AZT3*A56-B3OK*A55-ET*AL31*AXT3+B3OK.*AXT3*AL35;
% CHARGE FREE
C1=ET*AL15-B1OK.*AZT1*AL33-B1OK.*AXT1*GM33;
C2=ET*AL15-B2OK.*AZT2*AL33-B2OK.*AXT2*GM33;
C3=ET*AL15-B3OK.*AZT3*AL33-B3OK.*AXT3*GM33;
AU= A1.*B2.*C3-A1.*B3.*C2-A2.*B1.*C3+A2.*B3.*C1+A3.*B1.*C2-A3.*B2.*C1;
W =[];
for i=1:numel(OM)
AUU=AU(:,i);
r= double(solve(AUU==0,AX0));
r1=(sqrt((A33*r)./RO));
W = [W,r1];
end
SP1=W(1,:);
SP2=W(2,:);
SP3=W(3,:);
SP4=W(4,:);
How I increase speed of runing of above code. It takes long time to run .

Answers (3)

Yongjian Feng
Yongjian Feng on 3 Jul 2021
Hello Asha,
matlab provides profile for this purpose. (https://www.mathworks.com/help/matlab/ref/profile.html).
Try it please.
Thanks,
Yongjian
  1 Comment
ASHA RANI
ASHA RANI on 3 Jul 2021
I have MATLAB2017b version. I also tried this but not able to understand where I will do changes to increase speed.

Sign in to comment.


Heran Wang
Heran Wang on 3 Jul 2021
I recommend the higher version of MATLAB.
The higher the vesion is, the faster the caculation will be.
For example, the MATLAB 2021a will two times faster than MATLAB 2017b.
Try it ~

VBBV
VBBV on 12 Feb 2025 at 1:12
@ASHA RANI one way to speed up code is increase the step size or increment in the OM array. The variables used in the equations to solve them also contain big numbers which can consume more memory during execution. You could also vectorize the for loop if needed.
clc,clear,close all
format short
tic
syms AX0
e=0.2;
OM=[1:500:20000]; % change the step or increment in this array
%^^^^
ET=sqrt(-1);
A11 = 17.8*10^10;
A33 = 18.43*10^10;
A13 = 7.59*10^10;
%A56 = 1.89*10^10;
A55 = 4.357*10^10;
%A66 = 4.42*10^10;
A56=A55;
A66=A55;
AL15 = 37;
AL33 = 1.33;
AL31 = 12;
AL35 = 0.23;
GM11= 85.2;
GM33= 28.7;
RO=1.74*10^3;
NON=RO*(e^2).*(OM.^2);
NA55=A55-NON;
NA33=A33-NON;
NA11=A11-NON;
NA66=A66-NON;
AP2=NA11./NA55;
AP3=A33./NA55;
AP4=NA66./NA33;
AP44=A33./NA33;
AP24=(A13+A56);
AP25=AL15+AL31;
B1OKS=AP2-AP3*AX0;
B2OKS=AP4-AP44*AX0;
B3OKS=GM11/(AL33*AL33-GM33);
B1OK=sqrt(B1OKS);
B2OK=sqrt(B2OKS);
B3OK=sqrt(B3OKS);
APT1=AL15-AL33.*B1OKS;
APT2=AL15-AL33.*B2OKS;
APT3=AL15-AL33.*B3OKS;
AZT1=ET*AP25*B1OK./APT1;
AZT2=ET*AP25*B2OK./APT2;
AZT3=ET*AP25*B3OK./APT3;
AXT1=AP24.*AZT1./AP25;
AXT2=AP24.*AZT2./AP25;
AXT3=AP24.*AZT3./AP25;
A1=ET*A13-B1OK.*AZT1*A33-ET*AXT1*AL35+B1OK.*AXT1*AL33;
A2=ET*A13-B2OK.*AZT2*A33-ET*AXT2*AL35+B2OK.*AXT2*AL33;
A3=ET*A13-B3OK.*AZT3*A33-ET*AXT3*AL35+B3OK.*AXT3*AL33;
B1=ET*AZT1*A56-B1OK*A55-ET*AL31*AXT1+B1OK.*AXT1*AL35;
B2=ET*AZT2*A56-B2OK*A55-ET*AL31*AXT2+B2OK.*AXT2*AL35;
B3=ET*AZT3*A56-B3OK*A55-ET*AL31*AXT3+B3OK.*AXT3*AL35;
% CHARGE FREE
C1=ET*AL15-B1OK.*AZT1*AL33-B1OK.*AXT1*GM33;
C2=ET*AL15-B2OK.*AZT2*AL33-B2OK.*AXT2*GM33;
C3=ET*AL15-B3OK.*AZT3*AL33-B3OK.*AXT3*GM33;
AU= A1.*B2.*C3-A1.*B3.*C2-A2.*B1.*C3+A2.*B3.*C1+A3.*B1.*C2-A3.*B2.*C1;
W =[];
for i=1:numel(OM)
AUU=AU(:,i);
r= double(solve(AUU==0,AX0));
r1=(sqrt((A33*r)./RO));
W = [W,r1];
end
SP1=W(1,:);
SP2=W(2,:);
SP3=W(3,:);
SP4=W(4,:);
toc
Elapsed time is 48.924230 seconds.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!