A mathematical model is a question written as an equation: how fast will this grow, when will it level off, what happens if one input changes? Mathematical modeling in MATLAB comes down to three steps: write the model, solve it, and check it against data. This lesson walks through all three with a model you’ll meet everywhere, from population biology to product adoption.
Playing loads the video from YouTube. Watch on YouTube
The code uses base MATLAB only, no toolboxes, and also runs in GNU Octave. Where an example uses random numbers, your figures may differ slightly from the ones shown, because random number generators differ between versions.
Step 1: write the model
Logistic growth describes something that grows quickly at first and then slows as it approaches a limit: a population in a habitat with limited food, or users of a product in a market of fixed size. With P the population, r the growth rate and K the capacity, the model says how P changes over time:
dP/dt = r * P * (1 - P / K)When P is small, the bracket is close to 1 and growth is almost exponential. As P approaches K, the bracket approaches 0 and growth stops. Two numbers, r and K, describe the whole curve, which is what makes the model useful: you can estimate them from data and then reason about the future.
Step 2: solve it with ode45
ode45 solves differential equations numerically. You give it the right-hand side as a function of time and state, the time span and the starting value. Because logistic growth also has an exact solution, we can check how close the numerical answer is:
r = 0.8; K = 1000; P0 = 10;
f = @(t, P) r * P .* (1 - P / K);
[t, P] = ode45(f, [0 15], P0);
exact = K ./ (1 + (K / P0 - 1) * exp(-r * t));
fprintf('Largest difference from the exact solution: %.4f\n', max(abs(P - exact)));
fprintf('Population at t = 15: %.1f\n', P(end));Largest difference from the exact solution: 0.1541
Population at t = 15: 999.4A difference of about 0.15 on a population of 1,000 is small enough for most purposes. If you need more accuracy, pass tighter tolerances with odeset('RelTol', 1e-8). Plot the result with plot(t, P) to see the S-shaped curve.
Step 3: worked example, fit the model to real data
A model only earns trust when it matches data. Suppose a new app gained these users over its first 12 months, and you want to know where growth will level off. We define the model with r and K as unknowns, write the sum of squared errors between model and data, and let fminsearch find the values that make it smallest:
months = (0:11)';
users = [12 19 31 50 79 120 175 240 305 360 400 428]';
model = @(p, t) p(2) ./ (1 + (p(2) / users(1) - 1) * exp(-p(1) * t));
sse = @(p) sum((model(p, months) - users).^2);
best = fminsearch(sse, [0.5, 500]);
fprintf('Fitted growth rate r = %.3f, capacity K = %.0f\n', best(1), best(2));
fprintf('Forecast for month 18: %.0f users\n', model(best, 18));Fitted growth rate r = 0.521, capacity K = 487
Forecast for month 18: 485 usersThe fit says the app is close to its ceiling of about 487 users: growth will slow sharply from here. That’s a useful, testable statement. If the next few months come in well above the forecast, the model is wrong, perhaps because the market grew, and you refit it.
Checking a model honestly
- Plot the fitted curve over the data with
plot(months, users, 'o', months, model(best, months)). A good fit follows the data without systematic misses. - Fit on the first months only and compare the forecast with the months you held back. A model that can’t predict data it hasn’t seen won’t predict the future either.
- Try a starting guess far from the answer. fminsearch can stop at a poor local minimum; if different starts give different answers, the data doesn’t pin the model down.
Where to take it next
The same three steps work for any model: write it as a function, solve or simulate it, and fit its unknowns to data. Swap logistic growth for a model of cooling, drug concentration or loan repayments and the code barely changes. The next lessons in this series use MATLAB for numerical methods, optimization and machine learning on real problems.
Try it yourself
- Change the starting value P0 to 100 and to 900. How does the shape of the curve change, and why does growth still stop at K?
- Fit the model to only the first eight months of data. How far is its forecast from the last four months you held back?
- Replace logistic growth with exponential growth, dP/dt = r*P, and fit it to the same data. Which model would you trust for month 18, and why?
Key points
- A model is a question written as an equation, with a few unknowns you can estimate.
- Solve it numerically with ode45 and check the solver against an exact answer when one exists.
- Fit the unknowns to data with fminsearch, then test the model on data it hasn’t seen.
Want to go further?
I teach professionals one to one, and I build these systems for businesses. See Python AI mentoring or see what I build.