Writing for loop with a intial value given

I have 35 different values of udotreal I have initial value of u I want to write a for loop to get 35 values of u by doing this: New value of u = previous value + corresponding udotreal value. I was trying something like this:
for i = 1:35 u(1,i) = u(1,i) + udotreal(1,i);
end
Please help. Thanks in advanced

Answers (1)

Start your for-loop at 2, and use the previous value (i-1) to get the new one:
u(1) = ...
for i=2:35
u(i) = u(i-1) + udotreal(i) ; % or udotreal(i-1), I am not sure what you mean
end

3 Comments

This is what I want to do;
uk1 = uk + udotreal(1,1);
uk2 = uk1 + udotreal(1,2);
uk3 = uk2 + udotreal(1,3);
uk4 = uk3 + udotreal(1,4);
uk5 = uk4 + udotreal(1,5);
uk6 = uk5 + udotreal(1,6);
and so on...
do not create variables named like this! It is the contents of a variable that should be flexible, not its name.
The code above does what you suggest, storing all subsequent values into a single variable, but at subsequent locations, in the variable u. You have to define u(1) though.
Thank you. I got my code working using if else statement.
I have the variable named like that since I have more than 50-60 variables in the entire code and this is just a small part of the code. Hence, it seems to be a weird variable name.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 25 Oct 2017

Commented:

on 27 Oct 2017

Community Treasure Hunt

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

Start Hunting!