lsqcurvefit for function with exp in numerator and denominator

조회 수: 2 (최근 30일)
Yagnaseni Roy
Yagnaseni Roy 2015년 12월 30일
댓글: Yagnaseni Roy 2015년 12월 31일
I've played around with lsqcurvefit to have confidence in my use of it. However the one instance that it doesn't work for me is when my function contains an exponential in both the numerator and denominator. Could someone explain why this is happening and how to fix it?
Example:
Lets's say I know the function I'm looking for is f(v)=exp(3v)/(1+exp(3v))
Now I give MATLAB:
fun=@(x,xdata)=exp(x*xdata)/(1+exp(x*xdata))
xdata=[0.01;0.02;0.03;0.04;0.05]
ydata=[0.507499438;0.514995502;0.522484825;0.529964052;0.537429845]
x0=2;
I expect that by using
x = lsqcurvefit(fun,x0,xdata,ydata)
It should give me x=3. However I get the error:
Function value and YDATA sizes are incommensurate.
It works fine if the function is simply f(v)=exp(3v) though.

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 30일
fun=@(x,xdata)=exp(x*xdata)./(1+exp(x*xdata))
  댓글 수: 3
Matt J
Matt J 2015년 12월 30일
편집: Matt J 2015년 12월 30일
Lsqcurvefit is repeatedly making function calls of the form fun(x,xdata), not fun(x,xdata(i)). So, your code for fun() has to know how to handle xdata input in vector form, which is what Walter's change accomplishes.
One thing to be aware of is that xdata and ydata are allowed to be arrays of completely different dimensions. Similarly, there is no assumption in lsqcurvefit that each ydata(i) is a transformation of only one single corresponding xdata(i), like in your example. The only requirement that lsqcurvefit makes of F(x,xdata) is that its output is of the same dimensions as ydata and that it is a differentiable function of x. Accordingly, there is nothing helpful that lsqcurvefit could achieve by calling fun() with one xdata(i) element at a time.

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

추가 답변 (0개)

카테고리

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