Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Arrays and plotting

Arrays and plotting

Hello!

I have made a model an i want to solve the state space equation:   x' = A*x + F*u

A is a 14x14 matrix
x is a 14x1 column-matrix
F is a 14x4 matrix
and u is 4x1 column-matrix.

My code is : der(x) = A*x + F*u ;

u column-matrix is the input for my system.

I want, at any time time interval, to give a different input u (different column-matrix)
and at the end  to be able to plot any element of x column-matrix according to time.
(i want to plot the different values that the element of x column-matrix has take according to input u in one graph).

Has anyone any idea?

I am sorry for my silly questions but it is the first time i use Modelica(3 weeks now).

Thank you.

Edited by: Petrou - Jul-16-11 23:15:32

Re: Arrays and plotting

Hi,

It depends on how you want to specify the different u, via an external function call, via a parameter matrix, etc.

You can use a when equation with sample (for example):
when sample(0, 1) /* start at 0 and generate events at each of 1, 2, 3, etc */ then
  index = index + 1;
  reinit(u, differentU[index]); // note that you need to have enough columns in differentU for all the sample events you get during the simulation.
end when;

You can then use
simulate(Model, stopTime=10, numberOfIntervals=10);
plot({u[1],x[1],u[2],x[2]}); // here you can add all 14.

Cheers,
Adrian Pop/

Re: Arrays and plotting

Thank you very much for your answer Adrian.

I try all day what you said to me but i didnt succeed it.

Is it possible to declare u as (for example) 14x10 matrix and at each time interval get a different column of u??

I try this code:


Integer index = 1;
Real V[14,1]

equation

when sample(0, 1) then
for i in 1:14 loop
v[i,1] = u[i,index];
end for;

der(x) = A*x + F*v ;

index=index+1;

end when;

but i am getting errors...
Could you please help me, give some instructions more??

Thank you.

Re: Arrays and plotting

Can anyone please help me???

Re: Arrays and plotting

Here is it the model:

Code:


// file: petrou.mo
model petrou
  parameter Real A[14,14] = zeros(14,14);
  parameter Real F[14,4] = ones(14,4) * 10.5;
  // make it 12 to be sure we don't run out of bounds
  parameter Real u[12,4] = ones(12,4) * 1.5;
 
  Real x[14];
  Real v[4];
  Integer i(start = 0);
equation
  i = integer(floor(time + 1));
  for j in 1:4 loop
    v[j] = u[i,j];
  end for;
  der(x) = A*x + F*v;
end petrou;

The script to do the simulation and plotting.

Code:

// file: petrou.mos

loadFile("petrou.mo"); getErrorString();
system("rm -f petrou.exe petrou.c petrou.l* petrou.makefile petrou.o petrou petrou_*"); getErrorString();
simulate(petrou, stopTime=10, numberOfIntervals=10); getErrorString();
plot3({x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13], x[14], v[1], v[2], v[3], v[4]});
simulate(petrou, stopTime=10, numberOfIntervals=10, outputFormat="csv"); getErrorString();
readFile("petrou_res.csv"); getErrorString();

Code:


adrpo@ida-liu050 ~/dev/OpenModelicaBackend/build/bin/petrou
$ /c/OpenModelica1.7.0/bin/omc.exe petrou.mos

true
""
0
""
record SimulationResult
    resultFile = "petrou_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 10, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'petrou', storeInTemp = false, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*', measureTime = false, cflags = ''",
    messages = "",
    timeFrontend = 0.0374963188519052,
    timeBackend = 0.0962468650091716,
    timeSimCode = 0.025620405782819,
    timeTemplates = 0.0350056492390369,
    timeCompile = 2.18642527317487,
    timeSimulation = 0.0637284207730315,
    timeTotal = 2.44462340652001
end SimulationResult;
""
true
record SimulationResult
    resultFile = "petrou_res.csv",
    simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 10, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'petrou', storeInTemp = false, noClean = false, options = '', outputFormat = 'csv', variableFilter = '.*', measureTime = false, cflags = ''",
    messages = "",
    timeFrontend = 0.045258585014792,
    timeBackend = 0.116084451792839,
    timeSimCode = 0.0164164769151113,
    timeTemplates = 0.0333903710789758,
    timeCompile = 2.20739848211588,
    timeSimulation = 0.0628626532368119,
    timeTotal = 2.48147184330724
end SimulationResult;
""
"\"time\",\"x[1]\",\"x[2]\",\"x[3]\",\"x[4]\",\"x[5]\",\"x[6]\",\"x[7]\",\"x[8]\",\"x[9]\",\"x[10]\",\"x[11]\",\"x[12]\",\"x[13]\",\"x[14]\",\"der(x[1])\",\"der(x[2])\",\"der(x[3])\",\"der(x[4])\",\"der(x[5])\",\"der(x[6])\",\"der(x[7])\",\"der(x[8])\",\"der(x[9])\",\"der(x[10])\",\"der(x[11])\",\"der(x[12])\",\"der(x[13])\",\"der(x[14])\",\"v[1]\",\"v[2]\",\"v[3]\",\"v[4]\",\"i\",

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

1,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441.0000000000002,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

2,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,882.0000000000002,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

3,1323,1323,1323,1323,1323,1323,1323,1323,1323,1323,1323,1323,1323,1323,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

4,1764,1764,1764,1764,1764,1764,1764,1764,1764,1764,1764,1764,1764,1764,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

5,2205,2205,2205,2205,2205,2205,2205,2205,2205,2205,2205,2205,2205,2205,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

6,2646,2646,2646,2646,2646,2646,2646,2646,2646,2646,2646,2646,2646,2646,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

7,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,3087.000000000001,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

8,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,3528.000000000001,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

9,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,3969.000000000001,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

10,4410,4410,4410,4410,4410,4410,4410,4410,4410,4410,4410,4410,4410,4410,441,441,441,441,441,441,441,441,441,441,441,441,441,441,10.5,10.5,10.5,10.5,1,

"
""

Cheers,
Adrian Pop/

Re: Arrays and plotting

Thank you very much Mr. Pop,

You are great! I owe you!

Re: Arrays and plotting

Hello again...

The code you send me is perfect, but as i realize it doesn't support the switch of v at times intervals so to be able to solve the equation: der(x) = A*x + F*v
in each time interval for different v... (Maybe i am wrong.)

I try something like this in your code:

equation
when sample(0,1) then

  i = integer(floor(time + 1));

  for j in 1:4 loop

    v[j] = u[i,j];

  end for;

  der(x) = A*x + F*v;
end when;

but i am getting error :   Error: Invalid left-hand side of when-equation: der(x[1,1]).

and as i read the equations into when statements must have the format x = ...

Is there any solution?

Thank you very much and a big sorry if i am getting annoying...

Re: Arrays and plotting

Because of equation: "der(x) = A*x + F*v;" x is continuous, so you cannot make it discrete.
The value of x does not change between step times in the output file so the output is discrete,
however our plotting does interpolation, that's why you see it as continuous.
If you want the output to be discrete, then you do it like this:

Code:


model petrou
parameter Real A[14,14] = zeros(14,14);
parameter Real F[14,4] = ones(14,4) * 10.5;
// make it 12 to be sure we don't run out of bounds
parameter Real u[12,4] = ones(12,4) * 1.5;

Real x[14];
discrete Real y[14];
Real v[4];
Integer i(start = 0);
equation
i = integer(floor(time + 1));
for j in 1:4 loop
   v[j] = u[i,j];
end for;
der(x) = A*x + F*v;
when sample(0, 1) then
   y = x;
end when;
end petrou;

Code:


loadFile("petrou.mo"); getErrorString();
system("rm -f petrou.exe petrou.c petrou.l* petrou.makefile petrou.o petrou petrou_*"); getErrorString();
simulate(petrou, stopTime=10, numberOfIntervals=10); getErrorString();
plot3({y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8], y[9], y[10], y[11], y[12], y[13], y[14], v[1], v[2], v[3], v[4]});
simulate(petrou, stopTime=10, numberOfIntervals=10, outputFormat="csv"); getErrorString();
readFile("petrou_res.csv"); getErrorString();

Cheers,
Adrian Pop/

Re: Arrays and plotting

I am terribly sorry but obviously i am doing something wrong or i have not understand something...
i am getting errors again...

do i have to save petrou.mo in a specific folder, so to be able to load it as loadFile("petrou.mo") and not loadFile("C:\. . .\petrou.mo")?

i am writting in the OMshell the code for simlation and plotting you send me but i am getting errors...

also the plot3 function does not exist...

the code for model (i use OMC editor) works perfect..(i think)
when i plot there the x[1],x[2],...seems to be right... all the values are raising according to time...

Thank you...

Re: Arrays and plotting

Use the latest nightly-build to have plot3:
http://build.openmodelica.org/omc/build … ly-builds/

I only use omc from command line, i.e. (cmd.exe):
> cd path\to\where\the\files\are\
> c:\OpenModelica1.7.0\bin\omc petrou.mos

In OMShell, use forward slashes in loadFile, ie / instead of \.
You can use:
> cd("path/to/where/files/are");
> runScript("petrou.mos");

Cheers,
Adrian Pop/

Re: Arrays and plotting

Hello again.

Thank you, i did what you propose to me and everything work good...
I would like to ask something last.

As i said at the start of the topic i want at each time interval to give different values at  v(4x1 column-matrix) and resolve the equation: der(x) = A*x + F*v for every different v,
so x has different value at each time interval...

At the end of the simulation i want to plot x (all different values at each time interval of x[1] as one graph. the same for x[2],x[3],...)

I think as i can realize the next code doesn't renew the value of v at each time interval:


model petrou

  parameter Real A[14,14] = zeros(14,14);

  parameter Real F[14,4] = ones(14,4) * 10.5;

  // make it 12 to be sure we don't run out of bounds

  parameter Real u[12,4] = ones(12,4) * 1.5;

 

  Real x[14];

  Real v[4];

  Integer i(start = 0);

equation

  i = integer(floor(time + 1));

  for j in 1:4 loop

    v[j] = u[i,j];

  end for;

  der(x) = A*x + F*v;

end petrou;


I also used the code you proposed me :

model petrou

parameter Real A[14,14] = zeros(14,14);

parameter Real F[14,4] = ones(14,4) * 10.5;

// make it 12 to be sure we don't run out of bounds

parameter Real u[12,4] = ones(12,4) * 1.5;



Real x[14];

discrete Real y[14];

Real v[4];

Integer i(start = 0);

equation

i = integer(floor(time + 1));

for j in 1:4 loop

   v[j] = u[i,j];

end for;

der(x) = A*x + F*v;

when sample(0, 1) then

   y = x;

end when;

end petrou;

but i doesnt seems to renew the value of v at each time interval...

As i read and as you proposed to me the equation sample() triggers at each time interval and is the apropriate for what i want.
Because of the continuous nature of equation der() i can't use the next code (i am getting errors) :

equation
when sample(0,1) then

  i = integer(floor(time + 1));

  for j in 1:4 loop

    v[j] = u[i,j];

  end for;

  der(x) = A*x + F*v;
end when;

Is there a solution for my problem?

Thank you very much for one more time...

There are 0 guests and 0 other users also viewing this topic
You are here: