On 9/1/2013 3:16 AM, Kapila Bandara wrote: > i want to know how can i put constraint in curve fitting optimization function. > > For example i want to give constraint as 0<beta <1 in following function. > > ....................................................... > function [estimates, model] = fitcurvedemo(xdata, ydata) > % Call fminsearch with a random starting point. > start_point = rand(1,4 ); > model = @expfun; > estimates = fminsearch(model, start_point); > % expfun accepts curve parameters as inputs, and outputs sse, > % the sum of squares error for A*exp(-lambda*xdata)-ydata, > % and the FittedCurve. FMINSEARCH only needs sse, but we want > % to plot the FittedCurve at the end. > function [sse, FittedCurve] = expfun(params) > A = params(1); > lambda = params(2); > B=params(3); > beta=params(4); > FittedCurve = (A .* exp(-lambda * xdata) +B.*xdata.^beta); > ErrorVector = FittedCurve - ydata; > sse = sum(ErrorVector .^ 2); > end > end
You can use fmincon instead of fminsearch if you have Optimization Toolbox.
Or, for this simple constraint, use exp(-beta^2) where you currently have beta in FittedCurve. exp(-beta^2) is always between 0 and 1. I recommend you start beta out with a value of 1, not something close to 0 or higher than 2.
Alan Weiss MATLAB mathematical toolbox documentation