Anonymous function inside of function file

4 次查看(过去 30 天)
Attatched is the problem and my attempt so far.
matlabhelp.JPG
  3 个评论
Peter M Hogan
Peter M Hogan 2019-2-12
Thanks for the response, Walter. I am not getting the correct answer to this problem. Two things are happening. I am not able to define my "rho" function with the exponentiation as shown, as I get an error in return. I also am not sure how to include an anonymous function inside of a function as the problem asks me to do.
Jan
Jan 2019-2-12
Whenever you mention in the forum that an error occurs, post a copy of the complete message. It is easier to fix an error than to guess, what the error is.
Please post the code as text, not as screenshot. The latter does not allow to fix your code by copy&paste, but requires a re-typing of what you have done already.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2019-2-12
编辑:Jan 2019-2-12
Your anonymous function CfromF takes one input and converts its contents. In your code you call this function without an input argument as "(CfromF)", but you need CfromF(T) with an input argument. Please read and post the error message, which clarifies this problem.
Your function obtains the input T from the outside. You initialize F as empty matrix and overwrite T afterwards. Then the input is lost. Better: avoid smart tricks with anonymous functions. Simple convert the value directly:
function rho = density(TF)
TC = (TF - 32) * 5/9;
...
If you want to learn how to do this more complicated:
function rho = density(TF)
CfromF = @(F) (F - 32) * 5/9;
TC = CfromF(TF);
...
In both cases use the value TC afterwards.
  2 个评论
Peter M Hogan
Peter M Hogan 2019-2-12
function rho = density(T)
CfromF = @(F) 5*(F-32)/9;
TC=CfromF(F);
rho=5.53*10^-8*(TC).^3-8.5*10^-6*(TC).^2+6.56*10^-5*(TC)+1;
end
ok sorry! i will learn the proper posting convention. the error is that F is underdefined. I am required to use the anon function within the .m file. I am unsure how to allow T to be an input that is then changed into the ouput of "CfromF" before being run through rho. To call this funtion I must be able to enter an array of T values; ie; T=linspace(32,82,100) and then call rho=density(T), returning an array of outputs.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by