Define function for transient transient boundary condition
Show older comments
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
on 16 Nov 2024
Walter Roberson
on 16 Nov 2024
I did not make any suggestions.
I might possibly have reformatted the code.
Answers (2)
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
on 16 Nov 2024
Torsten
on 16 Nov 2024
And the code fragment does not work ?
John McGrath
on 16 Nov 2024
Edited: Walter Roberson
on 16 Nov 2024
Walter Roberson
on 16 Nov 2024
Trefrig = @(location,state)F1*state.time^4 + F2*state.time^3 + F3*state.time^2 + F4*state.time + F5;
Okay, Trefrig is the handle to an anonymous function.
plot(tlist, Trefrig);
You are trying to plot passing in a numeric vector, and a function handle. plot() is not able to process function handles.
Your Trefrig function handle ignores the first parameter, and instead uses state.time from the second parameter. However, you have not defined any structure or "object" at all, let alone one with a time field.
The function handle "Trefrig" is constructed to be used by the PDE Toolbox, not for plotting.
If you want to plot, use
tlistmin= 0; % Minimum time (s)
tlistmax= 300; % Maximum time (s)
tlistdelta= (tlistmax-tlistmin)/100; % time step
tlist=tlistmin:tlistdelta:tlistmax;
F1= 3.843e-8;
F2= -3.482e-5;
F3= 1.179e-2;
F4= -1.95;
F5= 23.69;
Trefrig = @(t)F1*t.^4 + F2*t.^3 + F3*t.^2 + F4*t + F5;
plot(tlist,Trefrig(tlist))
John McGrath
on 19 Nov 2024
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.
Walter Roberson
on 16 Nov 2024
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
on 19 Nov 2024
Walter Roberson
on 19 Nov 2024
AmbientTemperature cannot be set to a function handle. This is unlike most of the other parameters, which mostly can be set to function handles.
John McGrath
on 19 Nov 2024
Categories
Find more on Heat Transfer 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!