integ lower limit in simscape custom component

5 views (last 30 days)
Hello,
I am having troubles defining the lower limit in a custom block in simscape. I want to integrate a variable from a specific point of the simulation (defined by an event trigger) up to the actual simulation time, and use it as a condition for an if equation. I defined the event trigger as follows:
events
when initialevent
t_L = {0, 's'};
elsewhen edge(T>49 || T<51)
t_L = time;
end
end
The equation section looks like this:
if T<49 || T>51
Q == mass*sp_heat*T.der;
else if integ(Q,t_L)<1000
T.der == 0;
else
Q == mass*sp_heat*T.der;
end
end
I cannot even start the simulation, receiving the following error:
Failed to generate 'UserLibrary.ComponentTest'
Caused by:
['']: Expression is not of expected variability: parametric.
• In UserLibrary.ComponentTtest (line 115)
Line 115 is the one with the integration condition else if integ(Q,t_L)<1000
If I change the variable t_L to a value, it works, so I do not know where the problem is. I cannot find much documentation in the integ function, so I really appreciate your comments.
Thank you

Accepted Answer

Hassaan
Hassaan on 16 Jan 2024
Edited: Hassaan on 16 Jan 2024
In Simscape, all equations must maintain consistent variability, and parametric variability is not allowed to change during simulation. This is likely because time is a continuous variable, and t_L is being set as a parameter which cannot change during simulation.
  1. Initial Event: The initialevent section is executed at the start of the simulation. Any variables defined here are typically considered parameters (constant throughout the simulation).
  2. Event Trigger: The elsewhen statement with edge(T>49 || T<51) appears to be intended to capture an event when T crosses the thresholds of 49 or 51. However, t_L is being assigned the value of time within this event, which can potentially lead to a change in t_L's variability from parametric to continuous, causing the error.
  3. Integration: The integ function typically integrates over the entire simulation time from 0 to the current time. It doesn't support changing the lower limit of integration during the simulation.
Considering these points, if you want to perform integration starting from a particular event in the simulation, you would typically need to use an approach that doesn't change the variability of the variables involved. One way to achieve this is to use a continuous state variable that starts accumulating at the event. Here's a conceptual example:
% Define a new continuous state variable
variables(Balancing = true)
integ_Q = {0, 'unit'};
end
% In the equations section, control the accumulation using a logical variable
equations
if T<49 || T>51
Q == mass*sp_heat*T.der;
integ_Q.der == 0; % Stop accumulating
else
% When T is between 49 and 51
if integ_Q < 1000
T.der == 0;
integ_Q.der == Q; % Start accumulating
else
Q == mass*sp_heat*T.der;
integ_Q.der == 0; % Stop accumulating
end
end
end
In this example, integ_Q is a continuous state variable that represents the integrated quantity. Its derivative is controlled based on the condition of T. When T is between 49 and 51 and the accumulated quantity is less than 1000, integ_Q accumulates Q. Otherwise, its derivative is set to zero to stop accumulation.
Please adjust the units and other specifics according to your actual model. This approach avoids the problem of changing t_L during the simulation, which is not allowed in Simscape.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 Comment
ANTÓN
ANTÓN on 16 Jan 2024
Edited: ANTÓN on 16 Jan 2024
Thank you very much for your answer, it is a really good approach. I implemented it in my code and works great.
I have two questions related to your answer, I'll appreciate if you could check them:
1) what is the effect of including " Balacing=true " in the variable declaration?
I had to eliminate this attribute since I got the following error:
Variable attribute 'Balancing' can only be used in a 'domain' class.
Once I deleted the attribute, the code worked perfectly.
2) Is it possible to reset the value of integ_Q back to 0 using an event?
Thanks in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Equations in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!