# Reducing Computation Time!

3 views (last 30 days)
Mujtaba Farrukh on 14 Apr 2022
Answered: Walter Roberson on 15 Apr 2022
Hi there,
I am running a piece of code but the thing is,it executes in more than 7 minutes.
Objective: Reduce time of computation
I am attaching the code. Please do have a look, if there is any way to sort it out and reduce the time of execution,
Also, I am thinking of applying a global optimization toolbox to find min and max points. Is it helpful to be used?
Code:
%% CLEARING ALL THE PARAMETERS
clc;
clear;
close all;
%% FPROGRAM
tic
RMSE=1;
step=[10e-26];
flag = 0;
for i=1:length(step)
for a = 10^-26:step(i):10^-22
for b= 1:0.005:4
for c = 0.1:0.001:0.5
VF_new = b.*0.0296.*log((([20 30 60 100 350 500 700 1000].*1e-3)./a)+1)+([20 30 60 100 350 500 700 1000].*1e-3).*c;
d = sqrt((sum((VF_new-[2.596 2.619 2.666 2.712 2.912 3.008 3.126 3.294]).^2))/8);
if(d<RMSE)
RMSE = d; VF = VF_new; Rs = c; m = b; Io = a;
end
end
end
end
time(i)=toc;
end
%% PLOTTING OF THE GRAPH
stem(VF,'r','LineWidth',3)
str="{\color{red}RMSE }"+num2str(RMSE)+"{\color{red} Rs: }"+num2str(Rs)+"{\color{red} m: }"+num2str(m)+"{\color{red} Io: }"+num2str(Io);
title('VF Graph')
subtitle(str)
time=time';
step=step';
T=table(step,time)
Steven Lord on 14 Apr 2022
It might be easier to determine how to reduce the amount of time this code takes to do what it does if we understood what it's supposed to do. Could you give an explanation (in words not code) of what you're trying to do?

Walter Roberson on 15 Apr 2022
for i=1:length(step)
for a = 10^-26:step(i):10^-22
for b= 1:0.005:4
for c = 0.1:0.001:0.5
VF_new = b.*0.0296.*log((([20 30 60 100 350 500 700 1000].*1e-3)./a)+1)+([20 30 60 100 350 500 700 1000].*1e-3).*c;
d = sqrt((sum((VF_new-[2.596 2.619 2.666 2.712 2.912 3.008 3.126 3.294]).^2))/8);
if(d<RMSE)
RMSE = d; VF = VF_new; Rs = c; m = b; Io = a;
end
end
end
end
time(i)=toc;
end
That code iterates over all values of the vector step (there is only one in what you initialized.) For each different value of step it uses step as the distance between adjacent a values. It takes all those a values, and all the b values, and all the c values, and it finds the combination of a, b, c that minimizes the Root Mean Squared Error for VF_new, recording the combination of a, b, c that gave the best fit.
This would typically be something that you would instead use a minimizer such as fmincon for. Global Optimization Toolbox could perhaps be used if you were concerned that there were multiple local minima.
The code you posted is for discrete minimization, minimization at exactly a set up points. Using fmincon() would be for continuous minimization.
In the case of discrete minimization, sometimes the most efficient way is to set up a multidimensional grid of all of the possibilities, and then to min()

### Categories

Find more on Solver Outputs and Iterative Display in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by