Clear Filters
Clear Filters

Trying to convert Kelvin to Celsius in Simscape code

7 views (last 30 days)
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;
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:
Many thanks for any pointers.

Accepted Answer

Steve Miller
Steve Miller on 23 Nov 2021
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 Miller
Steve Miller on 23 Nov 2021
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);
Matthew Mishrikey
Matthew Mishrikey on 1 Dec 2021
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.

Sign in to comment.

More Answers (0)


Find more on Foundation and Custom Domains in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!