0

See the code

x(1) = 0;
k = 1;
   while x(k) <=6

x(k) = x(k) +2

k = k +1;

end

So I want the code to output

x(1) = 2

x(2) = 4

x(3) = 6

Basically I want to write out a list of x(i)s, that gets updated during each iteration. But I am getting an "out of bound" error and I realize that there is a conflict with the way MatLab sees "()" as a vector.

Anyway to get around this?

EDIT1

What if I want to do

x = 0;
y = 0;
k = 1;
   while x <=6 && y<=13

x = x +2; y = y + 3 +x;

k = k +1;

end

I want this to output

x = 2

y = 5

x = 4

y = 8

But it's not doing that, and I don't want to do this in two separate scripts. I want to handle everything at once.

Lemon
  • 12,664

1 Answers1

0

There's an error in your code, in the line

x(k)=x(k) + 2

because x(k) is not yet defined. See the correct code:

x(1) = 0;
k = 1;
   while x(k) <=6

   x(k+1) = x(k) +2

    k = k +1;

end
5xum
  • 123,496
  • 6
  • 128
  • 204