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?

2 views (last 30 days)
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'.
%
  4 Comments
Image Analyst
Image Analyst on 27 Jan 2016
That's not error I get. I can call the function problem - it's inside it where there's an error. I get that it doesn't know about v(-1) which is exactly what Walter told you about. Did you not see his answer about how to correct the code?
Dominic Gladdish
Dominic Gladdish on 27 Jan 2016
I have seen what Walter has said, and i dont feel my code is right which is why i put the photo up as well, i have taken out the v(t-1) etc so that line of the code just looks like
v = v + a*dt ;
but I still recieve the same error message as before.
Is there some code I need to take out? Is there any code I need to add to help matlab return both (v,vterm) outputs?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jan 2016
Edited: Walter Roberson on 26 Jan 2016
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).
  1 Comment
Walter Roberson
Walter Roberson on 27 Jan 2016
If you are getting "Undefined function 'Student' for input arguments of type 'double'." then you have not saved the code into Student.m in a directory that is on your MATLAB path. (Your current directory is automatically on your MATLAB path, but you might not have saved the file there.)

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!