Can anyone suggest something that could fix the program

1 回表示 (過去 30 日間)
Titu Thomas
Titu Thomas 2019 年 7 月 30 日
回答済み: Prabhan Purwar 2019 年 8 月 2 日
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 件のコメント
Alex Mcaulley
Alex Mcaulley 2019 年 7 月 30 日
What is your issue? Are you getting any error?
Titu Thomas
Titu Thomas 2019 年 7 月 30 日
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:

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by