Minimize squared error function with fmincon
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Rik
on 10 Dec 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);
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!