Can anyone suggest something that could fix the program

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

What is your issue? Are you getting any error?
There is no error while running the code, but the curve does not fit with the data points

댓글을 달려면 로그인하십시오.

답변 (1개)

Prabhan Purwar
Prabhan Purwar 2019년 8월 2일
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:

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

질문:

2019년 7월 30일

답변:

2019년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by