Computation to a matrix iteration not computing the rest of the steps

2 views (last 30 days)
Hello Everyone, I am fairly new to programming and I am attempting to iterate through a matrix that I succeffully computed (not shown here so it doesnt become too long)
cpot2 = zeros(size(10))
%j = 0
%fcomp = (p*copt3(1,j+1))+(q*copt3(1,j+2))*exp(r*dt)
for j = 0
for s = 1
cpot2 = (p*cpot3(j+1,1))+(q*cpot3(s+1,1))*exp(r*dt)
end
end
Basically it should do 2 more computations, one of which, the last iteration which should be substituted into cpot2, equal to zero. Unfortunately I am only getting the first result of my computation
Any help is appreciated!
  4 Comments
Mahmoud Galal
Mahmoud Galal on 22 May 2022
Thanks for the help, i managed to figure it out, I posted the solution below. It seemed that I had a misunderstanding where I was not aware that MATLAB treated vectors differently to matrices.
I am posting the answer to my query below.

Sign in to comment.

Accepted Answer

Mahmoud Galal
Mahmoud Galal on 22 May 2022
This is the answer:
cpot2 = [0;0;0]
%j = 0
%fcomp = (p*copt3(1,j+1))+(q*copt3(1,j+2))*exp(r*dt)
for j = 1:3
cpot2(j) = (p*cpot3(j)+q*cpot3(j+1))*disc
end
%Computation of cpot 1
cpot1 = [0;0]
for j = 1:2
cpot1(j) = (p*cpot2(j)+q*cpot2(j+1))*disc
end
cpot0 = 0
cpot0 = (p*cpot1(1)+q*cpot1(2))*disc
  2 Comments
Mahmoud Galal
Mahmoud Galal on 22 May 2022
Thanks for the comment, will make sure to remember this. I am still trying to learn this language.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!