Anonymous function inside of function file

12 Ansichten (letzte 30 Tage)
Peter M Hogan
Peter M Hogan am 12 Feb. 2019
Kommentiert: Peter M Hogan am 12 Feb. 2019
Attatched is the problem and my attempt so far.
matlabhelp.JPG
  3 Kommentare
Peter M Hogan
Peter M Hogan am 12 Feb. 2019
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 am 12 Feb. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 12 Feb. 2019
Bearbeitet: Jan am 12 Feb. 2019
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 Kommentare
Peter M Hogan
Peter M Hogan am 12 Feb. 2019
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 am 12 Feb. 2019
Wait! I got it. Thank you Jan!!!!

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by