Applying 'if' condition within location-dependent thermal Properties

1 次查看(过去 30 天)
Hallo!
I am using an external function ro_ult to calculate location-dependent mass density for a single-cell, 3-D thermal PDE analysis:
function ro_ult = ro_ult(location, state)
ro_s = 1300;
ro_d = 1800;
if location.y <=0
ro_ult = @(location,state) ro_d-(ro_d-ro_s)*exp(location.y/0.06);
else
ro_ult=1.6;
end
end
Any attempt to use the function produces a following error:
Not enough input arguments.
Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('ro_ult', 'ro_ult.m', 4)" style="font-weight:bold">ro_ult</a> (<a href="matlab: opentoline('ro_ult.m',4,0)">line 4</a>)
if location.y <=0
Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('pde_lun20_2h_walec6prb', 'pde_lun20_2h_walec6prb.m', 152)" style="font-weight:bold">pde_lun20_2h_walec6prb</a> (<a href="matlab: opentoline('pde_lun20_2h_walec6prb.m',152,0)">line 152</a>)
'MassDensity',ro_ult,...
I will be most thankfull for any help in this matter.

回答(1 个)

Jan
Jan 2021-7-21
After defining
ro_ult = @(location,state) ro_d-(ro_d-ro_s)*exp(location.y/0.06);
ro_ult is a function handle, which expects two inputs. Then code like this:
ro_ult = ro_ult(location, state);
... 'MassDensity',ro_ult,...
calls the function ro_ult without input arguments, which produces the shown message.
Calling the output exactly as the name of the function is extremely confusing. Maybe
... 'MassDensity',ro_ult,...
occurs without calling the function ro_ult() before. The function does not contain useful comments also. So it is not clear why it replies a number or a function handle depending on a value. Maybe you mean:
function x = ro_ult(location, state)
% Insert useful comments here...
ro_s = 1300;
ro_d = 1800;
if location.y <= 0
x = ro_d - (ro_d - ro_s) * exp(location.y / 0.06);
else
x = 1.6;
end
end

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by