the script works normally for few data but if it accepts a table with a lot of data it takes too long time (>1hr........give a result but in a long time)
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
the script works normally for few data but if it accepts a table with a lot of data it takes too long time (>1hr........give a result but in a long time)
clear;clc;close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pref=input('Please enter file prefix: ','s');
    fn=[pref '.mhk'];
    fr=fopen(fn,'r');
    k=0;
error = false;
t = fgetl(fr);
XTH = [];
Yed = [];
while ~feof(fr)
    t = fgetl(fr);
    [temp,~]=sscanf(t,'%f');
    if ~error
        k = k + 1;
        XTH(k) = temp(1);
        Yed(k) = temp(2);
    end
end
x = [XTH(1)];
y = [Yed(1)];
XTH=[x XTH];
Yed = transpose([y Yed]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = length(XTH); 
l = 0;
k = 0;
Vmin = 100000000;
r=0;
for u1 = 1:XTH(end) 
    l = l + 1;
    if ismember(u1,XTH)==1
        u1 = u1 + 0.1;
    end
    if u1>=XTH(1)+1 && u1< XTH(end)-1
        for u2 = u1:XTH(end)
            k = k + 1;
            if u2 == u1
               u2 = u2 + 1;
            end
            if ismember(u2,XTH)
               u2 = u2 + 1;
            end
            XTH = [(XTH) u1 u2];
            XTH = sort(XTH);
            num1 = find(XTH==u1);
            num2 = find(XTH==u2);
            XTH = XTH(XTH~=u1);
            XTH = XTH(XTH~=u2);
            a11 = XTH(1:num1);
            a12 = repelem(u1,length(XTH)-num1);
            a1 = transpose([a11, a12]); 
            a2 = ones(n,1);
            a31 = transpose(zeros(num1,1));
            a32 = XTH(num1+1:num2-1)-u1;
            a33 = repelem(u2-u1,length(XTH(num2:end)));
            a3 = transpose([a31 a32 a33]);
            a41 = transpose(zeros(num2-1,1));
            a42 = XTH(num2:end)-u2;
            a4 = transpose([a41 a42]);
            A = [a1 a2 a3 a4];
            AtA = transpose(A)*A;
            B = Yed;
            AtB = transpose(A)*B;
            if det(AtA)==0 
               continue 
            end
            if det(AtA)<1e-3
               continue
            end
            solution = linsolve(AtA, AtB);
            alpha1 = solution(1,1);
            beta1 = solution(2,1);
            alpha2 = solution(3,1);
            alpha3 = solution(4,1);
            beta2 = (alpha1*u1) + beta1 - (alpha2*u1);
            beta3 = (alpha1*u1) + beta1 + alpha2*(u2-u1) - (alpha3*u2);
            dy1 = 0;
            dy2 = 0;
            dy3 = 0;
            Dx = XTH(end)-XTH(1);
            width = 10;
            for j = 1:num1
                dy1 = dy1 + abs((Yed(j)-(alpha1*XTH(j)+beta1)));
            end
            for j = num1:num2-1
                dy2 = dy2 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
            end
            for j = num2:n
                dy3 = dy3 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
            end
             V(l+k) = Dx*width*(1/length(Yed))*(dy1+dy2+dy3);
             if V(l+k) < Vmin && V(l+k)~=0
                 aa1 = alpha1;
                 aa2 = alpha2;
                 aa3 = alpha3;
                 bb1 = beta1;
                 bb2 = beta2;
                 bb3 = beta3;
                 U1 = u1;
                 U2 = u2;
                 Vmin = V(l+k);
             end
        end
    end
end
bar(V(l+k),XTH)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot(transpose(XTH), Yed, 'k')
daspect([10,1,1]);
% ax = gca;
% ax.XLim = [XTH(1) XTH(end)];
hold on;
grid on;
x11 = XTH(1);
x21 = U1;
y11 = aa1*x11 + bb1;
y21 = aa1*x21 + bb1;
plot([x11,x21],[y11,y21], 'r')
x21 = U1;
x22 = U2;
y21 = aa2*x21 + bb2;
y22 = aa2*x22 + bb2;
plot([x21,x22],[y21,y22], 'r')
x31 = U2;
x32 = XTH(end);
y31 = aa3*x31 + bb3;
y32 = aa3*x32 + bb3;
plot([x31,x32],[y31,y32], 'r')
%line(U1,aa1*U1 + bb1)
disp('Line equation 1:')
line1 = ['y = ', num2str(aa1),'*x + ',num2str(bb1)];
disp(line1)
disp('Line equation 2:')
line2 = ['y = ', num2str(aa2),'*x + ',num2str(bb2)];
disp(line2)
line3 = ['y = ', num2str(aa3),'*x + ',num2str(bb3)];
disp(line3);
vol = [num2str(Vmin),' m^3'];
disp('Minimum volume achieved:')
disp(vol)
댓글 수: 0
답변 (1개)
  SaiDileep Kola
    
 2021년 1월 19일
        Decent way to do is know which parts of the code are taking long time by profiling, check link for profile documentation here, then check for optimisation or alternative snippets for that part.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Semiconductors and Converters에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!