The program is too slow

1 view (last 30 days)
Dmitry
Dmitry on 26 Jun 2023
Commented: Dyuman Joshi on 27 Jun 2023
I have data that I process with my code, but this data is too big and my program does not work, but for a smaller amount of data, it works well. How do I make the program work for my large amount of data? My data is so big that I can't attach it
My code:
gn_nach(:,1) = tv;
gn_nach(:,2) = dv;
format longG
d_first = 0.3584*(2/3)-0.11458-0.00415;%0.408/sqrt(3)-0.1+0.00172;%0.183908709293856-0.18; % d for first element 0.0522230368891257 0.05149276 0.070729999
d_rest = 0.3584*(2/3); % d for other elements 0.4053
ii1 = 1; %%%%%
ii = 1; %%%%%%
a_0 = 0;
%a_0 = optimvar('a_0',43521); %!!!!!!!!!!!!!!!
general_t_d(:,1) = gn_nach(:,1);
general_t_d(:,2) = gn_nach(:,2);
% Check if first element
if ii1 == 1
d = d_first;
else
d = d_rest;
end
while ii < size(general_t_d,1)
if abs(general_t_d(ii+1,2) - general_t_d(ii,2)) < d
general_t_d(ii+1,:) = [];
else
ii = ii + 1;
d = d_rest; % change d value after first element check
end
end
disp(general_t_d)
c1 = 1;
c2 = exp((-(3/6)^2)/2);
c3 = exp((-(6/6)^2)/2);
c4 = exp((-(9/6)^2)/2);
c5 = exp((-(12/6)^2)/2);
c1 = 1;
c2 = exp((-(3/6)^2)/2);
c3 = exp((-(6/6)^2)/2);
c4 = exp((-(9/6)^2)/2);
c5 = exp((-(12/6)^2)/2);
c6 = exp((-(10/6)^2)/2);
c7 = exp((-(12/6)^2)/2);
%c8 = exp((-(10/6)^2)/2);
%c9 = exp((-(12/6)^2)/2);
k = c1 + 2*c2+ 2*c3 + 2*c4 + 2*c5;% + 2*c6 + 2*c7;
c1 = 1/k;
c2 = c2/k;
c3 = c3/k;
c4 = c4/k;
c5 = c5/k;
%c6 = c6/k;
%c7 = c7/k;
%c8 = c8/k;
%c9 = c9/k;
C_sum = c1 + 2*(c2+ c3 + c4 + c5);% + c6 + c7);
t_v = general_t_d(:,1);
d_v = general_t_d(:,2);
%y_res(1) = t_v(1);
%y_res(2) = t_v(2);
%y_res(3) = t_v(3);
for i = 6:length(t_v) %9
if i == length(t_v)
y_res(length(t_v)) = t_v(length(t_v));
elseif i == length(t_v)-1
y_res(length(t_v)-1) = t_v(length(t_v)-1);
elseif i == length(t_v)-2
y_res(length(t_v)-2) = t_v(length(t_v)-2);
elseif i == length(t_v)-3
y_res(length(t_v)-3) = t_v(length(t_v)-3);
elseif i == length(t_v)-4
y_res(length(t_v)-4) = t_v(length(t_v)-4);
elseif i == length(t_v)-5
y_res(length(t_v)-5) = t_v(length(t_v)-5);
%{
elseif i == length(t_v)-6
y_res(length(t_v)-6) = t_v(length(t_v)-6);
elseif i == length(t_v)-7
y_res(length(t_v)-7) = t_v(length(t_v)-7);
%}
else
y_res(i) = c1.*t_v(i) + c2.*t_v(i+1) + c2.*t_v(i-1)+ c3.*t_v(i+2) + c3.*t_v(i-2) + c4.*t_v(i+3) + c4.*t_v(i-3) + c5.*t_v(i+4) + c5.*t_v(i-4);% + c6.*t_v(i+5) + c6.*t_v(i-5) + c7.*t_v(i+6) + c7.*t_v(i-6); % + c8.*t_v(i+7) + c8.*t_v(i-7) + c9.*t_v(i+8) + c9.*t_v(i-8);
end
end
figure('Color','w')
hold on
plot(d_v,y_res,'-r')
grid
plot(dv,tv,'-b',d_v,t_v,'.black')
  2 Comments
Walter Roberson
Walter Roberson on 27 Jun 2023
preallocate y_res
Dyuman Joshi
Dyuman Joshi on 27 Jun 2023
In addition to Walter's comment (which you should consider as a priority to improve code efficiency) -
Is the variable gn_nach used anywhere else in the code? If not, you should avoid defining it and directly define general_t_d, as copying of large amount of data can slow your code as well.
Additionally, you can also club all the initial if-elseif statements -
l = length(t_v);
if ismember(i, l-5:l)
y_res(i) = t_v(i);
else
y_res(i) = %formula
end

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with Optimization Toolbox 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!