Fitting data with custom function defined with recursion loop

Hi
I am trying to fit data with a function of this form: the function is defined on strictly positive values. In 0, the function is equal to 1-(the sum of the values it takes for x from 1 to 10)
for example, I tried with the function:
function[b]=Function2(x,a,c)
if x>0
b=a*(x+c);
elseif x<0
b=0;
else
y(1)=0;
for i=2:10
y(i)=y(i-1)+Function2(i,a,c);
end
b=1-y(10);
end
And used data defined so that I knew what a and c parameters to expect. The curve fitting tool replys to me that "Function value and YDATA sizes are not egal"
When I suppress the loop for x=0 that uses the function Function2 inside it's own expression, the curve fitting tool works (the function is then just a piecewise function), but then it gives a fitting curve that does not correspond at all to the piecewise function I expect.
How can I fit this function?
Thanks in advance
Bill

3 commentaires

I've read it 4 times and I still can't understand it. What does "defined on strictly positive values in 0" mean??? And I guess you're talking about element 1-10 of the output b, but when you say "1-(the sum of its positive values, from 1 to 10)", who's positive values are you talking about summing? a's, x's, or c's? And why is Function2 recursive - why is it calling itself?
Hi I've edited my question, it was unclear (mostly for punctuation reasons). What I meant was that the function is defined a certain way for x>0, and for x=0 the function is defined by the sum of some of the values it takes for x>0. c is just a parameter.
As a debugging step, inside the function please display
size(x), size(a), size(c)

Connectez-vous pour commenter.

Réponses (1)

Titus Edelhofer
Titus Edelhofer le 15 Mai 2015
Hi Bill,
I guess Curve Fitting tool will call your function with x being a vector and not a scalar. In this case your function will not work, since it's written for scalar x only. If length(x)>1 you should loop over the vector x.
Titus

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by