Info

この質問は閉じられています。 編集または回答するには再度開いてください。

function minimization with different parameters

1 回表示 (過去 30 日間)
OldCar
OldCar 2016 年 9 月 12 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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.

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 9 月 13 日
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!

Translated by