Index of zero error
Show older comments
Hello, whenever I use this function to try to solve for the variables I get an error saying that I am trying to take the index(0) of a function. I have looked this thing up and down and cant find where its getting zero from, any help would be appreciated I am kind of new to MATLAB.
function R
g=9.8; alpha=((45*pi)/180);
v0=180;
cd=0.007;
[x, y]=ode45(@projectile,[0,-300],[v0*cos(alpha) v0*sin(alpha) 0 0]',[],v0,cd,g);
N=length(x);
indx=find(y(:,3)>0);
height=@(v,x,y) (-spline(x,y,v));
opt = optimset('display','off');
xmax=fminbnd(height, 0, 1000, opt, x, y(:,3));
ymax=-height(xmax, x, y(:,3));
disp(['ymax = ' num2str(ymax) ' m at x = ' num2str(xmax) ' m']);
xe = interp1(y(indx(end)-5:N, 3), x(indx(end)-5:N), 0);
te = interp1(y(indx(end)-5:N, 3), y(indx(end)-5:N, 4), 0);
disp(['Travel distance = ' num23str(xe) ' m and time of travel = ' num2str(te) ' s'])
function p=projectile(x, z, v0, cd, g)
v = sqrt(z(1)^2+z(2)^2);
if v < v0*1e-6
error('Initail velocity not large enough, Increase')
end
p = [-cd*v; -(g+cd*v*z(2))/z(1); z(2)/z(1); 1/z(1)];
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!