forum.alglib.net
http://forum.alglib.net/

nonlinear fitting:circle fuction
http://forum.alglib.net/viewtopic.php?f=2&t=591
Page 1 of 1

Author:  Ray [ Tue Jun 19, 2012 7:24 pm ]
Post subject:  nonlinear fitting:circle fuction

Hello!
I really studied the manuals and the discussions in forum, but I did not found solution for my problem. My fitting program runs, but the solution of the parameters is strange.
I try to fit measured points with the function y=(r^2-(x-xm)^2)^(1/2)+ym ( upper half of a circle), because I scan a cylindrical object. I tried the example with lsfitcreatefg and lsfitcreatefgh, but both time the result I get is the same, namely as the initial value for the parametervector creal_1d_array c = "[9,9,4]" (expected [10,10,5])
The lsfitresults function puts the parameter 4 out, what means: gradient norm is no more than EpsG.
I tried to scale the parameters and to use bound constraints, but it does not work, I still get [9,9,4].
Please help me.

Attachments:
main.cpp [5.8 KiB]
Downloaded 753 times
main.rtf [8.93 KiB]
Downloaded 655 times

Author:  Sergey.Bochkanov [ Tue Jun 19, 2012 7:50 pm ]
Post subject:  Re: nonlinear fitting:circle fuction

Try to reformulate your problem. You tried to minimize sum of squared vertical distances from circle. But vertical distance is not good measure of fit quality.

For example, if you have (x,y) = (5,5) and R=4 - what is value of your merit function? f(x=5)=sqrt(4^2-5^2) = sqrt(-9) = 3*sqrt(-1). You can see that your target function is not defined for some reasonable values of R/xm/ym.

You should get better results with following parameterization: error(x_i, y_i, xm, ym, R) = (x_i-xm)^2+(y_i-ym)^2-R^2.

Author:  Ray [ Thu Jun 21, 2012 10:34 am ]
Post subject:  Re: nonlinear fitting:circle fuction

Hello mr. Bochkanov!
Thank you very much for the fast response. I tried to ipliment your suggestion (I have chosen func=(x-x_m)^2+(y-y_m)^2-r^2 )and it works, but still I get the same results as before.
I just can't understand, why I always get the result of what I inserted as initial value, namely real_1d_array c = "[9,9,4.5]";

Attachments:
File comment: code
main1.rtf [6.34 KiB]
Downloaded 668 times
File comment: code
main1.cpp [3.54 KiB]
Downloaded 776 times
File comment: Here are some "pseudo measured" values. The values scatter +/- 4 micrometer(thats the precision of my Triangulationlaser) from the real cylinder dimensions (y_m=10,x_m=10, r=5).
graph_1.jpg
graph_1.jpg [ 54.33 KiB | Viewed 7629 times ]

Author:  Sergey.Bochkanov [ Thu Jun 21, 2012 2:16 pm ]
Post subject:  Re: nonlinear fitting:circle fuction

Upppppsss, I MUST have noticed it at the very beginning :) You have an error in the gradient calculation code. Your code is

Code:
    grad[0] = 2*(x[0]-c[0]);
    grad[1] = 2*(x[1]-c[1]);
    grad[2] = -2*c[2];


and it should be

Code:
    grad[0] = -2*(x[0]-c[0]);
    grad[1] = -2*(x[1]-c[1]);
    grad[2] = -2*c[2];


after fixing this mistake, everything works fine. In case you still have some problems, feel free to post them here :)

Author:  Ray [ Thu Jun 21, 2012 3:07 pm ]
Post subject:  Re: nonlinear fitting:circle fuction

Dear mr. Bochkanov!
It works, I get super solution :-). You are wonderful!
If there is a way I can express my thankfullness, let me know.
Thank you, Thank you Thank you!

Author:  Ray [ Wed Nov 07, 2012 8:24 am ]
Post subject:  Re: nonlinear fitting:circle fuction

Hallo Mr. Bochkanov,
I am again working on the projekt in the University. Still have to fit measured data with a circle function (x-x_m)^2 + (y-y_m)^2=r^2 . I thought I understood how to do it, but now I have many doubts. In the example I posted on June 19 th I calculated the values of real_1d_array y by inserting the values of real_2d_array x in (x-x_m)^2 + (y-y_m)^2-r^2.
Is it possible to find the parameters x_m,y_m and r without calculating the values real_1d_array y. So is there a way just to put measured values x_i in real_2d_array x and measured values y_i in real_1d_array y.

I tried to do it, with y=sqrt(sqr(r) -sqr(x-x_m)) - y_m as circle function, but it does not work.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/