Anonymous function inside of function file

11 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by