@joyphys 2015-08-13T14:57:00.000000Z 字数 1811 阅读 3094

# Matlab数值计算最简单的一个例子——指数衰减

Blog

dN(t)dt=N(t)τ(1)

N(t)=N(0)exp(t/τ)

  dsolve('DN=-N/tau')  ans =  C3*exp(-t/tau)

ti=(i1)Δt,i=1,2,,npoints

dN(t)dtN(t)N(tΔt)Δt

N(ti+1)=N(ti)N(ti)Δtτ

%% Exponent decay% 'Computational Physics' book by N Giordano and H Nakanishi% Section 1.2 p2% Solve the Equation dN/dt = -N/tau% by Joyful Physics Blog% ------------------------------------------------------------N_nuclei_initial = 100; %initial number of nucleinpoints = 101; % Discretize time into 100 intervalsdt = 0.05; % set time step tau=1; % set time constantN_nuclei = zeros(npoints,1); % initializes N_nuclei, a vector of dimension npoints X 1,to being all zerostime = zeros(npoints,1); % this initializes the vector time to being all zerosN_nuclei(1) = N_nuclei_initial; % the initial condition, first entry in the vector N_nuclei is N_nuclei_initialtime(1) = 0; %Initialise timefor step=1:npoints-1 % loop over the timesteps and calculate the numerical solutionN_nuclei(step+1) = N_nuclei(step) - (N_nuclei(step)/tau)*dt;time(step+1) = time(step) + dt;end% calculate analytical solution belowt=0:0.05:5;N_analytical=N_nuclei_initial*exp(-t/tau);% Plot both numerical and analytical solutionplot(time,N_nuclei,'ro',t,N_analytical,'b'); %plots the numerical solution in red and the analytical solution in bluexlabel('Time (s)')ylabel('Number of nuclei')text(2,80,'Time constant = 1s')text(2,70,'Time step = 0.05s')

• 私有
• 公开
• 删除