Help with my for loop please?

1 view (last 30 days)
Dave
Dave on 28 Feb 2013
Hi,
I wrote some code below but it's not exactly how I want it; it's more like a demonstration of my ideal outcome. I'm trying to symbolically assign subscripts as the for loop goes on...so every time there's a u_i-1 or u_i-2..it's ideally meant to be u_(i-1) and u_(i-2).
My problem though is that I don't have a way of assigning symbolic subscripts within the for loop and as of right now the only way I can fix this is by manually editing each index of my matrix: Vector
Any help greatly appreciated :) :)
(PS I know a few variables are unassigned in my syms string, I have use for them later ^_^)
clear all
clc
syms P A_0 U L E j u x u_i
N = 4;
h = L/N;
k=N+2;
for i=1:k
vec = [u_i-2:u_i-1]+i;
mat(i,:) = vec;
Vector = fliplr(mat);
end
Vector

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2013
If u_(i-1) is a symbolic expression then you are not going to be able to use u_(i-2):u_(i-1) because the ":" operator is not defined for symbols.
If the u_ are numeric then there is no need to worry about symbolically assigning subscripts; leave u_ as a numeric vector and access u_(i-2) and u_(i-1)
  3 Comments
Walter Roberson
Walter Roberson on 28 Feb 2013
I do not see the point. If the size of your vec can vary between iterations then you cannot store vec into mat(i,:), and if the size of vec does not vary then you do not need to go through the trouble of working with symbolic endpoints.
If there is some good reason to use symbolic endpoints then remember you can create a vector of symbols and index the vector.
If you are using R2011b or later, perhaps you want something like
u_ = syms('u', [1 10]);
to create the vector.
Dave
Dave on 28 Feb 2013
I think that gets me closer to what I want but the subscripts won't be organized properly unless the for loop can categorize the 'Vector' matrix using your suggestion, but like you said before about ":" not defining symbols, I think this is dead in the water..
Also I think you meant to say 'sym' as in
u_ = sym('u', [1 10])
:)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!