Calculate the optimum of function

Hello. I have the following function :
%%Déclaration de la fonction objective qu'on va utiliser
function MSE=mseFunction(alpha,beta)
%%Chargez les données des 6 jours
filename = 'BS_1.xlsx';
y = xlsread(filename);
%ys va contenir les données qu'on a obtenu pendant 6 jours
yS=y(1:288);
%%Calcul de la moyenne mobile d'ordre 48 pour les 6 jours
%Initialisation du vecteur qui va contenir la moyenne mobile
MMCS=zeros(240,1);
%Calcul de la moyenne mobile
for i=1:240
MMCS(i)=1/48.*((yS(i)/2)+sum(yS(i+1:i+47))+(yS(i+48)/2));
end
%Remplir les 24 premieres et derniéres cases par des zeros
MoyenneMobileCentreeS = padarray(MMCS,24,'both');
%%Calcul des composantes saisonniares
yMS=yS(25:264)./MoyenneMobileCentreeS(25:264);
%Réecrire le vecteur de telle sorte que les 24 premieres et derniére
%valeurs soient des zéros
bS = padarray(yMS,24,'both');
%Ecrire les valeurs du vecteur sous forme d'une matrice avec 48 lignes et 7 colonnes
MS = reshape(bS,[48,6]);
%Remplacer les zeros par des valeurs NaN pour ne pas les introduire dans le
%calcul de la médianne
MS(1:24,1)=NaN;
MS(25:48,6)=NaN;
%Calcul de la médianne de chaque ligne de la matrice
ComposantsSaisonniersS = nanmedian(MS,2);
%%Lissage exponnentiel simple
LES=zeros(288,1);
LES(25)=MoyenneMobileCentreeS(25);
for i=26:264;
LES(i)=alpha.*MoyenneMobileCentreeS(i)+(1-alpha).*LES(i-1);
end
%%Intégration de la composante saisonniére
S=zeros(264,1);
S(1:48,:)=ComposantsSaisonniersS;
for j=49:264;
S(j)=beta.*(yS(j)./LES(j))+(1-beta).*S(j-48);
end
%%Prévision
PREV=zeros(264,1);
PREV(26:264)=S(26:264).*LES(25:263);
PREV2=padarray(PREV(:),24,'post');
%%Calcul de la MSE
MSE = mean((y(26:264)-PREV2(26:264)).^2);
I want to calculate its optimum using "optimization tool". What should I put in the objective function tab. Thanks!

 Accepted Answer

Star Strider
Star Strider on 31 Dec 2016
Edited: Star Strider on 31 Dec 2016
One approach:
% % % b(1) = alpha, b(2) = beta
call_mseFunction = @(b) mseFunction(b(1),b(2));
B0 = [3; 5]; % Choose Appropriate Initial Estimates
Bopt = fminsearch(call_mseFunction, B0);
alpha = Bopt(1)
beta = Bopt(2)
NOTE This is UNTESTED CODE. It should work.

10 Comments

Hello Strider, This source code does not work. More I want to use "optimization tool". Thanks!
What does ‘does not work’ mean? Does it throw any errors?
I have no idea what your function does or what the arguments to it should be. The Optimization Tool will want to know this as well.
When I write the following function :
f=@(alpha,beta)mseFunction(alpha,beta)
displays this error
Optimization running.
Error running optimization.
Not enough input arguments.
Thanks Strider for your help!
My pleasure!
Use my version:
% % % b(1) = alpha, b(2) = beta
f = @(b) mseFunction(b(1),b(2));
B0 = [3; 5]; % Choose Appropriate Initial Estimates
Bopt = fminsearch(f, B0);
alpha = Bopt(1)
beta = Bopt(2)
I changed my code to use ‘f’ as the objective function name to be compatible with your code.
The optimization functions all require a vector of parameters to optimize. My code creates that vector and uses the individual elements for the ‘alpha’ and ‘beta’ arguments. When the optimization is complete, it assigns the appropriate results to your variables. (My earlier code had an error that I have now corrected.)
amine&&
amine&& on 31 Dec 2016
Edited: amine&& on 31 Dec 2016
Hello, you did not understand me Strider. I don't want to use " fminsearch" . But only " optimization tool". Thanks!
Then use it! I’m not stopping you!
I’m just suggesting that you test your ‘f’ function with my code first to be sure your ‘f’ function works correctly, since your implementation of it did not. All the optimization functions use the same objective function syntax, and if it works with my code, it will work with any function in the Optimization Toolbox.
If I use two files :
%%Chargez les données des 6 jours
filename = 'BS_1.xlsx';
y = xlsread(filename);
%ys va contenir les données qu'on a obtenu pendant 6 jours
yS=y(1:288);
and
%%Déclaration de la fonction objective qu'on va utiliser
function MSE=mseFunction(alpha,beta,yS)
%%Calcul de la moyenne mobile d'ordre 48 pour les 6 jours
%Initialisation du vecteur qui va contenir la moyenne mobile
MMCS=zeros(240,1);
%Calcul de la moyenne mobile
for i=1:240
MMCS(i)=1/48.*((yS(i)/2)+sum(yS(i+1:i+47))+(yS(i+48)/2));
end
%Remplir les 24 premieres et derniéres cases par des zeros
MoyenneMobileCentreeS = padarray(MMCS,24,'both');
%%Calcul des composantes saisonniares
yMS=yS(25:264)./MoyenneMobileCentreeS(25:264);
%Réecrire le vecteur de telle sorte que les 24 premieres et derniére
%valeurs soient des zéros
bS = padarray(yMS,24,'both');
%Ecrire les valeurs du vecteur sous forme d'une matrice avec 48 lignes et 7 colonnes
MS = reshape(bS,[48,6]);
%Remplacer les zeros par des valeurs NaN pour ne pas les introduire dans le
%calcul de la médianne
MS(1:24,1)=NaN;
MS(25:48,6)=NaN;
%Calcul de la médianne de chaque ligne de la matrice
ComposantsSaisonniersS = nanmedian(MS,2);
%%Lissage exponnentiel simple
LES=zeros(288,1);
LES(25)=MoyenneMobileCentreeS(25);
for i=26:264;
LES(i)=alpha.*MoyenneMobileCentreeS(i)+(1-alpha).*LES(i-1);
end
%%Intégration de la composante saisonniére
S=zeros(264,1);
S(1:48,:)=ComposantsSaisonniersS;
for j=49:264;
S(j)=beta.*(yS(j)./LES(j))+(1-beta).*S(j-48);
end
%%Prévision
PREV=zeros(264,1);
PREV(26:264)=S(26:264).*LES(25:263);
PREV2=padarray(PREV(:),24,'post');
%%Calcul de la MSE
MSE = mean((y(26:264)-PREV2(26:264)).^2);
How to rewrite f? Thanks!
My pleasure!
Your ‘yS’ variable already has to be present in your workspace before you use the new version of ‘f’.
Now that you are passing the extra parameter, ‘f’ changes slightly to:
f = @(b) mseFunction(b(1), b(2), yS);
You can then call it from any of the optimization functions in the Optimization Toolbox. The ‘optimtool’ function is interactive, so I cannot help you with it. You have to experiment with it on your own. (Note that it and the Optimization App are going to be removed in a future release.)
I still suggest that you test it with fminsearch first.
Thanks Strider.
My pleasure.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!