i have this code
x = 1 +((riskf_end - riskf_start)/riskf_step);
y = 1 +((volatility_end - volatility_start)/volatility_step);
A = zeros(1 + x, 1 + y);
x and y are integers but the output of y is in exp notation and the 'zeros' command takes y as non integer. i can't figure this out.I tried 'format long g' but again the matrix does not recognize y as an integer.
ex.
riskf_end = 0.04
riskf_start = 0
riskf_step = 0.001
volatility_start = 0.13
volatility_step = 0.0001
volatility_end = 0.25
when putting volatility_step = 0.001 the problem disappears, but for smaller steps it apears again.x and y are always integers as the step will always be smaller than the boundaries. I would appriciate any help.Thanks in advance.

 채택된 답변

Mischa Kim
Mischa Kim 2016년 9월 9일

1 개 추천

Why not simply round the numbers, e.g.
A = zeros(1 + round(x), 1 + round(y));

댓글 수: 4

kostas zervos
kostas zervos 2016년 9월 9일
편집: kostas zervos 2016년 9월 9일
i can't round the numbers because the price of the option will be way off.as i mentioned the x and y are integers,you will see it if do the math.in option pricing every number counts,even after the decimical point.Thanks for the answer though.The inputs are percentages e.g. 0.25=25%,except the steps
That is the thing. y is not always an integer. E.g. for y = 0.00001
y =
1.200100000000000e+04
and
y - 1.200100000000000e+04
ans =
-1.818989403545857e-12
Equations that equate to zero symbolically, do not necessarily equate to zero numerically.
Guillaume
Guillaume 2016년 9월 9일
편집: Guillaume 2016년 9월 9일
@Kostas, I think you misunderstood Mischa. He's telling you to round the inputs you give to the zeros function (so x and y), not your original values ( the riskf_* and volatility_* which obviously have to stay what they are).
The problem you are encountering is that, even if the numbers are symbolically integer, since computers can only store an approximate representation of most numbers, you have small rounding errors that means the result of calculation is often not exactly integer. As point out by Mischa, your y is 0.00000000000181... off from the integer 12001.
You can't avoid that, so if the result is meant to be integer, simply round it to integer.
Note that playing with format will have absolutely no effect on the actual value of the number. format only changes the way matlab displays numbers to you, not how they are stored in memory.
kostas zervos
kostas zervos 2016년 9월 9일
I think i found a solution.Instead of dividing end-start with step,i do it seperately, end/step - start/step.The problem disappeared and the outputs are presented as integers.Thanks for the answers guys,i appriciate it.If the problem reappears i will try rounding the numbers.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2016년 9월 9일

댓글:

2016년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by