Minimize squared error function with fmincon

조회 수: 13 (최근 30일)
John Katsantonis
John Katsantonis 2021년 12월 10일
편집: John Katsantonis 2021년 12월 10일
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

답변 (1개)

Rik
Rik 2021년 12월 10일
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
John Katsantonis
John Katsantonis 2021년 12월 10일
So I will give the wrapper to fmincon then, and it should work, right? Thanks a lot , I'll come back with the results !!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by