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
  • Index
  • » Users
  • » needlearn
  • » Profile

Posts

Posts

Dear all,

(I'm sorry I post in wrong place. How can I move it into modelica language sub-forum or delete it and then post in that sub-forum?)

In "Modelica Language Specification 3.2" chapter 12.4.5, there is an example "collectPositive".

function collectPositive
  input Real x[:];
  output Real xpos[:];
algorithm
  for i in 1:size(x,1) loop
    if x[i]>0 then
      xpos:=cat(1,xpos,x[i:i]);
    end if;
  end for;
end collectPositive;

When I call it in the following way, there is an error.

model testcollectPositive
Real[:] x={2,8.2,-3,5,2,1,2,2,6};
Real[:] xpos;
equation
xpos=collectPositive(x);
end testcollectPositive;

The error message is following:

Size for output argument using : in function call:
collectPositive(x)
Basic type inconsistencies detected.
Check aborted.
ERROR: 1 error was found


Then I change the model into the other format as following:

model testcollectPositive1
Real[:] x={2,8.2,-3,5,2,1,2,2,6};
Real[:] xpos;
equation
   for i in 1:size(x,1) loop
    if x[i]>0 then
      xpos:=cat(1,xpos,x[i:i]);
    end if;
  end for;
end testcollectPositive1;

There is a error when compilation.

Size mismatch in equation:
xpos = cat(1, xpos, x[i:i]);
Size 1 of the left operand is size(xpos, 1) whereas size 1 of right operand is 1+size(xpos, 1).
Basic type inconsistencies detected.
Check aborted.
ERROR: 1 error was found

Could anyone help me to figure out the reason?
Thanks very much.

Needlearn

  • Index
  • » Users
  • » needlearn
  • » Profile
You are here: