# why is it takes so much to calculate this function ?

2 views (last 30 days)
tomer polsky on 26 Dec 2019
Answered: Roshni Garnayak on 8 Jan 2020
hi I have this code that describes soltion of ODE . I am using Euler method for solveing first order first degree of ODE .
the problem is that for some reason ,this function takes a lot of time . and when I am ploting the resluts . it gets stuck why is that ?
%%% values for calcalution of I1_desire
U=values_of_circuit.U; %% Vin
C2=values_of_circuit.C2; %% Capacitance of the output capsitor
C1=values_of_circuit.C1; %% Capacitance of the first capsitor
L1=values_of_circuit.L1; %% Inductnce of inductor 1
L2=values_of_circuit.L2; %% Inductnce of inductor 2
R_C2=values_of_circuit.R_C2; %% Resistence of the C2
R_C1=values_of_circuit.R_C1; %% Resistence of the C1
R1=values_of_circuit.R1; %% Resistence of the L1 (also this parmater is used for the half idial simulation )
R2=values_of_circuit.R2; %% Resistence of the L2
R_diode=values_of_circuit.R_diode; %% Resistence of diode
V_diode=values_of_circuit.V_diode; %% Forward volatge across the diode
alpha_1=Duty_Cycle_Parmeters.alpha_desire; %% alpha desire for the desried output voltage
t_ode=Run_Time_Values.t_ode; %% end time of the running simulation
A_a=Basic_Matrix.A_a;
A_b=Basic_Matrix.A_b;
B_a=Basic_Matrix.B_a;
B_b=Basic_Matrix.B_b;
C_a=Basic_Matrix.C_a;
C_b=Basic_Matrix.C_b;
I_1_desired=0.56187;
% I_1_desired=x_t_symbolic(3);
I_1_desired=double(subs(I_1_desired));
Rohav_H=1; %% width of Histers window
% I1_desired_min=(I_1_desired)-(Rohav_H/2);
% I1_desired_max=(I_1_desired)+(Rohav_H/2);
I1_desired_min=I_1_desired-0;
I1_desired_max=I_1_desired+0;
t_run_B_C=t_ode; %% run time for the function
t_delta_B_C=1000e-9; %% step size of the euler's therom
number_of_samples_B_C=round(t_run_B_C/t_delta_B_C); %%calculation of number of samples
S_switch=zeros(1,number_of_samples_B_C);%%S_switch= array of zeros with size of number_of_samples_B_C
x=zeros(4,number_of_samples_B_C); %%x=array of zeros with size of number_of_samples_B_C
x(:,1)=[ 0 0 0 0]';%% starting condtion
V_out_Boundery_Control=zeros(1,number_of_samples_B_C); %%V_out_Boundery_Control=array of zeros with size of number_of_samples_B_C
j=0;
num_of_samples_for_1_delay=round((1/ 892e3)/t_delta_B_C);
S_switch=zeros(1,round(number_of_samples_B_C/num_of_samples_for_1_delay));
test_counter=zeros(1,round(number_of_samples_B_C/num_of_samples_for_1_delay));
for i=1:number_of_samples_B_C-1
if(rem(i,num_of_samples_for_1_delay)==0)
j=j+1;
test_counter(j)=1;
if (x(3,j)<=I1_desired_min)
Switch_State=1;
elseif(x(3,j)>I1_desired_max)
Switch_State=0;
else
end
S_switch(j)= Switch_State;
A_total=A_a* Switch_State+A_b* not(Switch_State);
B_total=B_a* Switch_State+B_b*not(Switch_State);
C_total=C_a* Switch_State+C_b*not(Switch_State);
x(:,i+1)=x(:,i)+t_delta_B_C*( A_total*x(:,i)+B_total*U);
S_switch(i+1)=0.5;
V_out_Boundery_Control(:,i+1)=C_total*x(:,i+1);
end
t_testing=linspace(0,t_run_B_C, number_of_samples_B_C);
size_t=size(t_testing);
size_x=size(x);
B_C_with_delay_Eulers_Results.x_BC_with_delay=x;
B_C_with_delay_Eulers_Results.t_with_delay=t_testing;
B_C_with_delay_Eulers_Results.V_R_BC_with_delay=V_out_Boundery_Control;
B_C_with_delay_Eulers_Results.Switch_BC_with_delay=S_switch;
B_C_with_delay_Eulers_Results.test=test_counter;
and if I plot the resluts ,the plot takes a lot of time to be ready , and also works very slow when I want to zoom in .
plot(B_C_with_delay_Eulers_Results.t_with_delay,B_C_with_delay_Eulers_Results.x_BC_with_delay(3,:),'-- red d');
hold on;
plot(B_C_with_delay_Eulers_Results.t_with_delay,0.5*B_C_with_delay_Eulers_Results.Switch_BC_with_delay,'-- blue d');
hold on;
plot(B_C_with_delay_Eulers_Results.t_with_delay,0.5*B_C_with_delay_Eulers_Results.test,'-- magenta d');
hold on;
plot(B_C_with_delay_Eulers_Results.t_with_delay,1*I_1_desired*ones(size(B_C_with_delay_Eulers_Results.t_with_delay)),' black ','linewidth',3); %% plot--> of boundery limit
legend('I_1','Switch with delay ','test of the delay','boundery limit ');
John D'Errico on 26 Dec 2019
There is a reason why they only teach Euler's method to beginning students, but never recommend it as a method for later use. As methods go, it is usually crapola.

Roshni Garnayak on 8 Jan 2020
You can use the MATLAB Profiler to check which part of the code is consuming more time to run. Once you figure that out, you can optimize that part of the code.
To know more about the MATLAB Profiler, refer to the following link: