Ceate a vector of state space variable like A=[x(1), x(2), x(3),.....x(21)]

Hi...
How can I create a vector of state space variables by a loop to get
[x(1), x(2), x(3),.....x(21)]
I need this vector to use it in ODE15 as state space variables vector multiplied by other matrices forming DAEs.
Thanks

12 Comments

What vector do you want to create?
Let us say, there is no vector. I want to create 21 variables starting from x(1), to x(21) without using syms. I can use syms and x1 x2 x3...x21 but it takes time and I want to generalize by a function.
The reason for that to create a group or a vector of state variable.
Is that possible?
ismaeel, I think you're barking up the wrong tree. As John D'Errico points out below, the ODE suite accept numeric objects and function handles, not symbolic objects.
As far as I can tell from your description of this problem, what you're really after is a function M = MASS(t,y) for the mass matrix, where t is time and y is the state vector. If I'm wrong, you'll need to add more detail about what you're trying to do and how you're trying to do it.
When you have expressions like x(1) to x(21), the "x" is a vector already. Therefore I still do not understand what you want to achieve. When Matlab does not match your needs directly, try to use Matlab as it is designed to solve your problem.
ok, this is an example (my problem is larger than this one. That is why I want to use matrices instead of writing the whole equations)
function Out = SIMC0_1(t,x)
global Xdp ws H V td PC TF KA TA KE KF TE TSV RD SE Vref TH Xe Em Vt;
if t<td
PC0=PC;
else
PC0=1.1*PC;
end
%Function Output
%==========================================================================
Out= [
x(2)-ws;
ws/(2*H)*(x(6)-Em*V/(Xdp+Xe)*sin(x(1)-TH));
1/TE*((-(KE+SE))*x(3)+x(5));
1/TF*(-x(4)+KF/TF*x(3));
1/TA*(-x(5)+KA*x(4)-KA*KF/TF*x(3)+KA*(Vref-Vt));
-x(6)+x(7);
1/TSV*(-x(7)+PC0-1/RD*(x(2)/ws-1));
];
Any name out x(1) x(2).. is constant, for example ws=377 and so on
This is the function file
If you want I writ down the main file too.
Thanks
So do you want to turn the function output into something like this?
Out = M * x;
And if so, are you wondering how to formulate it as a matrix equation, import M into the function, or both?
Exactly Andrew, I want to turn (Out) to (A matrix * B matrix (state space x matrix)+ the rest matrix)
Do you have any idea?
Thanks
I solved it. Here is my simple code:
for i=1:10
x(i,:)=subs('x(i)','i', i)
end
Thanks
That gives you
x =
x(sqrt(-1))
x(2)
x(3)
x(4)
x(5)
x(6)
x(7)
x(8)
x(9)
x(10)
I don't see how that helps - even if the first entry were x(1).
I do not have this result. This is what I got:
x =
x(1)
x(2)
x(3)
x(4)
x(5)
x(6)
x(7)
x(8)
x(9)
x(10)
However, as a solution to this case, we can define x(1) separately, then make a loop from 2 to length of the matrix.
Or, just let the loop starts with 2 then subtract 1 from the vector. Thanks...
But again, how do you get from there to solving
dx / dt = M(t)*x + b?
Look at this:
It is what I am working on
I did it already but without using matrices
Thanks for your contribution.

Sign in to comment.

 Accepted Answer

The problem is, you apparently do not appreciate the basics of MATLAB.
One does not create a vector of (symbolic) variables to use in a tool like ode15s. ode15s takes a vector of NUMERIC starting values. It then returns a predicted solution based on the equations you pose, at a given set of times.
There are no symbolic variables that you need to create or define or allocate here. Really, it is best that you start reading the basic tutorials about variables and how to call functions, etc. These are fundamental concepts in MATLAB.
Regardless, as the help for sym indicates:
A = sym('A',[M N])
This creates M-by-N vectors or matrices of symbolic scalar variables.

4 Comments

ok thanks. But still I need to write in the function file that ODE15s call it. I did not get answer to my question. If I am wrong please correct me. I am trying to appreciate the basics of MATLAB as possible as I can but I need a help that is why asked my question here.
I think you did not get my point. I am writing my DEs in matrices. one of this matrixes is a vector of state space variables BUT not numeric vector. This is the problem not what you called it problem. However, thanks...
Well, whatever your goal, why not use sym? From the help...
A = sym('A',[M N]) creates M-by-N vectors or matrices of symbolic scalar
variables...
Thanks John. It creates A1 A2 A3 ...Amn
I need them to be like A(1) A(2) A(3)... A(mn)
Is there any function changes Amn to A(mn)?

Sign in to comment.

More Answers (1)

Hallo Ismaeel. There is no need to use loop to do that. Check the colon usage of Matlab or the linspace command
For this you just simply do i.e.:
x = 1:21;
or
x = linspace(1, 10, 21);

1 Comment

Hi Giorgos
ok but I do not want to have values. I mean I need them to be variables without any values.
if you print x = 1:21; you will get a vector of values not variable.
I am looking for a function or loop so that I get a vector like
[x(1), x(2), x(3),.....x(21)]

Sign in to comment.

Asked:

on 15 Mar 2015

Edited:

on 16 Mar 2015

Community Treasure Hunt

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

Start Hunting!