How to create a function that accepts a vector input

12 views (last 30 days)
I have created the function below to calculate the temperature distribution inside of a fuel cell:
function TD = temp_distribution(r)
q = (7200*(1-((2.4048*(0.7620/244)^2)/4)+((2.4048*(0.7620/244)^4)/64)-((2.4048*(0.7620/244)^6)/2304))*(cosd((pi*150)/610)))
TD = (1500-((q*r^2)/(4*0.05)));
end
And I would like to input a radius vector in order to find the heat distribution throughout the fuel cell; so, in a seperate file I created a radius vector and attempted to input it into my function.
rad = [0.001:0.001:0.762];
temp = temp_distribution(rad);
However, when I try to run this code I get an error with the fuction file. When I try to run the fuction with a single number input, it works just fine. So my question is, how can I change my fuction so that it will take the vector input, apply the function to every value in the vector, and then output a new vector?

Answers (1)

Stephen23
Stephen23 on 20 Feb 2019
Edited: Stephen23 on 20 Feb 2019
You will need to change all of the linear algebra operators (i.e. "matrix" operations) to element-wise operations (i.e. "array" operations):
E.g.
/ change to ./
* change to .*
etc.
Using MATLAB requires knowing the difference between array and matrix operations.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!