How to write anonymous function for variable coefficients in heat transfer problem

3 次查看(过去 30 天)
I am working on a 2D thermal model consisting of 3 slabs. Two have constant thermal conductivities but the third has a conductivity that depends on temperature.
The relevant bit of code is:
crackT = 900;
Nu = 10;
kFunc = @(region,state) k+(k*(Nu-1)*(state.u<crackT));
thermalProperties(thermalModel,'Face',1,'ThermalConductivity',k*Nu,'MassDensity',2500,'SpecificHeat',1000);
thermalProperties(thermalModel,'Face',3,'ThermalConductivity',kFunc,'MassDensity',2500,'SpecificHeat',1000);
thermalProperties(thermalModel,'Face',2,'ThermalConductivity',k,'MassDensity',2500,'SpecificHeat',1000);
This code runs, but it seems that k for region 3 is unmodified instead of being changed according to the temperature in the region.
I frankly do not understand how the anonymous functions work--specifically, what "region", "location", and "state" do in the @ construction, nor what the commonly encountered error Function specifying a material property must accept 2 input arguments and return 1 output argument means.
If anyone could explain how to make this work I would appreciate it.

回答(2 个)

Matt J
Matt J 2020-1-3
编辑:Matt J 2020-1-3
As a simpler example, consider the following
>> c=3;
>> kfunc=@(a,b) a*b-c;
The @(a,b) says that the expression to the right will act as a function with input arguments a and b while everything else in the expression (in particular, c) will be viewed as a constant. The values of the constant variables will be taken from their values in the workspace at the moment kfunc is defined. Thus, for example,
>> kfunc(2,4) %2*4-3=5
ans =
5
It is important to understand, though, that kfunc is not aware of any changes you later make to the constant stuff. Thus, for example, this doesn't result in different output:
>> c=5; kfunc(2,4)
ans =
5
If I want kfunc to be aware of the new value of c, I have to rebuild it:
>> c=5; kfunc=@(a,b) a*b-c;
>> kfunc(2,4) %2*4-5=3
ans =
3
With that in mind, it should be clear that the output of your function,
kFunc = @(region,state) k+(k*(Nu-1)*(state.u<crackT));
can only vary with the "state" input argument. The "region" input variable does not appear in the defining expresssion k+(k*(Nu-1)*(state.u<crackT)) and everything else in the expression is a constant from the point of view of kFunc.
  1 个评论
Allen
Allen 2020-1-3
This is quite helpful for understanding anonymous functions--thanks--but the crux of my problem seems to be in using undocumented "state" and "region" variables. state is apparently some structure containing the dependent variable being solved for, and state.u is that variable (in my case, temperature). I cannot see how to use region on the right side (my model has 3 regions), and what I've written should make k larger if state.u (temperature) < crackT, but that's not showing up in the model.
I will post a separate question about these mysterious things.

请先登录,再进行评论。


MandarinLu
MandarinLu 2020-10-27
Actually I met with the similar problem recently. I have read both contributions. Even though I like to share a case I found and hope answers with more insights can be shared in terms of this topic.
For the generated mesh, the index of node and correpsonding coordinates (x, y, z) can be found in mesh.Nodes.
However the arguments such as location, state, or region in the funcion handle have different number of "Nodes" (I guess it should be) compared to mesh.Nodes but close to each other. I found this when I debuged my code and move the mouse over location.x or state.u for example. Unluckily I do not know how the toolbox set the size of location and state.
I hope someone can share more opinions.

Community Treasure Hunt

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

Start Hunting!

Translated by