I cannot get my function to accept an input with a decimal and when the answer is displayed it displays an array of numbers.

31 views (last 30 days)
I cannot get my function to accept an input with a decimal and when the answer is displayed it displays an array of numbers instead of just the answer. Screenshot 2019-09-02 at 3.44.20 PM.png

Accepted Answer

Walter Roberson
Walter Roberson on 2 Sep 2019
Edited: Walter Roberson on 9 Sep 2019
You are confusing formulas and indexing. In MATLAB, there are two situations available:
A) If T is a numeric variable, then assigning to p(T) is always indexing, and T must be either logical or positive integer. The value of T gives the relative offset into the array (first location being index 1)
B) If T is an unresolved symbolic variable, such as syms T, then assigning to p(T) defines a symbolic function, equivalent to
p = symfun(EXPRESSION, T);
Later, p could then be invoked with a parameter that is an arbitrary number or possibly even symbolic value. The result of invoking p would be symbolic.
In MATLAB, if you want to define a formula that that returns a numeric value when passed a parameter, you need to create an anonymous function,
p = @(T) (999.83952 +(16.945176*T)-((7.9870401*10^-3)*T^2)-((46.170461*10^-6)*T^3)+((105.56302*10^-9)*T^4
-((280.54253*10^-12)*T^5)))/(1+((16.879850*10^-3)*T));
The result would be an unevaluated function handle, and you would need to invoke it on a particular numeric input in order to get a numeric result.
Remember, if you see something in a paper or textbook that looks like (for example)
then that is defining a formula, and if you want you can create a formula in MATLAB with
f = @(x) sin(exp(x));
but remember that this does not give a result for any particular x and you need to invoke the formula on your input to get a result.
fx = f(X);
Most of the time what you want to do is go directly to the result,
fx = sin(exp(X));
Notice there is not necessarily any () on the left hand side. There could be, for example,
for K = 1 : 10
fx(K) = sin(exp(X(K)));
end
This would be indexing not formula.
  8 Comments
Nathan Stawasz
Nathan Stawasz on 2 Sep 2019
How would I edit this function to get it so you can input the temperature into the fuction name f_density (68.14) and get the answer when the function is called ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!