Index of zero error
Afficher commentaires plus anciens
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)];
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!