Define function for transient transient boundary condition

47 views (last 30 days)
John McGrath
John McGrath on 15 Nov 2024 at 15:20
Edited: Torsten on 19 Nov 2024 at 20:09
As shown below in yellow highlight, I have a polynomial expression for a refrigerant time-varying thermal boundary condition. I want to define this as a function that I can call later in a thermalBC statement. My code won’t run. I need help in defining the function properly and calling it properly. Thanks
F1= 3.843e-8
F2= -3.482e-5
F3= 1.179e-2
F4= -1.95
F5= 23.69
tlistmin= 0; % Minimum time (s)
tlistmax= 300; % Maximum time (s)
tlistdelta= (tlistmax-tlistmin)/100 % time step
tlist=tlistmin:tlistdelta:tlistmax
Trefrig= F1*tlist.^4 + F2*tlist.^3 + F3*tlist.^2 + F4*tlist + F5
function Trefrig = Trefrig(state.time)
thermalBC(thermalmodel,"Edge",7,...
"ConvectionCoefficient",hrefrig,...
"AmbientTemperature", @Trefrig)
  2 Comments
John McGrath
John McGrath on 16 Nov 2024 at 13:46
Hi Walter
Thanks for your suggestion. MatLab is coming back with "Invalid use of operator" for the line:
functtion Trefrig = Trefrig(state.time)
Walter Roberson
Walter Roberson on 16 Nov 2024 at 18:42
I did not make any suggestions.
I might possibly have reformatted the code.

Sign in to comment.

Answers (2)

Torsten
Torsten on 15 Nov 2024 at 20:33
Edited: Torsten on 15 Nov 2024 at 20:34
F1= 3.843e-8;
F2= -3.482e-5;
F3= 1.179e-2;
F4= -1.95;
F5= 23.69;
Trefrig = @(location,state)F1*state.time^4 + F2*state.time^3 + F3*state.time^2 + F4*state.time + F5;
thermalBC(thermalmodel,"Edge",7,...
"ConvectionCoefficient",hrefrig,...
"AmbientTemperature",Trefrig)
For further information, I recommend
  7 Comments
John McGrath
John McGrath on 19 Nov 2024 at 15:40
This plotting code worked. Thanks Torsten
I'd like to return to your original suggested code to produce the transient thermal boundary condition that I seek.
I ran into a problem when I copied and pasted your code into the Command Window.
F1= 3.843e-8;
F2= -3.482e-5;
F3= 1.179e-2;
F4= -1.95;
F5= 23.69;
Trefrig = @(location,state)F1*state.time^4 + F2*state.time^3 + F3*state.time^2 + F4*state.time + F5;
thermalBC(thermalmodel,"Edge",7,...
"ConvectionCoefficient",hrefrig,...
"AmbientTemperature",Trefrig)
The errors returned are these:
Error using pde.ThermalBC (line 87)
The value of 'AmbientTemperature' is invalid. Must be specified as a numeric value.
Error in pde.ThermalModel/thermalBC (line 154)
bc = pde.ThermalBC(self.BoundaryConditions,argsToPass{:});
Error in untitled205 (line 7)
thermalBC(thermalmodel,"Edge",7,...
My understanding is that you have defined an anonymous function [Trefrig] and use its handle to specify the time-varying boundary condition in the thermalBC line of code. I don’t understand why the error occurs.
I have some related questions. When you use “@location,state)”, is it understood that location refers to “Edge”,7 and state refers to state.time? In my original code I defined a time variable tlist as well as the minimum time, the maximum time and the time increment. How are those values defined for your code using state.time?
Torsten
Torsten on 19 Nov 2024 at 18:05
Edited: Torsten on 19 Nov 2024 at 18:06
I don’t understand why the error occurs.
@Walter Roberson wrote that the ambient temperature can only be specified as a constant value, not as a time-varying function handle. You will have to check the documentation of the PDE Toolbox.
I have some related questions. When you use “@location,state)”, is it understood that location refers to “Edge”,7 and state refers to state.time? In my original code I defined a time variable tlist as well as the minimum time, the maximum time and the time increment. How are those values defined for your code using state.time?
If the ambient temperature could be specified as a time-varying function handle (see above), the function is called by the PDE Toolbox solver during the integration and returns your ambient temperature, evaluated at the time "state.time" when the solver needs it to know. It's not possible to set it in the way you did.

Sign in to comment.


Walter Roberson
Walter Roberson on 16 Nov 2024 at 19:03
function Trefrig = Trefrig(state.time)
thermalBC(thermalmodel,"Edge",7,...
"ConvectionCoefficient",hrefrig,...
"AmbientTemperature", @Trefrig)
First of all: when you define a function then the list of parameters must be strictly unindexed variables. You cannot designate the field of a structure to receive data.
Second of all, attempting to assign the output to the same name as the function would be an attempt to convert between the name representing a function and the name representing a variable, and is no longer permitted.
Thirdly: neither thermalmodel nor hrefrig are defined inside the function.
Fourthly: you are passing @Trefrig as the AmbientTemperature parameter. That is a handle to the exact same function you are defining. The intent is almost certainly that the function handle would be invoked -- but invoking it would cause termalBC to be called again, which would pass in @Trefrig which would lead to @Trefrig being called again, which would call termalBC again which... If you are going to deliberately construct recursive code, you need to ensure that you have an escape condition before blindly calling the function again.
Fifthly: the AmbientTemperature parameter accepts scalar numeric values only, not a function handle.
  4 Comments
John McGrath
John McGrath on 19 Nov 2024 at 20:03
So what is the proper way to model a time-varying Ambient Temperature? The error message indicates that it must be a numeric value but the Ambient Temperature is not a single value, it's a vector of numeric values, one for each time.
thanks
Torsten
Torsten on 19 Nov 2024 at 20:08
Edited: Torsten on 19 Nov 2024 at 20:09
You will have to define your problem as a general PDE, not as a heat transfer problem.
Look at page 2-127 of the linked User Guide for the PDE Toolbox on how to define a general Neumann boundary condition on edge 7 for a general PDE.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!