MATLAB: Im trying to write code for a vertically falling cannonball, i keep getting an error could “Undefined function ‘function name’ for input arguments of type ‘double’. Can anyone help me out

doubleerror

function [v,vterm] = Student(m,d,T)
% A funuction to deterined a vertically falling cannonball, while being
% effected by drag
% Various Constant
cd = 0.47; % drag coefficient for a sphere
rho = 1.2; % density of air
g = 9.81; % gravitational constant
dt = 0.1; % Time step
v = 0; % Vertically falling cannonball, released from rest
for t = 0:dt:T
a = ((cd*rho*(pi*d^2))/2*m)-g ;
v = v(t-1) + a(t)*dt ;
hold on;
plot (t,v,'.');
end
Fnet = cd*0.5*rho*(v(t)^2)*(pi*d^2);
if Fnet == 0
vterm = sqrt(2*m*g/rho*(pi*d^2)*cd);
end
The code I used to call this function is as followed
[v,vterm] = Student(5,2,20)
and the error message that was returned to me was:
Undefined function 'Student' for input arguments of type 'double'.
%

Best Answer

Your line
a = ((cd*rho*(pi*d^2))/2*m)-g ;
computes a scalar value, unless d happens to be a square 2D array or m happens to be a non-scalar numeric value.
Your line after that,
v = v(t-1) + a(t)*dt ;
attempts to access that scalar a at index t where t is from the for loop, a non-integer value from 0 in increments of 0.1. It is not legal to index by a floating point value. You also attempt to access the scalar v at index t-1 which would be at negative indices until t reached 1.
Referring back to your line
a = ((cd*rho*(pi*d^2))/2*m)-g
I wonder whether you are intending to divide by 2 and multiply the result by m (as your current code does), or if you are instead intending to divide by (2 times m)? The code might be valid as it is, but it is better to avoid potential confusion by rewriting the code. For example you do not need any () on that line unless you are intending to divide by (2 times m).