function tesode23t=[0 11];initial_x=0;initial_dxdt=0;[t,x]=ode23s(@F,t,[initial_x initial_dxdt]);plot(t,x(:,1));xlabel('t'); ylabel('x');figure plot(t,x(:,2));xlabel('t'); ylabel('x');function dx=F(t,x)L=30;d=[0 3 14 17 20.525 23.025 42.2125 45.0125 67.5125 70.0125 92.5125 95.0125 117.5125 120.0125 142.5125 145.0125 167.5125 170.0125 192.5125 195.0125 217.5125 220.0125 242.5125 245.0125 267.5125 270.0125 292.5125 295.0125 317.5125 ];v=200*1000/3600;T=d/v;Tn=(L+d)/v; w=40;O=b*v/L; for i=1:length(t) for N=1:length(T); K1=t(i)-T(N); FAI1=heaviside(t(i)-T(N))-heaviside(t(i)-Tn(N)); dx=zeros(2,1); dx(1)=x(2); dx(2)=-w^2*x(1)-sum((cosh(O*K1)+sinh(O*K1)+cos(O*K1)+sin(O*K1))*FAI1); end endendend
MATLAB: I have a problem that when I use the Heaviside function in the For loop with ODE23 or ODE45, the code does not give exact results (always give 0)
ode23
Best Answer