How to create a function that accepts a vector input

10 次查看(过去 30 天)
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?

回答(1 个)

Stephen23
Stephen23 2019-2-20
编辑:Stephen23 2019-2-20
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by