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)];

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 8월 2일

0 개 추천

indx=find(y(:,3)>0);
when there are no y(:,3)>0 indx is empty and thus its 'end' ( indx(end)) is indx(0) which doesn't exist!
what you should do here is add an isempty if statement before the interpolation
if isempty(indx)
error('doh!') %or whatever
else
what you have now
end
Also, it's worth your time to learn to use the debugger.
dbstop if error
will point you right to it.

댓글 수: 2

Joseph
Joseph 2011년 8월 2일
Okay I get what your saying but that doesnt really help, although thanks for locating the problem. Why are there no y(:,3)>0? there should be because I got the values from the ode45 command and it should have the positive values in it.
Joseph
Joseph 2011년 8월 2일
Actually nevermind, I found out why there were no y(:,3)>0. In the ode45 function is should have been 300 not -300. Thanks for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by