# CurveFitter.setOffsetMultiplySlopeParams -- unexpected behavior

Hello all,

I’m having some trouble with the CurveFitter.setOffsetMultiplySlopeParams. The fitting succeeds without using it most of the time, but takes a while, so I though it might help. However, even in cases that do fine without setOffsetMultiplySlopeParams, if I use it as shown in the code sample below, the doCustomFit method returns after very few iterations, reporting success, but the fit is totally off with a near-zero R^2 value. As you can see, the returned parameters are completely different depending on whether the line calling setOffsetMultiplySlopeParams is included or commented out. The data input arrays in the code are not sorted, but having them sorted does not change the outcome.

I hope the code isn’t confusing, with arrays etc. that contain actual test case data. I have made simple toy code before to see if I know how to use setOffsetMultiplySlopeParams, and in those cases using it has not changed the result, it has only sped up the process 10-20x. Here I’m seeing something different and don’t understand why. Any tips would be appreciated.

Regards,
David

CurveFitter.getResultString without setOffsetMultiplySlopeParams:
Formula: (defined in plugin)
Status: Success
Number of completed minimizations: 4
Number of iterations: 6865 (max: 37500)
Time: 273 ms
Sum of residuals squared: 290522.346
Standard deviation: 27.12010
R^2: 0.78421
Parameters:
a = 626.17988
b = 417.49826
c = 39.83278
d = 3.60259
e = 0.27787

## CurveFitter.getResultString with setOffsetMultiplySlopeParams: Formula: (defined in plugin) Status: Success Number of completed minimizations: 2 Number of iterations: 4 (max: 24000) Time: 9 ms Sum of residuals squared: 1346306.73 Standard deviation: 127.66231 R^2: 0.00000 Parameters: a = 280.57576 b = 0.64139 c = 281.26236 d = 7.71855 e = -3.10118

package imagejtest;

import static java.lang.Math.*;
import ij.measure.CurveFitter;
import ij.measure.UserFunction;

public class CurveFitterTest implements UserFunction {

static double w = 13.343434343434344;

@Override
public double userFunction(double[] p, double x) {
// ellipse curve of width w with a superimposed gaussian peak
// return NaN as penalty
if(abs(x) < 0.5*w)
return (p[1]<0 || p[2]<0 || p[3]<0 || abs(p[4])>w/4) ? Double.NaN : p[0] - p[1]*sqrt(1-4*x*x/(w*w)) + p[2]*exp( -(x-p[4])*(x-p[4]) / (p[3]) );
else return p[0];
}

public static void main(String[] args) {

double[] x = {-5.755200643605778, -6.237186450183008, -6.340046344621093, -4.459266224742079, -4.600585682994414, -5.802586095020653, -5.767047035169177, -3.4954892718899644, -3.749846129464858, -4.453495753990013, -4.380592759588221, -5.000595180884476, -5.292237912219956, -2.7984971293360426, -2.8535450162985314, -2.7293468011583606, -3.9377498682016117, -4.022941242214405, -5.054391704687049, -5.4816434696795575, -5.214667399090539, -1.2078127882841638, -1.9556892900057894, -2.3573616341616654, -2.5046943714985783, -2.981941761825784, -2.905065900234071, -4.11370439695899, -4.549592830867487, -4.597515463253774, -4.73305448299097, -0.08664551541156819, -0.9719323429780921, -1.2952165749065647, -1.409447593802207, -1.280273972581896, -1.271218387004204, -2.429255491670018, -2.9026317557814427, -2.9289132863727882, -2.661242587291584, -4.5511834021791255, -4.821587293334881, -5.497646446526448, 0.9327768595408985, -0.3569841361805866, 0.03706630124946346, -0.2967205332295649, -1.343592757650165, -0.8717062307695449, -1.509659386519669, -2.0661745160850185, -2.48651735510417, -2.5111246815459745, -3.1526245189650557, -4.365838318648141, -5.028964515539406, -5.263640746532236, -5.3674499855859725, 1.8139561557780686, 1.6290352483145023, 0.42112533468800195, 1.533667317009916, 1.3478350998505448, -0.07537191904970485, 0.010321576309428497, -1.3338722852727662, -0.8968396306865705, -1.34374126435162, -2.079963022760796, -3.090856452283396, -3.2928150765284707, -3.573375985920539, -4.448802882019766, -4.180948227138934, -4.604071405813303, -5.145965977764418, 2.427754750455602, 1.655712834755907, 2.1766582211266394, 2.265542333499389, 1.5162148164425233, 1.0554492145163583, 1.037889354843237, -0.4176795778453249, 0.044135116147336416, -1.2163755851283757, -1.3590276617137276, -2.072494210150951, -2.2405920378119673, -3.0441586837811916, -3.1575872947009085, -3.7099029821972493, -4.166209981605503, -4.556013889208291, -4.991379892375574, 4.627085547757938, 3.1837253592216697, 2.424713721503715, 2.760595221822327, 2.831721631609294, 2.5438281325357686, 1.9542172183734459, 1.7443352028997567, 1.1249874940327205, 0.3531087332045941, 0.28602511670188946, -0.8282178437705695, -1.4560005655997104, -1.7403714215081243, -2.196468405577896, -2.0869420645139067, -2.7899375148906684, -3.653861460522754, -3.75865548761927, -4.663035996156361, -4.9548265521581, 5.144948127131694, 4.290481360165207, 3.9981749271936082, 3.483081971277165, 4.027767333220814, 2.552747344593384, 2.9501010846946034, 1.989806119228661, 1.6244183186061965, 1.82685311713065, 1.0185909471657193, -0.26070809750809426, 0.4705920197786066, -0.5699200240376683, -1.0619176558472796, -1.1118551105877712, -1.902417321342882, -2.601718041300186, -2.958267108353518, -3.559821163557666, -3.8724208884626496, -4.603380377768107, -5.309570719437715, 5.890326532877837, 5.850469745895602, 5.1631981272564635, 5.146905194970736, 4.963174047599577, 4.453008650011621, 3.762949385302778, 4.13195186837587, 3.4407930491614698, 2.7069406097202076, 2.3109924584386246, 1.4769833362043505, 1.0284610404777517, 1.438179340536892, 0.04497840849668693, 0.0426890164098965, -1.2573612068269353, -1.1922746283330596, -1.7036343959515317, -1.944355963694684, -2.952544639739003, -3.1212650838321934, -4.303104856041421, -4.147082922933702, -5.516528326587281, -5.600771953073428, 5.800677054943524, 5.658544240031604, 5.694917736745363, 5.254976556489124, 4.936724235634603, 4.899999863416082, 4.401756484428155, 3.4036548378379448, 2.7492197332193276, 2.2760184614588885, 1.6614532796045023, 1.5320747913791164, 1.7613689338156253, 1.1423358727411939, 0.03660840658756355, -0.13295946762393557, -0.48720091971178103, -1.624406285768985, -2.898546898907929, -2.4583582462961804, -2.627697617205728, -4.213960916447199, -3.9852915945297878, -4.209284403201663, -4.356468803815252, 6.155938668806834, 5.181300330139021, 4.988887385918895, 3.9808258617318706, 3.8511726307109795, 3.0062167382238894, 3.07437237693182, 2.3145881096391054, 2.085708332438715, 1.5614470252374393, 1.2845624188135947, 0.4911351366114618, -0.1326801601885081, -1.351954670416414, -1.958378008685977, -2.1817079357632245, -1.9597518037433737, -3.0622578115790016, -3.4957278182401748, -4.116701734811603, -4.386316020298959, -4.6419931795700995, -5.996375697328056, 5.058042344160814, 4.684800942424563, 3.8226518042414934, 3.569573326116187, 3.1854876658771794, 2.7864419517721624, 2.249330656915843, 2.4737842619134867, 1.5395845612065442, 1.4056149908722966, -0.257586608689189, -0.8149359524588153, -1.6418825865642126, -0.913491359785271, -2.0497087450537634, -2.091955546746389, -2.63609510748523, -3.991380894461593, -3.8305623670417837, -4.714137110004483, -5.191004394118802, 5.522323274737268, 5.546541844189876, 4.579391668436148, 4.2088353632642646, 3.7671943735651525, 2.8986626351934226, 2.9394796667934653, 2.355708115382841, 1.5749745415441119, 1.2847397391734607, 0.13514906704017193, 0.15549992678739158, -1.2185985977552656, -0.9836101460920506, -1.3568441554229556, -2.5457257915125293, -2.1553831213625907, -3.4892020530211822, -3.7856360787430137, -3.8931672416211938, -4.7967007958489765, -5.54124774894381, -5.391613099156466, 6.066642372253848, 4.756241113072074, 4.585514189409517, 4.433928782594931, 3.2328577081037335, 2.874685521907804, 2.1649442457626944, 1.8051184146618782, 0.9161821434257327, 0.7530142265730873, -0.31921998517171907, 0.01553456693693156, -0.8619612505272001, -1.3972418003884044, -2.32459800808591, -2.5884815681546285, -2.723555966001135, -4.251862694187811, -4.19275847520554, -3.764844684971772, -4.485128024146069, -4.816958617629679, 5.78259370283372, 4.7593599408197536, 4.469232274800978, 4.007922995766263, 2.8977412364003405, 2.693481129999291, 1.588944177725641, 1.4932081513867876, 1.1916819018627853, 1.2809210032338547, 0.008754358920706523, -0.7473929370539548, -0.5776227281717148, -0.9495149068891655, -2.204432406487586, -2.5157038078781535, -2.861184947321089, -2.9888686219229044, -4.1950058821031835, -4.588680275795637, -4.642609505981006, 5.601321545305141, 4.218232729650033, 3.6173863174345238, 2.8563172289531624, 2.8930203004860697, 2.377005300723559, 2.047031638371642, 1.106247489585471, 0.7766478863498005, -0.011887763516964713, -0.5000749742592834, -1.3934368592506374, -1.601789198696718, -2.4147762046231613, -1.7312549742233718, -1.7281198562034086, -2.7172915355443026, -3.574048701721113, -4.483799513966057, 5.3630058604767035, 4.942933990572427, 4.048303518384326, 3.777445853581238, 3.315030875258264, 3.101648930623071, 1.6847411728224146, 1.5420278103625338, 0.4726788195143634, 0.8171374037183647, 0.29946688202973143, -0.8759482875493814, -1.1387404246116029, -0.9216861950016038, -1.1689018554271144, -2.7150541353515325, -3.2801010000768316, 5.315743837818779, 4.7913848787457045, 4.647111191472886, 4.198609373108346, 3.601270137273373, 3.108712884324188, 1.3398424989028883, 2.209011611451199, 1.3330738352549514, 0.9993353726515538, 0.42650334767810166, -0.4282032662902455, 0.6421248257251169, -0.6009724212307055, -1.4855017147877878, 5.049581366368597, 4.286480609273934, 4.291691478107673, 3.387817857212017, 3.289188338850307, 2.814035901063098, 2.396801784300046, 1.3442323834893022, 1.1263158623465852, 0.9916758970558976, 0.9342582320173372, 0.09004179528896684, -1.7558157590526964, 5.515816646721076, 4.831820609930741, 4.2429152192472035, 3.8199735719779295, 3.11155840796018, 1.7644342526588261, 1.8973447246768047, 1.8675214375138596, 1.1403591006337488, 1.2621002250473683, 5.603434226712667, 4.950442443480764, 4.530651929107156, 3.809050184045073, 3.2402454974065806, 3.268154155117253, 3.0278533295021086, 2.1898012605353387, 1.8281462867838636, 5.323035282170778, 4.118546609483089, 4.057517567745691, 4.3386592266716795, 3.8177369567469523, 2.973720835151188, 5.168818735867125, 5.150020663761977, 4.527163976435801, 4.649465087631417, 3.5153798714331375, 4.775672609433684};
double[] y = {426.0, 472.0, 505.0, 375.0, 381.0, 425.0, 458.0, 270.0, 291.0, 354.0, 343.0, 394.0, 434.0, 248.0, 275.0, 280.0, 318.0, 320.0, 349.0, 381.0, 422.0, 283.0, 257.0, 270.0, 257.0, 268.0, 256.0, 299.0, 316.0, 427.0, 427.0, 295.0, 269.0, 281.0, 265.0, 250.0, 262.0, 261.0, 253.0, 260.0, 308.0, 349.0, 356.0, 393.0, 294.0, 285.0, 294.0, 245.0, 234.0, 236.0, 241.0, 245.0, 246.0, 274.0, 266.0, 303.0, 351.0, 382.0, 393.0, 244.0, 303.0, 269.0, 294.0, 272.0, 269.0, 240.0, 270.0, 258.0, 224.0, 233.0, 270.0, 276.0, 297.0, 338.0, 321.0, 387.0, 392.0, 297.0, 241.0, 286.0, 264.0, 268.0, 249.0, 258.0, 244.0, 247.0, 248.0, 231.0, 210.0, 225.0, 249.0, 275.0, 252.0, 305.0, 363.0, 357.0, 305.0, 263.0, 275.0, 273.0, 249.0, 279.0, 256.0, 239.0, 266.0, 262.0, 277.0, 258.0, 242.0, 248.0, 233.0, 245.0, 256.0, 280.0, 272.0, 308.0, 353.0, 417.0, 380.0, 340.0, 324.0, 293.0, 253.0, 244.0, 242.0, 277.0, 228.0, 281.0, 259.0, 211.0, 235.0, 219.0, 253.0, 241.0, 240.0, 276.0, 284.0, 275.0, 340.0, 383.0, 476.0, 460.0, 404.0, 343.0, 335.0, 306.0, 285.0, 269.0, 264.0, 250.0, 246.0, 278.0, 270.0, 276.0, 265.0, 224.0, 215.0, 246.0, 223.0, 261.0, 249.0, 279.0, 271.0, 320.0, 323.0, 382.0, 480.0, 455.0, 381.0, 404.0, 324.0, 305.0, 279.0, 267.0, 249.0, 263.0, 264.0, 264.0, 230.0, 241.0, 221.0, 249.0, 222.0, 241.0, 245.0, 266.0, 247.0, 233.0, 303.0, 328.0, 360.0, 428.0, 370.0, 331.0, 309.0, 287.0, 247.0, 264.0, 264.0, 272.0, 223.0, 212.0, 248.0, 244.0, 227.0, 237.0, 225.0, 245.0, 222.0, 223.0, 264.0, 278.0, 298.0, 360.0, 375.0, 285.0, 259.0, 284.0, 242.0, 263.0, 251.0, 240.0, 246.0, 236.0, 233.0, 215.0, 257.0, 235.0, 231.0, 227.0, 243.0, 230.0, 265.0, 328.0, 377.0, 394.0, 342.0, 308.0, 297.0, 270.0, 269.0, 239.0, 271.0, 265.0, 231.0, 258.0, 254.0, 228.0, 234.0, 241.0, 213.0, 214.0, 260.0, 249.0, 293.0, 348.0, 390.0, 418.0, 330.0, 334.0, 310.0, 280.0, 284.0, 248.0, 246.0, 238.0, 234.0, 259.0, 267.0, 230.0, 232.0, 209.0, 223.0, 237.0, 210.0, 241.0, 258.0, 271.0, 338.0, 389.0, 393.0, 338.0, 314.0, 238.0, 261.0, 243.0, 241.0, 227.0, 262.0, 242.0, 263.0, 224.0, 241.0, 235.0, 218.0, 235.0, 232.0, 246.0, 272.0, 292.0, 357.0, 353.0, 311.0, 285.0, 282.0, 249.0, 249.0, 219.0, 226.0, 230.0, 220.0, 227.0, 208.0, 234.0, 230.0, 204.0, 257.0, 228.0, 254.0, 275.0, 377.0, 328.0, 292.0, 286.0, 259.0, 254.0, 254.0, 215.0, 224.0, 221.0, 226.0, 205.0, 229.0, 231.0, 204.0, 257.0, 249.0, 371.0, 341.0, 348.0, 283.0, 270.0, 231.0, 260.0, 226.0, 222.0, 217.0, 216.0, 218.0, 258.0, 228.0, 230.0, 383.0, 342.0, 318.0, 292.0, 262.0, 245.0, 216.0, 239.0, 211.0, 233.0, 236.0, 252.0, 232.0, 349.0, 327.0, 281.0, 262.0, 228.0, 248.0, 228.0, 229.0, 232.0, 251.0, 419.0, 304.0, 308.0, 263.0, 250.0, 217.0, 209.0, 218.0, 200.0, 329.0, 292.0, 290.0, 263.0, 242.0, 234.0, 411.0, 349.0, 319.0, 268.0, 287.0, 368.0};

double max = 404.0;
double min = 216.85;
double centerMean = 281.2;

CurveFitter fitter = new CurveFitter(x, y);
fitter.setOffsetMultiplySlopeParams(0, -1, -1);
fitter.doCustomFit(
new CurveFitterTest(),
5,
null,
new double[] {
max,
max - min,
centerMean,
w / 8,
0
},
new double[] {
(max - min) / 4,
(max - min) / 2,
(max - min) / 4,
w / 8,
w / 8
},
false
);

System.out.println(fitter.getResultString());
}
}

You might want to repost this question on the ImageJ mailing list, because I suspect the experts on the CurveFitter code—besides @Wayne—have not made their way over here to the forum yet.

Thank you Curtis. I was thinking just that. Good to know it won’t be frowned upon to “cross-post”