How to create a function that accepts a vector input

조회 수: 13 (최근 30일)
Reese Collins
Reese Collins 2019년 2월 20일
편집: Stephen23 2019년 2월 20일
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