# Fit of numerical expression

2 views (last 30 days)
Moritz Ruhwedel on 13 Aug 2019
Commented: Moritz Ruhwedel on 14 Aug 2019
Hi all, i have my data which i want to be fitted consisting of the independent variable x and the dependent variable y. My function that should fit the data is something like f(x,a,b,c,d) = integral(g(x,y,a,b,c),y,0,d) where d is known and a, b and c are the fitting parameters. g can only be integrated numericaly. If i try to do the fit using ft = fittype('f(x,a,b,c,d)') i get an error like: Expression f(x,a,b,c,d) is not a valid MATLAB expression,
has non-scalar coefficients, or cannot be evaluated
Any ideas how i can solve this problem?
Edit: If i try to compute values for f for certain sets of variables i get: Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is
4.0e-01. and a probably right value for f, but this value is much smaller than the suggested error. g is definitly finite over the range i integrate. How reliable could this value be and how could i manage to get an smaller error?

Torsten on 13 Aug 2019
Use an anonymous function to define your model equation.
For examples, see

Moritz Ruhwedel on 14 Aug 2019
Okay thank you, but know that i tried to do this with my real model i get:
>> ft = fittype(@(d_Fe, r_bulk_Pt, r_Fe, mfp_Pt, h, k, x) ((x/((x)/(1e-2) * integral(@(t) (mfp_Pt.*r_bulk_Pt)./((x+ h.*sin(k.*t)).*(mfp_Pt./(x+ h.*sin(k.*t)) - (3.*mfp_Pt.^2)./(8.*(x+ h.*sin(k.*t)).^2) + (3.*mfp_Pt.^2.*(((x+ h.*sin(k.*t)).*etp(-(x+ h.*sin(k.*t))./mfp_Pt))./(12.*mfp_Pt) - etp(-(x+ h.*sin(k.*t))./mfp_Pt).*((x+ h.*sin(k.*t)).^2./(24.*mfp_Pt.^2) - 1./2) - etp(-(x+ h.*sin(k.*t))./mfp_Pt)./4 + ((x+ h.*sin(k.*t)).^2.*ei(-(x+ h.*sin(k.*t))./mfp_Pt).*((x+ h.*sin(k.*t)).^2 - 12.*mfp_Pt.^2))./(24.*mfp_Pt.^4) + ((x+ h.*sin(k.*t)).*etp(-(x+ h.*sin(k.*t))./mfp_Pt).*((x+ h.*sin(k.*t)).^2 - 12.*mfp_Pt.^2))./(24.*mfp_Pt.^3)))./(2.*(x+ h.*sin(k.*t)).^2)))./(x+ h.*sin(k.*t)), 0, 1e-2, 'ArrayValued', 1)) + d_Fe/r_Fe)*1/(x + d_Fe))^(-1))
Error using fittype/testCustomModelEvaluation (line 12)
Expression
((x/((x)/(1e-2)*integral(@(t)(mfp_Pt.*r_bulk_Pt)./((x+h.*sin(k.*t)).*(mfp_Pt./(x+h.*sin(k.*t))-(3.*mfp_Pt.^2)./(8.*(x+h.*sin(k.*t)).^2)+(3.*mfp_Pt.^2.*(((x+h.*sin(k.*t)).*etp(-(x+h.*sin(k.*t))./mfp_Pt))./(12.*mfp_Pt)-etp(-(x+h.*sin(k.*t))./mfp_Pt).*((x+h.*sin(k.*t)).^2./(24.*mfp_Pt.^2)-1./2)-etp(-(x+h.*sin(k.*t))./mfp_Pt)./4+((x+h.*sin(k.*t)).^2.*ei(-(x+h.*sin(k.*t))./mfp_Pt).*((x+h.*sin(k.*t)).^2-12.*mfp_Pt.^2))./(24.*mfp_Pt.^4)+((x+h.*sin(k.*t)).*etp(-(x+h.*sin(k.*t))./mfp_Pt).*((x+h.*sin(k.*t)).^2-12.*mfp_Pt.^2))./(24.*mfp_Pt.^3)))./(2.*(x+h.*sin(k.*t)).^2)))./(x+h.*sin(k.*t)),0,1e-2,'ArrayValued',1))+d_Fe/r_Fe)*1/(x+d_Fe))^(-1)
is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error while trying to evaluate FITTYPE function :
Undefined function 'etp' for input arguments of type 'double'.
Error in fittype>iCreateFittype (line 373)
testCustomModelEvaluation( obj );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Caused by:
Error using fittype/evaluate (line 88)
Error while trying to evaluate FITTYPE function :
Undefined function 'etp' for input arguments of type 'double'.
Torsten on 14 Aug 2019
Don't define your function in the call to "fittype", but use a separate function file:
ft = fittype(@(d_Fe, r_bulk_Pt, r_Fe, mfp_Pt, h, k, x) fun(d_Fe,r_bulk_Pt,r_Fe,mfp_Pt,h,k,x));
and write a function
function res = fun(d_Fe,r_bulk_Pt,r_Fe,mfp_Pt,h,k,x)
...
end
This way, you have the possibility to debug what is happening.
One obvious thing is that x/((x)... is wrong because x is a vector. Use elementwise division here.
Moritz Ruhwedel on 14 Aug 2019
Okay now i found the problem, when replacing a variable called x i replaced accidently the x in exp with a t. Now it works, thank you Torsten, you saved my Bachelorthesis :D