Anonymous function inside of function file

조회 수: 12 (최근 30일)
Peter M Hogan
Peter M Hogan 2019년 2월 12일
댓글: Peter M Hogan 2019년 2월 12일
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.
Peter M Hogan
Peter M Hogan 2019년 2월 12일
Wait! I got it. Thank you Jan!!!!

댓글을 달려면 로그인하십시오.

카테고리

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