lsqcurvefit for function with exp in numerator and denominator

2 次查看(过去 30 天)
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by