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

Simple Array multiplication

Simple Array multiplication

I am currently trying to get sum up the first 100 values of a fourier function. I wrote an OpenModelica program to help. But for some reason i am unable to get the program top work. If someone could help that would be greatly appreaciate it. This is what i wrote just an an example.

model sample1
Real x[5] = 1:5; //So this line should create a file column 1 row array. from 1,2,3,4,5
Real p=5;
Real y[5]; // here i am telling OpenModelica that this y-array is also 5 columns wide
//parameter Real z;
equation
y = p * x; // here i should multiply the array by the the real value, unfortunatelly this is not happening, in fact the code is not working it all.
z = sum(y); //here i should be able to add the all of the columns of y-array to get my sum.
end sample1;

I know i am making a dumb mistake somewhere, but after looking online and trying in vain to figure it out by myself, i am finally reaching out to hopefully get some help.

Thanks.

Re: Simple Array multiplication

You almost got it current/smile

Code:


model sample1
  parameter Real x[5] = 1:5; //So this line should create a file column 1 row array. from 1,2,3,4,5
  parameter Real p=5;
  Real y[5]; // here i am telling OpenModelica that this y-array is also 5 columns wide
  Real z;
equation
  y = p * x; // here i should multiply the array by the the real value, unfortunatelly this is not happening, in fact the code is not working it all.
  z = sum(y); //here i should be able to add the all of the columns of y-array to get my sum.
end sample1;

Re: Simple Array multiplication

Adrpo,

Thank you so much, that worked!!  current/big_smile

Re: Simple Array multiplication

Thank you again for your feedback. I have another question. I updated the code to try to get the following to work. However, i keep getting an error in variable "u". Open Modelica tells me that this cannot be solved bcause the operation is of integer type. Any help will be appreciated thank you.

model sample1
parameter Real x[5] = 1:5;
extends Modelica.Constants;
Modelica.SIunits.Length L = 2;
Real y[5];
Real p[5];
Real u[5];
Real z;
equation
u = x*pi/L;
p = ((e^(-1*((u)^2)*t)-1)/x^3); /*/here i have a problem where u cannot be solved because it is of type integer? But i am not sure what it should be then /*/ 
u =x*pi/L;
y = L * pi * x;
z = sum(y);
end sample1;

Edited by: mx_judo2021 - Mar-30-21 10:49:38

Re: Simple Array multiplication

You need to use the element-wise operators .^ .- and ./ instead of ^ - and /.

Re: Simple Array multiplication

Just an update, thanks sjoelund.se, the advice helped. The code runs just fine now. I am placing the updated code here so it may help others who have the same question.

model sample1
parameter Real x[5] = 1:5;
extends Modelica.Constants;
Modelica.SIunits.Length L = 2;
Real y[5];
Real p[5];
Real u[5];
Real z;
Real t = time;
equation
u = (x.*pi)./L;
p = ((e.^(-1.*((u).^2).*t).-1)./x.^3);
y = L * pi * x;
z = sum(y);
end sample1;

Edited by: mx_judo2021 - Mar-31-21 14:54:35
There are 0 guests and 0 other users also viewing this topic
You are here: