USE MATLAB FOR CODE Population growth can be modeled via a differential equation
ID: 3865711 • Letter: U
Question
USE MATLAB FOR CODE
Population growth can be modeled via a differential equation of the form
Assume that no individuals leave the population during the time interval of interest.
Complete the following:
Numerically solve this population growth problem using the forward Euler integration technique. Use a time step size of 2 years. Store your derivative term in an anonymous function and use that anonymous function in your forward Euler solution implementation. You may recall that we discussed anonymous functions in Lecture 07.
Next, numerically solve this population growth problem using the built-in function ode45. Instead of using a separate programmer-defined function file, however, define your function inside the ode45 function call like such:
ode45(@(t,p) your_function, ...)
Remember that one of the inputs to ode45 is another function; here, we are merely defining what the function is inside of the ode45 function call instead of listing the name of another function created elsewhere. The programming paradigm of using functions as inputs to other functions is aptly known in computer science as "functional programming."
Remember that the results from the forward Euler and ode45 numerical solution approaches are approximate. You may reasonably anticipate, therefore, some error between the numerical solutions and the analytic solution. Generally, however, the more rigorous the numerical solution approach is, the better the approximate numerical solution will be. The trade-off between effort required and solution quality is a common theme in many STEM fields.
Hints:
For the forward Euler portion of this problem, note I am asking you to code the algorithm we discussed in slides 15-18 of Lecture 25. In that lecture, I showed you how to perform the operation by hand; now, you will implement the algorithm programmatically.
A sample solution plot (created without specifying ode45 tolerances or ode45 time step sizes) follows:
dP(t)dt=k P(t) (5) m = k P(t) dtExplanation / Answer
tspan = 0:2:40; P0 = 10; [t,P] = ode45(@(t,P) 0.1*P, tspan, P0); plot(t,P,'-.r*') y0=10; i=1; for i=1:21 f(i,1)=0.1*y0; y(i,1)=y0+2*f(i,1); y0=y(i,1); end hold on plot(t,y,':bs') i=1; t0=0; for i=1:21 yd(i,1)=10*exp(0.1*(t0+2*(i-1))); i=i+1; end hold on plot(t,yd,'--go') xlabel('Time[Years]') ylabel('Population size[Individuals]') title('Analytic and Numeric Solutions for dP(t)/dt=k P(t)') legend('Exact Solution','Forward Euler Solution','ode45 Solution','Location','northwest','Orientation','vertical')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.