Question

Alrighty, I have run into a silly problem and both my own work and internet searching haven't really provided a solution, so I wanted to ask and see if anyone has any suggestions. I'm writing a Matlab program that is ultimately meant to draw interest rate curves, and this is done via using cubic B-splines and then an optimizer to obtain coefficients in the cubic B-spline basis, and using this information to graph. Now I've run into a tricky little problem when attempting to plot the graphs. I'll provide the error messages and relevant code below, but in words what's happening is this:

To program the B-splines for a given degree, you start with indicator functions and then build them recursively. This indicator is with respect to some input variable (for me that's time, but what the variable actually is isn't particularly relevant). When I go to graph with respect to this variable later on, using a range like, say t = (0:1:30), I get an error telling me that Operands to the || and && operators must be convertible to logical scalar values.

I did some internet searching and found that && doesn't work well with vector inputs (which I understand I would be passing into this while attempting to graph), and suggested using & instead. I tried that, and instead received an error saying that I had matrix dimensions that didn't agree, which didn't make sense to me since there never way any matrix involved in the first place. So at this point, I'm not sure what a good way to resolve this issue might be. Is there a vector robust way of programming indicator functions in matlab that would allow for plotting?

Here is the relevant code:

startvec = 0.007*ones(16,2);

%[x,fval] = fminsearch(@(x) FittingFunction(x, Rhat, today, startdays, enddays, fixedcoupondays, floatingcoupondays),startvec,optimset('Display','iter','MaxIter',50,'MaxFunEvals',50));

f0 = 0;
l0 = 0;
t = (0:1:30);

x = startvec;

for k=-3:1:12
    f0 = f0 + x(k+4,1).*Splines(3,k,t);
    l0 = l0 + x(k+4,2).*Splines(3,k,t);
end

h1 = figure('Name','OIS Curve','NumberTitle','off')
plot(t,f0);
title('OIS Curve as a Function of Time');
xlabel('time (years)');
ylabel('OIS Rate');
%hold off
print(h1,'-dpdf','OISCurve.pdf')

h2 = figure('Name','LIBOR Curve','NumberTitle','off')
plot(t,l0);
title('LIBOR Curve as a Function of Time');
xlabel('time (years)');
ylabel('LIBOR Rate');
%hold off
print(h2,'-dpdf','LIBORCurve.pdf')

The function Splines is written here:

function spline = Splines(n,k,t)

% The following code builds a B-spline function of any degree, using the
% recursive properties of B-splines.  Can be defined from k=-3 to k=12.

knots = [-1/4 -1/6 -1/12 0 1/12 1/4 1/2 3/4 1 3/2 2 4 7 11 21 31 41 51 61 71 81 91 101 111];

if (n==0)
    if (t>knots(k+4) && t<knots(k+5))
        spline = 1;
    else
        spline = 0;
    end
else
    spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);
end

The error message I received when using && was:

Operands to the || and && operators must be convertible to logical scalar values.

Error in Splines (line 9) if (t>knots(k+4) && t

Error in Splines (line 15) spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);

Error in Splines (line 15) spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);

Error in Splines (line 15) spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);

Error in InterestRatesHW1 (line 88) f0 = f0 + x(k+4,1).*Splines(3,k,t);

The error that I get when I use just & is:

Error using * Inner matrix dimensions must agree.

Error in Splines (line 15) spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);

Error in Splines (line 15) spline = ((t-knots(k+4))/(knots(k+4+n)-knots(k+4)))*Splines(n-1,k,t) + ((knots(k+5+n)-t)/(knots(k+5+n)-knots(k+5)))*Splines(n-1,k+1,t);

Error in InterestRatesHW1 (line 88) f0 = f0 + x(k+4,1).*Splines(3,k,t);

I'm sorry to ask what is I'm sure a basic question, but this is getting to be very frustrating, so any help is greatly appreciated.

Was it helpful?

Solution

You have to use a single & instead of the double && if you want to perform the and-operation on vectors. a & b will return a logical vector with a 1 for each element i where a(i) && b(i) == 1 and 0 where a(i) && b(i) == 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top