function [ s ] = harmonic(ms,omegas,damprat,ug,omegax)ks=ms*omegas^2;u(1)=0;u(2)=0;a(1)=0;cd=2*ms*omegas*damprat;k=ms/(0.01)^2+cd/0.02;x=ms/(0.01)^2-cd/0.02;y=ks-2*ms/(0.01)^2;for t=0:0.01:10 for n=1:1:1001 if n==(t*100+1) p(n)=-ms*ug*sin(omegax*t)-x*u(n)-y*u(n+1); u(n+2)=p(n)/k; a(n+1)=(u(n+2)-2*u(n+1)+u(n))/(0.01)^2; fprintf('t=%5d\t%10.3f\n',t,a(n)) plot(t,a(n)) end endend
When I run the above function in command window, I get the following:
harmonic(820.116,7.068,0.014,70,6.283)t= 0 0.000t=1.000000e-002 0.000t=2.000000e-002 -4.352t=3.000000e-002 -6.449t=4.000000e-002 -5.280t=5.000000e-002 -1.471t=6.000000e-002 3.059t=7.000000e-002 6.072t=8.000000e-002 6.123t=9.000000e-002 3.260t=1.000000e-001 -1.027t=1.100000e-001 -4.576t=1.200000e-001 -5.631t=1.300000e-001 -3.705??? Attempted to access u(17); index out of bounds because numel(u)=16.Error in ==> harmonic at 13 p(n)=-ms*ug*sin(omegax*t)-x*u(n)-y*u(n+1);
Please suggest why does this happen?
Best Answer