From: Valentin on
Im trying to fit the svensson model to yield curve data.

First, i defined the function to be calibrated (6 parametes):


function F = calsvensson(x,xdata)
F = x(1)+(x(2)*(1-exp(-xdata/x(5)))*(x(5)/xdata))+(x(3)*((x(5)/xdata)*(1-exp(-xdata/x(5)))-exp(-xdata/x(5))))+(x(4)*((x(6)/xdata)*(1-exp(-xdata/x(6)))-exp(-xdata/x(6))));

Second, I call the optimization for a sample data:

x0= [0.384 3.785 -4.567 -0.347 0.595 6.004];

ydata=[3.88 3.95 4.25 3.10 3.49 3.75 3.53 4.00 3.97 3.79 3.89 4.09 4.14 4.17 4.65]; %yield

xdata=[8.17 3.75 6.75 6.58 9.25 18.00 9.58 18.17 9.92 6.75 10.75 18.75 23.75 28.75 24.75]; %maturity

[x,resnorm] = lsqcurvefit(@calsvensson,x0,xdata,ydata)

but then, I got the following message:

Error using ==> mrdivide
Matrix dimensions must agree.

Error in ==> calsvensson at 13
F =
x(1)+(x(2)*(1-exp(-xdata/x(5)))*(x(5)/xdata))+(x(3)*((x(5)/xdata)*(1-exp(-xdata/x(5)))-exp(-xdata/x(5))))+(x(4)*((x(6)/xdata)*(1-exp(-xdata/x(6)))-exp(-xdata/x(6))));

Error in ==> lsqcurvefit at 209
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});

Error in ==> svenssonfit at 9
[x,resnorm] = lsqcurvefit(@calsvensson,x0,xdata,ydata)
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.

I tried changing for a simpler function like the one that appears in the Matlab help and it worked but with this complex one it did not work. Could anybody give me some advice? Thanks.
From: Paul Kerr-Delworth on
"Valentin " <vdelano(a)larrainvial.com> wrote in message <hr7g1j$hlj$1(a)fred.mathworks.com>...
> Im trying to fit the svensson model to yield curve data.
>
> First, i defined the function to be calibrated (6 parametes):
>
>
> function F = calsvensson(x,xdata)
> F = x(1)+(x(2)*(1-exp(-xdata/x(5)))*(x(5)/xdata))+(x(3)*((x(5)/xdata)*(1-exp(-xdata/x(5)))-exp(-xdata/x(5))))+(x(4)*((x(6)/xdata)*(1-exp(-xdata/x(6)))-exp(-xdata/x(6))));
>
> Second, I call the optimization for a sample data:
>
> x0= [0.384 3.785 -4.567 -0.347 0.595 6.004];
>
> ydata=[3.88 3.95 4.25 3.10 3.49 3.75 3.53 4.00 3.97 3.79 3.89 4.09 4.14 4.17 4.65]; %yield
>
> xdata=[8.17 3.75 6.75 6.58 9.25 18.00 9.58 18.17 9.92 6.75 10.75 18.75 23.75 28.75 24.75]; %maturity
>
> [x,resnorm] = lsqcurvefit(@calsvensson,x0,xdata,ydata)
>
> but then, I got the following message:
>
> Error using ==> mrdivide
> Matrix dimensions must agree.
>
> Error in ==> calsvensson at 13
> F =
> x(1)+(x(2)*(1-exp(-xdata/x(5)))*(x(5)/xdata))+(x(3)*((x(5)/xdata)*(1-exp(-xdata/x(5)))-exp(-xdata/x(5))))+(x(4)*((x(6)/xdata)*(1-exp(-xdata/x(6)))-exp(-xdata/x(6))));
>
> Error in ==> lsqcurvefit at 209
> initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
>
> Error in ==> svenssonfit at 9
> [x,resnorm] = lsqcurvefit(@calsvensson,x0,xdata,ydata)
> Caused by:
> Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
>
> I tried changing for a simpler function like the one that appears in the Matlab help and it worked but with this complex one it did not work. Could anybody give me some advice? Thanks.

Hi,

The error you are receiving is from your objective function. You can also see this error if you run

calsvensson(x0,xdata)

I think your objective function needs to use array right division (./) and array multiplication (.*) rather than just right division (/) and multiplication (*). If you replace your function with the following

function F = calsvensson(x,xdata)
F = x(1)+(x(2)*(1-exp(-xdata./x(5))).*(x(5)./xdata))+(x(3)*((x(5)./xdata).*(1-exp(-xdata./x(5)))-exp(-xdata./x(5))))+(x(4)*((x(6)./xdata).*(1-exp(-xdata./x(6)))-exp(-xdata./x(6))));

your call to lsqcurvefit will run. For more information on these operators, see

<<http://www.mathworks.com/access/helpdesk/help/techdoc/ref/arithmeticoperators.html>>

However, when I tried this I found that lsqcurvefit was not converging to a solution. To have a quick look why this could be, I plotted your data

plot(xdata, ydata, 'o')

The data appears to be fairly noisy, or perhaps has no real trend. As one suggestion,you could check your data to see if these really are the points you wish to fit.

Hope this helps.

Best regards,

Paul