필터 지우기
필터 지우기

Running Newton Method for multiple different inputs

조회 수: 6 (최근 30일)
Jd45ter
Jd45ter 2022년 4월 28일
답변: Shreshth 2023년 11월 24일
I am currently trying to code for using Newtons Method.
My goal is that I have 30 prices (PV), each corresponds to a time in years. Such that the first PV of 98828.9817 is for the time t=1, and the next one is t=2, until t=30.
Each price has a YTM that needs to be calculated for it using newtons method.
For each price we have a set of payments C, for t=1 C = [104000], t =2 is [4000 104000], for up to t=30, as what Ive calculate in a for loop. The issue I am having is that it appears that I am not getting the correct output when i try to run Newtons method while attempting to loop through the prices. But im unsure where I have gone wrong. Any help is appreciated. Thanks
clear all;
format long;
global PV C t; %global variable
F = 100000; %Face value
it_max = 100; %Number of iterations
y0 = 0.10; %Starting estimate for YTM
tol = 10e-6; %Convergence tolerance
for i = 1:1:30
C = F * 0.04.*ones(i,1);
C(i) = C(i)+F;
for t = 1:1:length(C);
%YTM
[y, it] = newton_method(@func_bond, it_max, y0, tol);
fprintf('Price: $%0.4f YTM: %0.4f\n', PV(length(C)), y);
end
end
function [fy, fpy] = func_bond(y)
%Input: (y)
%Output: [fy, fpy] which are f(y) and f'(y)
global PV C t;
PV = [98828.9817; 97812.0511; 96937.8969; 96159.9962; 95269.2339;...
94353.5669; 93276.0334; 92237.8837; 91261.0455; 90214.0597;...
89083.9301; 87944.5962; 86976.3584; 85928.4188; 84982.5065;...
84248.2589; 83540.8304; 82911.5228; 82417.0923; 82009.0742;...
81633.1317; 81287.3700; 81087.7608; 80919.5136; 80780.7515;...
80669.7282; 80584.8196; 80524.5143; 80487.4060; 80472.1856];
fy = sum(C.*exp(-t.*y))-PV(length(C));
fpy = sum(-t.*C.*exp(-t.*y));
end
function [x, it] = newton_method(f, it_max, x0, tol)
%Input: f: function
% it_max: maximum number of iterations
% x0: initial guess
% tol: user defined tolerance
pre_x = x0;
disp(sprintf('n \t x_n \t |(x_n - x_{n-1})|'));
disp(sprintf('0 \t %2.10g', pre_x));
for it = 1:it_max
[fx, fpx] = f(pre_x);
new_x = pre_x - fx/fpx;
disp(sprintf('%d \t %2.10g \t %2.8g', it, new_x, abs(new_x - pre_x)));
if(abs(new_x - pre_x)<= tol)
break;
end
pre_x = new_x;
end
x = new_x;
end
  댓글 수: 2
Jan
Jan 2022년 4월 28일
"it appears that I am not getting the correct output" - this is an important detail. It is useful to share it with the readers: Why do you think so?
Jd45ter
Jd45ter 2022년 4월 28일
From what I’ve tested it I’m not getting the correct output as it looks like when I run the func_bond function I don’t think it it iterating over the time period of t=1:1length(C), I think instead it is just calculating for t=length(C)

댓글을 달려면 로그인하십시오.

답변 (1개)

Shreshth
Shreshth 2023년 11월 24일
Hello Jd45ter,
I understand that you are facing issue while looping through each bond price and implementing Newton’s method to find the YTM. While going through the code provided, I could spot some mistakes that can be avoided to make the function run efficiently.
  1. The t variable in the func_bond function should not be a global variable that gets overwritten in the loop. Instead, it should be an array that corresponds to the time periods for each cash flow.
  2. The loop structure is incorrect. You need to calculate the YTM for each price, and the inner loop should not be there. The t array should be constructed outside of the func_bond function
  3. The PV array should also not be a global variable inside func_bond. Instead, it should be passed as a parameter to the function.
  4. The fprintf statement prints the YTM for each price, but it's inside the loop that should not exist. It should be outside and after the Newton's method call.
By implementing the above corrections, you will be able to get the desired outputs.
To get a better understanding of global and local variables, please refer to the documentation below:
Thank you,
Shubham Shreshth.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by