# is there a python (or matlab) function that achieves the minimum MSE between given set of output vector and calculated set of vector?

You can create an objective function that takes a and b as the input parameters and output a measure of fit (mse) and then use one of MATLAB in-built functions to minimise the error to get the best a and b. I have quickly create an example code below. You can modify it. Read more about fmincon – it allows you to put constraints on the solution etc.

``````function [a,b] = optimize_ab(x, h,f,g)
% h= h(x) should take vector x and yield vector output
% f=f(x,a)
% g=g(x,b)
% x: input vector
% ========================================================
% Example: find a function to approximate f=x^2 with a*x^1.5+b*x^2.5
% x=1:4;
% h(x)=x.^2;
% [email protected](x,a)a*x.^1.5;
% g(x)[email protected](x,b)b*x.^2.5;
% [a,b] = optimize_ab(x,h,f,g)
%=========================================================
h_=h(x);
N =length(x);
res = fmincon(@MSE, [0,0], [], []);
a=res(1);
b=res(2);

function mse = MSE(x_)
a_=x_(1);
b_=x_(2);
mse =sqrt(sum((h_-(f(x,a_)+g(x,b_))).^2)/N);
end
end
``````

CLICK HERE to find out more related problems solutions.

Scroll to Top