Trying to convert Kelvin to Celsius in Simscape code

I have a simulation with some Simscape Thermal and Electrical elements. The electrical element is a modification of the stock thermal resistor. The modification uses a nonlinear computation to determine the resistance (the stock thermal resistor is linear).
I discovered I was getting the wrong value because my nonlinear formula is anticipating temperature values in Celsius, however Simscape thermal works in Kelvin.
The code section in question is:
intermediates (Access=private, ExternalAccess=none)
% Calculate R, protecting against negative values
Rdem = R0*(a*T^2 + b*T + c);
R = if Rdem > 0, Rdem else {0,'Ohm'} end;
end
My formula for Rdem depends on T being in Celsius. I could probably change the formula, but I'd rather figure out how to change T to Celsius.
Even if I specify the units of T as 'degC' instead of K in the parameters/variables sections, it seems Simscape does not automatically convert K to degC in my block.
Documentation gives some details about
1) doing the affine conversion in a ps-to-simulink block (but i'm staying in Simscape domain)
2) symbolic toolbox has some unitConvert function but I don't have that toolbox
3) unit conversion block is for Simulink signals, again my signal is in Simscape domain
4) value() function doesn't seem to do the trick:
>> tk = {300, 'K'}; value(tk,'C','affine')
Check for incorrect argument data type or missing argument in call to function 'value'.
5) something else called simscape.Value was introduced in R2021b but I only have R2021a: https://www.mathworks.com/help/physmod/simscape/ref/simscape.value.html
Many thanks for any pointers.

 Réponse acceptée

Steve Miller
Steve Miller le 23 Nov 2021

0 votes

The piece of code you have shared doesn't completely describe the situation. A mismatch in units could be caused by the parameters a, b, and c. You should not need to recast your units if all elements in your equations (parameters, variables) have defined units.
Take a look at the source code for "Variable Thermal Resistance". You will see that input signals and parameters all have units associated with them, and they can be modified in the block dialog without modifying the source code.
--Steve

7 commentaires

I've tried what you suggest -
both editing the units in the Parameters/Variables of the block dialog, and also manually setting the default units in the source code. It looks like the value that comes through is 298.15C and not 25C.
Can you share the code with me?
--Steve
Thanks for checking Steve.
I zipped because file attach doesn't permit .ssc extensions.
When you run it and look at the third subplot of the scope, you get a resistance value of 3.15e-3 ohm near t=0. It should give a resistance value closer to 1e-3 ohms. If you manually plug the T values of 300C or 25C, you come up with those respective Ron values.
I have modified your model (see attached) so that the temperature remains constant and I have added a Simulink calculation of the resistance. Changing the parameter "Reference temperature" between 298K and 25 degC and changing the initial value of "Temperature" on the Variables tab betweem 298 K and 25 degC gives you the same answer on the scope, and it matches the Simulink implementation I put in which must take temperature in Kelvin as an input.
Note that Simscape is treating the units properly; the manual calculation in Simulink must receive Kelvin because the coefficients in the polynomial are not associated with units in the gain and constant blocks.
--Steve
If I run your sim as-is, the resistance value output is 3.1e-3 ohms. At 25C (or 298.15K) however, the result should be 1e-3 ohms.
How do I modify the code in the NL resistor block so that the T going into the formula can be interpreted correctly?
Can I cast the temperatures as celsius in code? It seems strange to me that I must convert my formula into a Kelvin based one.
You can access the temperature in terms of a different unit by using the function value().
See this documentation page: Working with Physical Units in Equations
The attached example yields the resistance you were expecting. Here is the equation used.
Rdem = R0*(a*{value(T,'degC')^3,'K^3'} + b*{value(T,'degC')^2,'K^2'} + c*{value(T,'degC'),'K'} + d);
--Steve
Thank you for that info.
I'm a little confused about the formulation {value(T,'degC')^3,'K^3'}
I think the first part pretends T is unitless but then tags it with unit degC? What about the K^3?
Is it correct that Matlab uses C, and simscape thermal uses degC?
To try to understand value() better I wanted to plug it into Matlab command line, but it seems it only works within simscape.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by