Info

This question is closed. Reopen it to edit or answer.

function minimization with different parameters

1 view (last 30 days)
OldCar
OldCar on 12 Sep 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a function defined by intervals, it is k/x for x>c and -|x|+q with x independent variable. I want to minimize the difference between this function and some data (it is a fit MLH). the function to minimize is CHI2=sum((yi-f(x)).^2./stdi). I am not able to give a function divided by steps to fminsearch. The function f(x) is continous but not derivable.

Answers (1)

Walter Roberson
Walter Roberson on 13 Sep 2016
fminsearch does not require derivatives. You can use standard logical indexing techniques in calculating your objective function
f = @(x) (x>c) .* k ./ (x + (x==0)) + (x<=c) .* (q - abs(x));
Note: your formula has a singularity at the point x = 0 in the case where c is negative, so unless you can constrain c to be positive, it is not continuous. I work around the division by 0 by adding 1 in the case where x == 0, which gives the "wrong" answer at 0, but as long as you know that c > 0 then when x == 0, x > c cannot be true so the (x > c) .* (expression) would multiply out to 0, making the exception irrelevant. This would not be the case if the division by 0 was left in, as k/0 for non-zero k would give either +inf or -inf and either of those multiplied by the 0 from (x > c) would give NaN. So the (x + (x==0)) in the denominator is there so NaN does not get introduced for positive c and x == 0. But if your c < 0 and your x == 0 then You Have A Problem.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!