Matrix operation with for loop

Hi everyone,
I am trying to model a physics problem and I need to calculate the states of my object at each step. To do so I have to do a matrix multiplication as follow
x(i+1) = Ad*x(i)+Bu*u(i)+w
While,
x is 4x1,
Ad is 4x4 and constant,
x0 is my initial state,
Bu is 4x1 and constant,
u is 1x1,
and w is 4x1 and constant.
I wrote a for loop for matrix multiplication, but I have a problem with setting up my initial state vector in the loop. I really appreciate it if you could help me out. Thank you.

11 Comments

Are you sure that shouldn't be Ad*x instead of Ad*x(i)?
It should be an easy job......copy your code here......
Here is the code I have right now and I don't know where/how should I place the initial state. I greatly appreciate your help. Ad, Bu,w,x0, and Umin are Known from another part of my code.
for i = 1:N
u(i) = Umin(1,:);
x = zeros(4,1);
for i1 = 1:4
for i2 = 1:4
x(i1) = Ad(i1,i2)*x(i2)+Bu(i1)*u(i)+w(i1);
end;
end;
disp(x);
end
Sasha M
Sasha M on 25 Feb 2018
Edited: Sasha M on 25 Feb 2018
Yes, I am sure it's x(i). At each point, it's using the previous state to find the current state.
Study and run
x = nan( 1, 12 );
x0 = 17; % initial state
%
x(1) = x0;
for jj = 2 : length(x)
x(jj) = x(jj-1) + randi([-3,3]);
end
and inspect the result
>> x
x =
17 20 17 20 21 18 16 16 19 22 20 23
>>
Thank you for your response. I understand the code you wrote, but not sure how to apply it when my initial state is a vector and not scalar.
does x(i) represent the i'th index of x, or perhaps does it mean the whole x array at iteration or run #i? In other words, if you're running 10 iterations and you're calculating x at iteration #4
"x at iteration 4" = Ad*"x at iteration 3" + Bu*"u at iteration 3"+w
Otherwise x(4) depends on x(3), and x(3) depends on x(2), and x(2) depends only on x(1), so it doesn't matter if x starts out as a scalar or a vector. And what is x0? Do you mean the first set of 4 x values? Or do you mean the x value at the first x index, in other words x0 is really x(1)? Please explain more clearly.
"I understand the code you wrote" If so, how come
  • I cannot see x0 in your code
  • all your loops start from 1 and not 2
  • the rhs includes x(i2) rather than x(i2)-1
With a vector
x = nan( 4, 12 );
x0 = repmat( 17,[4,1]); % initial state
%
x(:,1) = x0;
for jj = 2 : size(x,2)
x(:,jj) = x(:,jj-1) + randi([-3,3],[4,1]);
end
inspect x
>> x
x =
17 20 18 20 18 17 20 19 19 19 17 15
17 20 18 16 14 16 15 15 17 17 19 20
17 17 19 22 23 24 26 23 26 23 22 20
17 14 12 11 11 11 13 10 7 6 6 7
>>
Yes, that's true, x(i) refers to the entire state of x (4x1) at i'th iteration. And yes, x0 is really my x(1). So my first for loop is counting each iteration and for x(i) and u(i), the second(i1) and third(i2) for loop are counting the index based on each row and column to do the matrix operation and finding all the 4 index of x(i). Is that a correct approach?
I assume that your code in the previous comment is supposed to be Matlab code.
  • "x(i) refers to the entire state of x (4x1) at i'th iteration." In Matlab code, if i is a scalar then x(i) is a scalar
  • "x0 is really my x(1)" However, x(1) is overwritten in the first iteration of the loop.
  • In your x(i1) = Ad(i1,i2)*x(i2)+Bu(i1)*u(i)+w(i1);, for every i1 the scalar, x(i1), is written once and then overwritten three times.
  • I'm lost
Actually, I wrote my code exactly like the example you provided and it worked! Thank you very much.
x = nan(4,N);
x0 = [5;10;0;10];
x(:,1) = x0;
for jj = 2 : size(x,2)
u(jj-1) = Umin(1,:)
x(:,jj) = Ad*x(:,jj-1)+Bu*u(jj-1)+w;
end

Sign in to comment.

Answers (0)

Categories

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

Asked:

on 23 Feb 2018

Edited:

on 25 Feb 2018

Community Treasure Hunt

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

Start Hunting!