- #1
xuej1112
- 18
- 0
who can help me to run this code in matlab? I run it in my computer but the result is totally different from the book! In the book,the image is a increase curve,but my result is a decrease curve.So, is there something wrong of my MATLAB?
code:
syms r P A B t;
P = exp(A+B*r);
disp('first derivative of P with respect to r')
diff(P,r)
disp('second derivative of P with respect to r')
diff(diff(P,r))
syms f B A s k m p r t DA DB
f1=0.5*B^2*s^2+B*(k*(m-r)-p*s)-DA-DB*r-r
collect(f1,r)
dsolve('-B*k-DB-1=0','B(0)=0')
B = -1/k+exp(-k*t)/k;
f2=1/2*B^2*s^2+B*(k*m-p*s)-DA
dsolve('1/2*(-1/k+exp(-k*t)/k)^2*s^2+(-1/k+exp(-k*t)/k)*(k*m-p*s)-DA=0','A(0)=0')
tau=(1:1:60)';
nTau=length(tau);
p=0.20;s=0.25;k=0.30;theta=5;r=6;
% Calculate A, B and the yield curve;
B=(exp(-k.*tau)-1)./k;
A=B.*(((p*s)/k)-theta)-theta.*tau+(p.*s.*tau)./k+(s^2/(4*k)).*(-(B.^2)+(2*exp(-k.*tau)-2)./(k^2)+(2.*tau)./k);
y=-(1./tau).*(A+B.*r);
figure;
plot(tau,y)
code:
syms r P A B t;
P = exp(A+B*r);
disp('first derivative of P with respect to r')
diff(P,r)
disp('second derivative of P with respect to r')
diff(diff(P,r))
syms f B A s k m p r t DA DB
f1=0.5*B^2*s^2+B*(k*(m-r)-p*s)-DA-DB*r-r
collect(f1,r)
dsolve('-B*k-DB-1=0','B(0)=0')
B = -1/k+exp(-k*t)/k;
f2=1/2*B^2*s^2+B*(k*m-p*s)-DA
dsolve('1/2*(-1/k+exp(-k*t)/k)^2*s^2+(-1/k+exp(-k*t)/k)*(k*m-p*s)-DA=0','A(0)=0')
tau=(1:1:60)';
nTau=length(tau);
p=0.20;s=0.25;k=0.30;theta=5;r=6;
% Calculate A, B and the yield curve;
B=(exp(-k.*tau)-1)./k;
A=B.*(((p*s)/k)-theta)-theta.*tau+(p.*s.*tau)./k+(s^2/(4*k)).*(-(B.^2)+(2*exp(-k.*tau)-2)./(k^2)+(2.*tau)./k);
y=-(1./tau).*(A+B.*r);
figure;
plot(tau,y)