% F1_10.m % Carol Lucas % To make life easy for entering changes, suggest that you set up a % matrix in the command window, e.g., set parameters at starting values % then move them to a matrix, e.g. to start with text example. % tau=3;noinf=.5;noinit=1;h=.2;time=15; % A=[tau noinf noinit h time]; % then just enter A at the prompt signal. Anytime you change a parameter, % just be sure to reactivate the 'A=[...' line. % Hitting return without 5 entries leads to default values of F1_10. clear t no nexact P=input('Please enter[tau ninf noinitial h time] as row matrix: '); if length(P)~=5; tau=3;noinf=.5;noin=1;h=.2;tend=15; else tau=P(1);noinf=P(2);noin=P(3);h=P(4);tend=P(5); end; tstart=0;n=1;no(1)=noin;t(1)=tstart;nexact(1)=noin; figure(1);clf; plot([tstart tend],[noinf noinf]); xlabel('time');ylabel('no'); title('dno/dt = -(no-ninf)/tau, euler=blue, exact=green, matlab=red'); axis([tstart tend 0 1]); hold on; % This demonstrates the simplest ode solver - euler, whole step, no correction. while t(n)<=tend; ndot=(noinf-no(n))/tau; % This is usually a function call, e.g.'nofun' no(n+1)=no(n)+h*ndot; t(n+1)=t(n)+h; n=n+1; nexact(n)=noinf+(noin-noinf)*exp(-t(n)/tau); plot(t(n-1:n),no(n-1:n),'b',t(n-1:n),nexact(n-1:n),'g');pause(.8); end % CHECK WITH MATLABS ROUTINES input('Hit any key to see MATLAB ODE SOLVER ode23') [t1,n1]=ode23('nofun',t,noin,[],tau,noinf); figure(1);plot(t1,n1,'r');