Combining fsolve and lsqcurvefit

Good evening,
I have a collection of experimental data (xi, yi) called (texp, rexp). I know that 'texp' must be derived from 'rexp' following:
t(i)=k(1)*integral(@(x) exp(-k(2)./(x.*log(x))), 1, r(i))
, being k(1) and k(2) parameters. So 'r(i)' is the upper integration limit. I need to find the values of k(1) and k(2) that best fits my model. My strategy is solving the equations 't(i)-texp=0' in 'r' with fsolve and fitting k(1) and k(2) with lsqcurvefit. I am trying this:
rteor=@(k,r) fsolve(@(r) arrayfun(@(T) k(1).*integral(@(x) exp(-k(2)./(x.*log(x))), 1, r)-T, texp), 1.0001);
x0=[2,6.12750];
k = lsqcurvefit(rteor, x0, texp, rexp)
which results on the following errors:
Error using lsqcurvefit (line 251)
Function value and YDATA sizes are not equal.
Thank you for your help!

6 Comments

Matt J
Matt J on 6 Jun 2018
Edited: Matt J on 6 Jun 2018
Well, what is the size of your YDATA and what is the size of rteor(x0)? Are they the same?
My YDATA ('rexp') is an 1x20 array, but rteor(x0) is a single 1x1 number (using arbitrary values k(1)=k(2)=7 and initial point = 50):
fsolve(@(r) 7.*integral(@(x) exp(-7./(x.*log(x))), 1, r)-0.00001, 50)
I've also tried to overcome this problem using fsolve for 2 variables, 'r' and 'k':
rteor=@(k,r) fsolve(@(k, r) arrayfun(@(T) k(1).*integral(@(x) exp(-FC(2)./(x.*log(x))), 1, r)-T, texp), 1.0001, 2)
x0=[2,6.12750];
FC = lsqcurvefit(rteor, x0, texp, rexp)
, resulting:
Field assignment to a non-structure array object.
(thanks !!)
lsqcurvefit is fussy about rows vs columns. Make sure you data is all columns, including the texp that you have in defining rteor
ok, i have checked them up, they both are 1x20 arrays...
Maybe introducting the initial points of fsolve also like 1x20 arrays? I have tried this:
initialpts=ones(1,20); x0=[2, 6.12750];
rteor=@(k, r) arrayfun(@(p) fsolve(@(r) arrayfun(@(T) k(1).*integral(@(x) exp(-k(2)./(x.*log(x))), 1, r)-T, texp), p), initialpts);
k = lsqcurvefit(rteor, x0, texp, rexp)
and it gives an orange-color error repeated a lot of times:
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm
instead.
> In fsolve (line 298)
In @(p)fsolve(@(r)arrayfun(@(T)FC(1).*integral(@(x)exp(-FC(2)./(x.*log(x))),1,r)-T,texp),p)
In @(FC,r)arrayfun(@(p)fsolve(@(r)arrayfun(@(T)FC(1).*integral(@(x)exp(-FC(2)./(x.*log(x))),1,r)-T,texp),p),intialpts)
In lsqcurvefit (line 202)
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.
<stopping criteria details>
, and finally gives the initial values not varied:
k =
2.0000 6.1275
I feel I am near but cannot find the mistake...
You can get rid of the warning by passing an options structure to fsolve to tell it to use Levenberg-Marquardt
However, that suggests that multiple equations are attempted to be solved simultaneously, which is likely an error at some point.
Hmmm... notice that lsqcurvefit is going to be passing vectors to the function, so you need to expect that r will be a vector. When I answered you in https://www.mathworks.com/matlabcentral/answers/403892-fitting-data-to-integral-function#comment_574988 I was careful to use different variable names, R vs r, to make it clear whether one was receiving a vector or scalar at that point.
well, taking your comment into account, I have made some advances. I changed the name of the variable in fsolve, with no effect. But i have changed the initial points to 8:
inpts=[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8];x0=[10 8];
rteor=@(k, r) arrayfun(@(p) fsolve(@(r) arrayfun(@(T) k(1).*integral(@(x) exp(-k(2)./(x.*log(x))), 1, r)-T, texp), p), inpts)
k = lsqcurvefit(rteor, x0, texp, rexp)
and matlab gives a result (after new 20-orange Levenberg-Marquardt warning messages):
k =
8.8155 9.8131
I am hopeful, but when trying to plot rteor with this values using
plot(texp,rexp,'ko',texp,rteor(k,texp),'x');
, 'rteor' appers as an horizontal line... Am i wrong now in 'plot' ?
(thanks !!!)

Sign in to comment.

Answers (0)

Asked:

on 6 Jun 2018

Edited:

on 7 Jun 2018

Community Treasure Hunt

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

Start Hunting!