I am trying to fit some data with the model: exp(-b/x); When x goes to zero, y should go to zero as well since anything power negative infinity is zero. However Matlab sees the infinity and terminates everything. Here is my code:
vv=data(:,1);
ii=data(:,2);
g = fittype('exp(-b/x)');
f0 = fit(vv,ii,g);
xx = linspace(-1,1);
plot(vv,ii,'o',xx,f0(xx),'r-');
grid('on')

댓글 수: 5

Mathieu NOE
Mathieu NOE 2021년 2월 25일
hello
could you share the data as well ?
tx
Basil Eldeeb
Basil Eldeeb 2021년 2월 26일
it's unable to upload, the data is visibly linear, However my model is exponential:
0.000000 -4.333102E-5
0.050000 0.030123
0.100000 0.061024
0.150000 0.092435
0.200000 0.123947
0.250000 0.155877
0.300000 0.187916
0.350000 0.219968
0.400000 0.251753
0.450000 0.283991
0.500000 0.316017
0.550000 0.347719
0.600000 0.379196
0.650000 0.410459
0.700000 0.441492
0.750000 0.472394
0.800000 0.503174
0.850000 0.533827
0.900000 0.564569
0.950000 0.595374
1.000000 0.626224
Mathieu NOE
Mathieu NOE 2021년 2월 26일
yes , your data show a very linear trend
how can you expect to fit an exponential model to these data ? it will never work
Basil Eldeeb
Basil Eldeeb 2021년 2월 26일
supposedly when the exponent is quite small it will behave linearly to a first order. The model is more complex, actually, I am just facing a problem with the exp(-b/x) term. I want matlab to evaluate exp(-Inf) without giving error. The other answer shows promise. However it gave an error, you can see it in my response
not sure it's really a good model...
data = readmatrix('data.txt');
x = data(:,1);
y = data(:,2);
% exponential fit method
% model : y = exp(-b/x)
f = @(b,x) exp(b./x);
obj_fun = @(params) norm(f(params(1), x)-y);
sol = fminsearch(obj_fun, -0.1);
b_sol = sol(1)
y_fit = f(b_sol, x);
figure
plot(x,y,'r',x,y_fit,'-.k');
legend('data','exp fit');

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

 채택된 답변

Matt J
Matt J 2021년 2월 25일
편집: Matt J 2021년 2월 25일

0 개 추천

When x goes to zero, y should go to zero as well since anything power negative infinity is zero.
Only if b>=0.
g = fittype('exp(-b/x)', 'options', fitoptions('Lower',0) );

댓글 수: 5

Basil Eldeeb
Basil Eldeeb 2021년 2월 26일
편집: Basil Eldeeb 2021년 2월 26일
Hello, Thanks for the response. I take it matlab applies limit evaluation on its own. However, the code is giving me this error:
Error using curvefit.basefitoptions/set
The name 'Lower' is not an accessible
property for an instance of class
'basefitoptions'.
now I have two questions:
Why am I receiving this error?
How do I specify which parameter is to be limited if I have more than one?
I guess they wanted a different syntax...
data=[0.000000 -4.333102E-5
0.050000 0.030123
0.100000 0.061024
0.150000 0.092435
0.200000 0.123947
0.250000 0.155877
0.300000 0.187916
0.350000 0.219968
0.400000 0.251753
0.450000 0.283991
0.500000 0.316017
0.550000 0.347719
0.600000 0.379196
0.650000 0.410459
0.700000 0.441492
0.750000 0.472394
0.800000 0.503174
0.850000 0.533827
0.900000 0.564569
0.950000 0.595374
1.000000 0.626224];
vv=data(:,1);
ii=data(:,2);
g = fittype('exp(-b/x)');
options=fitoptions('exp(-b/x)','Lower',0);
f0 = fit(vv,ii,g,options);
Warning: Start point not provided, choosing random start point.
plot(f0,vv,ii)
grid('on')
Basil Eldeeb
Basil Eldeeb 2021년 3월 1일
Thank you for the response. I wanted to ask how to specify multiple options for multiple parameters, for example:
g = fittype('a*x^2*exp(-b/x)');
One more question; For my model the exp(-b/x) is only valid for positive x. So is there a way where I can apply two fittings simultaneously to the same data? i.e. one including exp(-b/x) as a multiple for +ve x and excludes it for -ve x. Thanks in advance!
Matt J
Matt J 2021년 3월 1일
편집: Matt J 2021년 3월 1일
You should divide the data into two sets and fit each one separately, e.g.,
pos=vv>0; neg=~pos;
fpos = fit(vv(pos),ii(pos),gpos,options);
fneg = fit(vv(neg),ii(neg),gneg,options);
Basil Eldeeb
Basil Eldeeb 2021년 3월 2일
I appreciate it, thank you for the help!.

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

추가 답변 (0개)

카테고리

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

질문:

2021년 2월 25일

댓글:

2021년 3월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by