Can anyone suggest something that could fix the program

1 view (last 30 days)
clear all
clc
%following line calls my data
[xdataini, ydataini] = textread('data.txt','%f %f','headerlines',0);
figure;
plot(xdataini,ydataini,'ok')
ylim([0,1]);
xlim([0,0.25]);
%warning off MATLAB:divideByZero
xdata=xdataini(1:245);
ydata=ydataini(1:245);
x0=[1,1];
[x]=lsqcurvefit(@subprogramTL,x0,xdata,ydata);
V=subprogramTL([x],xdata);
fid = fopen('data1.txt','w');
fprintf(fid,'%3.5f \n',V);
fclose(fid);
hold on
plot (xdata,V,'r')
hold off
SUBPROGRAM
function F=subprogramTL(x,xdata)
m = 5.8;
v = 1.7;
F=(1-((x(1)/2).*atan((2.*m.*v)/((1+2.*m)^2 + v^2)*(x(2)./(2*xdata)) + 1 + 2.*m + v.^2))).^2;
%+ ((x(1)/4).*log(((1+((2*m)./(1+(2*xdata)/x(2)))).^2 + v.^2)/((1+(2*m)).^2 +v.^2)).^2);
end
  2 Comments
Titu Thomas
Titu Thomas on 30 Jul 2019
There is no error while running the code, but the curve does not fit with the data points

Sign in to comment.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 2 Aug 2019
Code is working perfectly fine. The output of lsqcurvefit() function is the best fit of variables and depends upon assigned model. For more information refer to following link:
As the data appears to be like an exponential function, then I would suggest you make use of fit() function with appropriate fittype and fitoption as explained in the following example.
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'StartPoint',[1 1]);
ft = fittype('a*(x-b)^n','problem','n','options',fo);
[curve2,gof2] = fit(cdate,pop,ft,'problem',2)
For more information refer to following link:

Community Treasure Hunt

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

Start Hunting!