Hi,
I've tried all tips given in several Matlab tutorials to improve the process of fitting a function but I cannot get the correct result. In the next code I try to fit the function with two sets of data where I know which would be the best fitting (xs and ys should be [-1 2] and [0 2]). Unfortunately, I cannot have the desired result no matter what I do.
Would anyone give some idea about what could I do?
Thanks
MATLAB code
%Values of the function to fit
T1=[2.39999999999998 3.60000000000002 5.20000000000005 ... 6.70000000000005 8.09999999999991 9.29999999999998 ... 9.80000000000007 10.3000000000000 10.8000000000000 ... 10.8000000000000 10.7500000000000 10.3000000000001 ... 9.60000000000002 8.79999999999996 7.30000000000001 ... 6.50000000000000 6.54999999999995 3.60000000000002 ... 1.79999999999995 -0.399999999999977 -2.50000000000000 ... -4.39999999999998 -6 -7.70000000000005 ... -9.20000000000005 -10.1000000000000 -10.9000000000000 ... -11.1000000000000 -10.8000000000000 -10.0999999999999 ... -8.79999999999996 -7.19999999999993 -5.50000000000000 ... -3.50000000000000 -1.20000000000005 0.399999999999977 ... 2.20000000000005];T2=[-3.50000000000000 -1.50000000000000 0.200000000000045 ...2.19999999999999 4 5.50000000000000 7 8 9.09999999999997 ...9.99999999999994 10.4000000000000 10.7000000000000 ...10.7000000000000 11.2000000000000 10 9.39999999999998 9 ...7.59999999999997 6.29999999999995 4.60000000000002 ...2.69999999999999 1 -1.39999999999998 -3.19999999999999 ... -5.40000000000009 -7.20000000000005 -8.89999999999998 ...-9.79999999999996 -10.6000000000000 -11.1000000000000 ...-11.5000000000000 -10.9000000000000 -9.50000000000000 ...-8.10000000000002 -6.50000000000000 -4.70000000000005 ...-2.70000000000005];xt=linspace(-180,180,37); %degrees
x=xt*2*pi/360; %radians
%options=fitoptions('gauss2');
%options.Robust='Bisquare';
options = fitoptions('Method', 'NonlinearLeastSquares');%Best fits should be at [-1 2] and [0 2]
options.Lower=[-5 -5]; options.Upper=[5 5];options.StartPoint=[0 0];% options.MaxFunEvals=;
% options.MaxIter=;
% options.TolFun=;
% options.TolX=;
% options.DiffMaxChange=;
% options.DiffMinChange=;
type = fittype( ... 'sqrt((xs)^2+(ys)^2)-sqrt((xs-.65*cos(x))^2+(ys-.65*sin(x))^2)');mycurve1=fit(x',T1',type,options)figure, plot(xt, T1, 'r-', xt, mycurve1(x), 'y-')mycurve2=fit(x',T2',type,options)hold on, plot(xt, T2, 'b-', xt, mycurve2(x), 'g-')
Best Answer