Re: Matlab: code needs slightly fixing.



On Nov 16, 5:20 pm, Sunny...@xxxxxxxxxxxx wrote:
Hi

I found that this is the part of my code that is giving me problems:

M=100

for i = M:-1:1

for n = 1:i

W(n) = exp(-r*dt)*(u*W(n+1) + m*W(n) + d*W(n-1));

end
end

And the output i need is W(1)

I keep getting the error:

??? Subscript indices must either be real positive integers or
logicals.

How should I change the subscripts? n

Thank you.

I am no Matlab expert, but I see two issues.

1. You are calculating w(n) - but it needs w(n+1). How would it do
that?

Recommend changing subscripts w(n-1) = ... (u*w(n) + m*w(n-1)+
d*w(n-2)). Of course look at comment 2 below. You need to define
initial condition w(-1) and w(-2)

2. You are calculating w(n), but need w(n-1). At n = 1 - this means
you need w(0).

Where have you defined that? Recommend defining an initial condition
to start this off.

My 2 cents.
.


Quantcast