Fitting an integral

조회 수: 1 (최근 30일)
deniz
deniz 2011년 4월 23일
I'm going to explain my problem with a sample. Assume that i have a function (f(x)=a.*x^2) and i need to calculate its integral indefinitely, lets assume this function doesn't have an indefinite integral. At the end i need to fit this integral to a data set. So I thought this could be achieved by quad function.
ftyp1 = fittype('quad(@(x) a.*x.^2,0,x)','options',ops1,'independent',{'x'},'coefficients',{'a'})
But in this example Matlab gives error. If i create another function to calculate quad function, like this :
function [c ] = myfunction( b ,a)
if length(a)==1, a = a(ones(size(b))); end
if length(b)==1, b = b(ones(size(a))); end
c=NaN(length(b),1);
parfor i=1:length(b)
c(i)=quad(@(x) a(i).*x.^2,0,b(i));
end
end
ftyp1 = fittype('myfunction(x,a)','options',ops1,'independent',{'x'},'coefficients',{'a'})
With using the sample code above, Matlab could find the coefficients. The trick is making it calculate quad function in another function as you can see.So my question is this : how could i implement quad function directly to fittype function without creating extra functions?

답변 (3개)

Andrew Newell
Andrew Newell 2011년 4월 23일
If your expression is a linear function of the coefficients, as in your example, you could do something like this:
ftyp1 = fittype('@(x) a.*quad(x.^2,0,x)','independent',{'x'},'coefficients',{'a'})
  댓글 수: 2
deniz
deniz 2011년 4월 24일
Thanks for the answer but it didn't work. It started as it would work as it states that 'Warning: Start point not provided, choosing random start point'. But after that it gave me this: ??? Undefined function or method 'isnan' for input arguments of type 'function_handle'.
I also tried this : char(inline('@(x) quad( a.*x^2,0,x)')) and it gave me same error. I also remind you that if there is another way or another function to fit (fitting quad), i would be happy to try that.
Andrew Newell
Andrew Newell 2011년 4월 24일
I confess I only went as far as creating a fittype object and did not try to fit anything.

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


Andrew Newell
Andrew Newell 2011년 4월 24일
I think the heart of the problem is that quad only accepts scalars for the lower and upper bounds, so it cannot deal with the vector x. I tried using arrayfun, but it does not accept function handles. Also, fittype does not accept function handles, so solutions involving nested functions are out. Your myfunction is probably the best approach.
  댓글 수: 1
deniz
deniz 2011년 4월 25일
Thanks anyway

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


Giovanni Ughi
Giovanni Ughi 2011년 10월 17일
Useful topic. In case I have to fit a model like this: model = b.*x .* quad(a.*x.^2) with a,b parameters and x independent variable?
apparently is not possible to write something like this
for i:1:length(b)
c(i) = @(x) b(i).*x .* quad(@(x) a(i).*x^2,0,d(i))
end
any suggestion?
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 10월 17일
c{i} = @(x) b(i).*x .* quad(@(x) a(i).*x^2,0,d(i));
Function handles can not be stored as elements of regular arrays, but can be stored as elements of cell arrays.
What you are going to _do_ with those stored function handles, I do not know.

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

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by