MATLAB: How to use fminsearch for a vector

fminsearchMATLABoptimizationvector

I am trying to write to code to deconvolute a complex function into a linear combination of trigonometric basis functions using fminsearch:
x = -pi:0.1:pi
y_sin=sin(x)
y_cos=cos(x)
y_tan=0.1*tan(x)
% define the variables
convol_spec=y_sin+2*y_cos+3*y_tan
%define the convoluted function
fun = @(coeff) (coeff(1)*y_sin) + (coeff(2)*y_cos) + (coeff(3)*y_tan) - convol_spec;
% define the objective function to search for "coeff" such that each trigonometric function will be multiplied by a coefficent "coeff"
coeff_guess= [0.33,0.33,0.33];
%guesses for the coefficents are all equally weighted
coeff = fminsearch(fun,coeff_guess)
%minimize the function using fminsearch with desired guesses for the coefficents
I get the following error "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-63." I suspect that this is becasue my tirgonometirc basis functions are 1×63 vectors and not scalers. Every example I have found uses scalers. Is it possible to use fminsearch for such sclaers? Thanks.

Best Answer

You must scalarize your objective function
coeff = fminsearch(@(c) norm(fun(c)) ,coeff_guess)