Minimize squared error function with fmincon

3 Ansichten (letzte 30 Tage)
John Katsantonis
John Katsantonis am 10 Dez. 2021
Bearbeitet: John Katsantonis am 10 Dez. 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

Antworten (1)

Rik
Rik am 10 Dez. 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 Kommentar
John Katsantonis
John Katsantonis am 10 Dez. 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 !!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by