Is there a way to reduce compilation time for this segment?

tic
iscorr = 1;
myunc = zeros(1,length(Kurzschluss.Plateaus_I));
myfunction = '(X2/X1)*(sqrt(1 - X3^2/(X2^2 * X1^2)))';
for i=1:length(mystruct.Plateaus_I)
x1 = mystruct.Plateaus_I{1,i};
x2 = mystruct.Plateaus_U{1,i};
x3 = mystruct.Plateaus_P{1,i};
myunc(i) = calculateEndUncertainty_3(x1,x2,x3,iscorr,myfunction);
end
toc

4 Comments

Can you show calculateEndUncertainty_3 function?
function [unc] = CalculateEndUncertainty_3(x1,x2,x3,iscorr,myfunction)
N = 3;
if iscorr
Enable_correlation = 1;
else
Enable_correlation = 0;
end
StrModelFunction = myfunction;
StrInput = '';
StrMean = '';
for c1 = 1:N
StrInput = [StrInput 'X' num2str(c1) ','];
StrMean = [StrMean 'mean(x' num2str(c1) '),'];
end
StrInput(end) = [];
StrMean(end) = [];
eval(['syms f(' StrInput ')'])
eval(['f(' StrInput ') =' StrModelFunction ';'])
u2 = 0;
for c1 = 1:N
temp = diff(f,eval(['X' num2str(c1)]));
df_dx = double(eval(['temp(' StrMean ')']));
u2 = u2 + df_dx^2*std(eval(['x' num2str(c1)]))^2/length(x1) ;
end
if Enable_correlation
for c1 = 1:N-1
for c2 = c1+1:N
temp_c1 = diff(f,eval(['X' num2str(c1)]));
df_dx_c1 = double(eval(['temp_c1(' StrMean ')']));
temp_c2 = diff(f,eval(['X' num2str(c2)]));
df_dx_c2 = double(eval(['temp_c2(' StrMean ')']));
temp = cov(eval(['x' num2str(c1)]),eval(['x' num2str(c2)]));
u2 = u2 + 2*df_dx_c1*df_dx_c2*temp(2)/length(x1);
end
end
end
unc = sqrt(abs(u2));
res = double(eval(['f(' StrMean ')']));
end
You have a lot of eval() that is why script is slow
The function below depends on f(), temp(), temp_c1(), temp_c2()
Maybe you can attach whole your project? Any description?
The whole project is way too long. This function is supposed to take 3 inputs of a function as vectors (in my case containing about 5 lightly diffefrent values) and calculate the uncertainty of the result based on the mean value of each input vector etc.. The function has been applied on 3 unidimensional cells named Plateaus_I, Plateaus_U and Plateaus_P containing a vector in each cell.

Sign in to comment.

More Answers (1)

Hi Souheil!
If you have access to the parallel computing toolbox, you can improve execution time with a parfor loop.
tic
iscorr = 1;
myunc = zeros(1,length(Kurzschluss.Plateaus_I));
myfunction = '(X2/X1)*(sqrt(1 - X3^2/(X2^2 * X1^2)))';
parfor i=1:length(mystruct.Plateaus_I)
x1 = mystruct.Plateaus_I{1,i};
x2 = mystruct.Plateaus_U{1,i};
x3 = mystruct.Plateaus_P{1,i};
myunc(i) = calculateEndUncertainty_3(x1,x2,x3,iscorr,myfunction);
end
toc
Other optimizations will likely come from improving your functions, such as calculateEndUncertainty_3. This page from MathWorks has some great advice on how to do this. I would reccomend looking at vectorization in particular, as MATLAB is extremely powerful when properly vectorized.
Hope this helps, good luck!

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Products

Release

R2015b

Community Treasure Hunt

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

Start Hunting!