% slopef.m function z=slopef(ftx,ng) % SLOPEF(ftx,ng) plots the slope field for the ODE x'=f(t,x) where the string % variable ftx contains contains the expression in t and x defining the right % hand side. A grid using ng points both vertically and horizontally is used, % covering the ranges obtained from the axis command. ax=axis; %plot([],[]) cla; axis(ax); hold on; tg=linspace(ax(1),ax(2),ng); xg=linspace(ax(3),ax(4),ng); dt=tg(2)-tg(1); dx=xg(2)-xg(1); for i=1:ng t=tg(i); for j=1:ng x=xg(j); v=eval(ftx); d=.3/max(1/dt,abs(v)/dx); plot([t-d,t+d],[x-v*d,x+v*d],'g'); xlabel(['Slopefield: x'' = ',ftx]) end end