How do I make my user defined function accept vectors as inputs rather than scalars?
Show older comments
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
Answers (1)
Sean de Wolski
on 9 Feb 2016
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.
Categories
Find more on Physics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!