Computation to a matrix iteration not computing the rest of the steps
Show older comments
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
Jeffrey Clark
on 21 May 2022
Each for as you have coded will only do one iteration, see documentation: for loop to repeat specified number of times - MATLAB for (mathworks.com)
Mahmoud Galal
on 21 May 2022
zeros(size(10))
doesn't do what you think it does/want...try it at command line and see. And 10 isn't the size you need anyways, your loop goes over six values in each dimension, but you reference j+1 and s+1 so the sizes will end up at 6 and 7 .
It's not at all clear what you're trying to do here...where does the idea about "3 iterations" come from? There's no "3" in sight over the loop indices.
As written the above for each iteration over j is the equivlent of
cpot2=(p*cpot3(1)+q*sum(cpot3(2:7)))*exp(r*dt); % j=0
cpot2=(p*cpot3(2)+q*sum(cpot3(2:7)))*exp(r*dt); % j=1
...
cpot2=(p*cpot3(6)+q*sum(cpot3(2:7)))*exp(r*dt); % j=5
I'm guessing this is probably not what you're intending, but we have no way to guess what that might be.
Mahmoud Galal
on 22 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!