How to plot the bond price; Error incorrect dimensions

1 view (last 30 days)
Hi, I'm trying to plot a bond price over the yield curve but I keep getting an error: Screenshot (35).png
Here is my code:
fighandle = figure(1);
fighandle.Position(3) = 800;
axeshandle = axes;
axeshandle.YLim = [0 inf];
x = 1:0.1:7;
yield = ones(1,50) * x; % yield curve
T = length(yield);
r=0.03; % initial yield rate
Par=1000; % par value
coupon=r.*Par; % coupon payments
cc=zeros(1,T)+coupon; % vector of cash flows
cc(50)=cc(50)+Par; % add par to cash flows
P = sum(cc./((1+yield./100).^(1:T))) % calculate bond price
plothandle = plot(axeshandle,x,P)
Any help/suggestion would be appreciated. Thanks
  7 Comments
Ruchir Dhiman
Ruchir Dhiman on 17 Jul 2019
Edited: Ruchir Dhiman on 17 Jul 2019
All operations on matrices require the matrix dimensions to be compatible.
I would suggest that, before plotting the results, just write the dimensions of the matrices which you are using to represent your data. And make sure that the dimensions of any auxiliary matrices you create are compatible.
For reference, you cannon multiply (M1 * M2) a matrix with dimensions m x n (m rows, n columns) with a matrix with dimension a x b (a rows, b columns) if n is not equal to a.
Similarly, if you are using .* or ./ on 2 matrices, then the dimensions should be equal i.e m=a and n=b. Otherwise you will get another error.
Edit - You mentioned that you just picked 0.1 randomly.
Try using x = linspace(1,7,50) instead of x = 1:0.1:7
It will give you 50 equally-spaced elements between 1 and 7 (both inclusive).

Sign in to comment.

Answers (0)

Categories

Find more on Price and Analyze Financial Instruments in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!