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

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

Réponses (1)

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.

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by