How do I use the curve fitting tool with a custom function that does not depend of x?

5 Ansichten (letzte 30 Tage)
I always get an error in the toolbox when I try to fit to a Matlab function I have. I have a set of points to which I want to fit some data. Problem is, the points are, for example, for x=1.4, 2.5, 4.1,... , and the data I have is for x=1, 2, 3,... , yet the way this data is calculated does not dependent of the value of x, even though each individual data point is assigned to x=1, 2, 3,.... I think that the fact that it does not depend of x must be the problem, since when I use this function and just assign some values to the parameters f1,f2,f3,f4,f5,a,b,c,d I obtain a fit (though wrong unless I am able to manually fit the parameters).
If I just use fit(f1,f2,f3,f4,f5,a,b,c,d) as the custom equation I get the error:
"The independent variable x does not appear in the equation expression."
And I use fit(f1,f2,f3,f4,f5,a,b,c,d,x) I get:
"Expression fit(f1,f2,f3,f4,f5,a,b,c,d,x) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated"
What could be the problem? The function I have is as follows:
function f=fit(f1,f2,f3,f4,f5,a,b,c,d)
x=(1:(size(stopProb,2)-1))';
sum1=1:(size(stopProb,2)-1);
sum2=1:(size(stopProb,2)-1);
sum3=1:(size(stopProb,2)-1);
sum4=1:(size(stopProb,2)-1);
sum5=1:(size(stopProb,2)-1);
for i=2:size(stopProb,2)
sum1(i-1)=sum(stopProb(1:a,i));
sum2(i-1)=sum(stopProb(a+1:b,i));
sum3(i-1)=sum(stopProb(b+1:c,i));
sum4(i-1)=sum(stopProb(c+1:d,i));
sum5(i-1)=sum(stopProb(d+1:size(stopProb,1),i));
end
v=[sum1;sum2;sum3;sum4;sum5];
sumTotal=v';
probInterval=zeros(size(sumTotal,1),2);
f_dia_fit=zeros(size(sumTotal,1),1);
for u=1:size(sumTotal,1)
probInterval(u,1)=sumTotal(u,1)/(sumTotal(u,1)+sumTotal(u,2)+sumTotal(u,3)+sumTotal(u,4)+sumTotal(u,5));
probInterval(u,2)=sumTotal(u,2)/(sumTotal(u,1)+sumTotal(u,2)+sumTotal(u,3)+sumTotal(u,4)+sumTotal(u,5));
probInterval(u,3)=sumTotal(u,3)/(sumTotal(u,1)+sumTotal(u,2)+sumTotal(u,3)+sumTotal(u,4)+sumTotal(u,5));
probInterval(u,4)=sumTotal(u,4)/(sumTotal(u,1)+sumTotal(u,2)+sumTotal(u,3)+sumTotal(u,4)+sumTotal(u,5));
probInterval(u,5)=sumTotal(u,5)/(sumTotal(u,1)+sumTotal(u,2)+sumTotal(u,3)+sumTotal(u,4)+sumTotal(u,5));
f_dia_fit(u,1)=probInterval(u,1)*f1+probInterval(u,2)*f2+probInterval(u,3)*f3+probInterval(u,4)*f4+probInterval(u,5)*f5;
end
f=f_dia_fit;
  4 Kommentare
André Simões
André Simões am 23 Mär. 2019
Bearbeitet: André Simões am 23 Mär. 2019
You just made me realize that I did not express myself well. What I meant is that to calculate the values of y of this data are not calculated using a x value, like for example a linear regression (y=mx+b), yet they do correspond to some x values (x=1, 2, 3,...). which is why I write
x=(1:(size(stopProb,2)-1))
in the function.
Walter Roberson
Walter Roberson am 24 Mär. 2019
Think of using t as the independent variable if that helps.
The first time step, t=1, you get a value which happens to be stored in y(1)
The second time step, t=2, (a fixed interval later), you get a value which happens to be stored in y(2)
The third time step, t=3, (the fixed interval after the second), you get a value which happens to be stored in y(3)
and so on.
It does not matter that you have no literal 'x' or 't' in your sequence of values: it only matters that they represent function values at consistent step size. In such a case, you can use the index as your independent variable. x = 1 : length(y) is fine.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

André Simões
André Simões am 23 Mär. 2019
Bearbeitet: André Simões am 23 Mär. 2019
There is no red text since I am using the Curve Fitting Tool. I have changed the function name. The errors I get are those I described.
If I just use metodo_fit_v3(f1,f2,f3,f4,f5,a,b,c,d) as the custom equation I get the error:
"The independent variable x does not appear in the equation expression.
Use x in the expression or indicate another variable as the independent variable."
If I use metodo_fit_v3(f1,f2,f3,f4,f5,a,b,c,d,x) I get:
"Expression metodo_fit_v3(f1,f2,f3,f4,f5,a,b,c,d,x) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> metodo_fit_v3(f1,f2,f3,f4,f5,a,b,c,d,x)
??? Undefined function 'metodo_fit_v3' for input arguments of type 'double'."

Produkte


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by