MATLAB Answers

How to make my code run faster?

1 view (last 30 days)
Sarah Kandeel
Sarah Kandeel on 25 Feb 2018
Commented: Sarah Kandeel on 27 Feb 2018
Hello, I just wanted to know, how do I make this code run faster?
tic
for z_0=0.1:0.05:0.8
for R_0=0.01:0.05:1
for N=0:1:5
load('am_z.mat')
syms D_guess; x_h=1; x_w=3.9/7.4; theta0= 0; theta1= 1; u= 1; AM=0.968740; BM=0.969441; alpha_h_Ih=0.25; alpha_h_xh=0.25; alpha_h_xw =0.25; alpha_h_theta=0.25; alpha_h_Yh= 0.25; alpha_w_Iw= 1/3; alpha_w_xh=1/3; alpha_w_xw= 1/3; alpha_h_am=1- alpha_h_Ih- alpha_h_xh; alpha_h_bm=1- alpha_h_Yh- alpha_h_xh; Y_h=1; Y_w=1; %z_0=0.5; R_0=0.5; N=1;
%% Defining the functions t_w=(Y_w-D_guess)*(1-exp(-(alpha_w_xh*u*sqrt(x_h))/(alpha_w_Iw)));
I_h=Y_h+D_guess+t_w;
p =z_0*(1-sqrt(z));
u_h0= alpha_h_Ih*log(I_h)+alpha_h_xh*log(x_h)+alpha_h_xw*sqrt(x_w)+alpha_h_theta*sqrt(theta0); %u_h when theta=0
u_h1= alpha_h_Ih*log(Y_h+D_guess)+alpha_h_xh*log(x_h)+alpha_h_xw*sqrt(x_w)+alpha_h_theta*sqrt(theta1); %u_h when theta=1
v_h=alpha_h_Ih*log(Y_h+D_guess)+alpha_h_xh*log(x_h)+ alpha_h_am*log(am);
R = R_0+alpha_h_Yh*log(Y_h)+alpha_h_xh*log(x_h)+alpha_h_bm*log(BM);
%%%Seperation of the equation into three parts:
%% %Part 2: for j=1:1:35; %A loop for 'j' to create the first integration and summation that contains variable 'j'
kappa_computed(j)= alpha_h_xw*sqrt(x_w)-alpha_h_am.*log(am(j))-alpha_h_Ih*(log(Y_h+D_guess)-log(I_h));
fun1= @(kappa_computed)(u_h0-kappa_computed)*((kappa_computed^((N-2)/2))*exp(-kappa_computed/2))/(2^(N/2)...
*gamma(N/2))*Pram(j); %The integration of the first function contiaining k
fun2= @(kappa_computed)(v_h(j))*((kappa_computed^((N-2)/2))*exp(-kappa_computed/2))/(2^(N/2)...
*gamma(N/2))*Pram(j); %The integration of the second function contiaining k
integral_1= int(fun1,0,kappa_computed(j)); %Computing the integration of the function fun1
integral_2= int(fun2,kappa_computed(j),Inf);%Computing the integration of the function fun2
i_n_t1(j)=integral_1; %Creating an array called 'i_n_t1' to store the value of the first function 'integral_1'
i_n_t2(j)=integral_2; %Creating an array called 'i_n_t2' to store the value of the first function 'integral_2'
j=j+1; %Incrementing the value of 'j'
end
num1= sum(i_n_t1); %Sum of all the variable in the array 'i_n_t1' saved as 'num1'
num2= sum(i_n_t2); %Sum of all the variable in the array 'i_n_t2' saved as 'num2'
for i=1:1:33;
pt_1=(1-p(i))*u_h1*Prz(i); %First function that contains varaiable 'i'
sum_p1(i)= pt_1; %Creating an array called 'sum_p1' to store the value of the first function 'pt_1'
pt_2=num1*p(i)*Prz(i); %Second function that contains varaiable 'i' that is multiplied by the
%previous function 'num1'
pt_3=num2*p(i)*Prz(i); %Second function that contains varaiable 'i' that is multiplied by the
%previous variable 'num2
sum_p2(i)=pt_2; %Creating an array called 'sum_p2' to store the value of the first function 'pt_2'
sum_p3(i)=pt_3; %Creating an array called 'sum_p3' to store the value of the first function 'pt_3'
i=i+1; %Incrementing the value of 'i'
end
sum1=sum (sum_p1); %Sum of all the variable in the array 'sum_p1'
sum2=sum(sum_p2); %Sum of all the variable in the array 'sum_p2' saved as 'sum2'
sum3=sum(sum_p3); %Sum of all the variable in the array 'sum_p3' saved as 'sum3'
%% EU_h= sum1+sum2+sum3;
eqn=EU_h==R; D_computed=solve(eqn,D_guess)
end
end
end
toc

  0 Comments

Sign in to comment.

Accepted Answer

mizuki
mizuki on 25 Feb 2018
First, use code profiler to identify which part of your code takes much time to execute. Then, consider vectorization/using parallel computing (replace for with parfor using Parallel Computing Toolbox).

  1 Comment

Sarah Kandeel
Sarah Kandeel on 27 Feb 2018
Thanks, I will try that.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by