How do I make my user defined function accept vectors as inputs rather than scalars?

2 次查看(过去 30 天)
Specifically I would like to make my input (d) a vector. How do I change my code so that my function accepts the vector as input?
function Fgravitational=GravForce(m1,m2,d)
%Newtons Gravitational force eqation
%inputs: mass 1, mass 2, distance
%output: force of gravity in N
G=6.672e-11;
Fgravitational=-G*((m1*m2)/d^2);
fprintf('The gravitational force is %.3e N \n ', Fgravitational);
end

回答(1 个)

Sean de Wolski
Sean de Wolski 2016-2-9
Fgravitational=-G*((m1*m2)/d^2);
Put a . before the mathematical operators:
G.*((m1.*me)./d.^2)
There's a helper function vectorize that does this for you.

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by