Minimize squared error function with fmincon
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Rik
el 10 de Dic. de 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);
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!