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)
Show older comments
Nathan Stawasz
on 2 Sep 2019
Edited: Walter Roberson
on 9 Sep 2019
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.
0 Comments
Accepted Answer
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
Walter Roberson
on 2 Sep 2019
Remove the line
T= input('Enter the temperature of the pure water:');
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!