Minimize squared error function with fmincon

3 vues (au cours des 30 derniers jours)
John Katsantonis
John Katsantonis le 10 Déc 2021
Hello ! I'm fairly new to matlab and I have run into a problem . I need to minimise an existing function with fmincon from matlab and have as a result two variants, k1 and k2 ( numbers). THe function itself, returns the mean squared error between 2 vectors ( the result is also a number). In order to do it , it calls another function which reads and transforms the input data and produces the 2 vectors for the final compare and calculation if the square error. The 2 variants, k1 and k2 are input arguments on both of the functions ( error function and internal function). So how should I syntax the fmincon to minimize the mean squad error value in order to return me the optimal k1 and k2 ?
I hope what I'm asking makes sense...if not , I hope the code helps ...Simply: I want to minimize MSE, and find optimal k1 and k2....
function [MSE]=sqrdError(a,b,k1,k2,c,b)
[e,D,w] = function(k1,k2,a,b);
figure; plot(e,D,e,w,c,b,'*');% I plot both the experimental and numerical results
mse=0;
F=zeros(size(c));
for i=1:length(c)
[~,index]= min(abs(eps-c(i)));
F(i)=D(index);
v=(b(i)-F(i))^2;
mse=mse+v;
end
MSE=1/length(F)*mse;
end

Réponses (1)

Rik
Rik le 10 Déc 2021
fmincon promises to minimize the function value if you provide it with a vector. So the solution is to have a wrapper function that takes in a two-element vector and returns a scalar.
%looks like this should do:
wrapper=@(k_vector) sqrdError(a,b,k_vector(1),k_vector(2),c,b);
  1 commentaire
John Katsantonis
John Katsantonis le 10 Déc 2021
So I will give the wrapper to fmincon then, and it should work, right? Thanks a lot , I'll come back with the results !!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by