The problem
Campuses, depots and industrial sites increasingly combine solar, storage and EV charging behind one grid connection. Uncoordinated, the EVs can wipe out the benefit of solar and push the site into peak demand charges, while the stationary battery is cycled poorly. A coordinated controller that forecasts and optimises can reduce cost and grid stress, but it must be robust to forecast error and respect every driver's needs.
Objectives
- Build a microgrid model with PV, stationary battery, building load, EV chargers and a grid connection.
- Formulate a receding-horizon optimisation for cost, peak demand and battery wear.
- Integrate the optimiser with the Simulink plant through a co-simulation loop.
- Compare against rule-based and uncoordinated strategies on several seasonal scenarios.
- Quantify sensitivity to forecast error and fleet size.
What you need
Tools and toolboxes
- MATLAB — Forecasting, scenario generation, the receding-horizon loop and statistics.
- Simulink — Microgrid plant model with power-flow balance, battery SOC dynamics and data logging.
- Optimization Toolbox — LP or MILP formulation with optimproblem and solution with linprog or intlinprog at each control step.
- Simscape Electrical — Optional: an electrical-level DC or AC microgrid with converters for detailed validation of selected days.
Software
- MATLAB R2023b or later
- Simulink
- PV generation and building load data in CSV (measured or public)
- EV session data (real or synthetic)
Extra parts and data
None beyond the software above.
Key concepts
- Energy management system
- The supervisory controller that decides power setpoints for each asset every few minutes. It works on energy and cost over hours, while converter controllers handle fast electrical dynamics.
- Model predictive control for scheduling
- At each step, solve an optimisation over a forecast horizon, apply only the first decision, then repeat with updated measurements. This corrects for forecast errors over time.
- Battery wear cost
- A penalty per kWh of throughput or per cycle depth added to the objective, so the optimiser does not cycle the stationary battery or EVs for very small gains.
- Forecast error
- PV, load and EV arrivals are predicted, not known. Testing with realistic error shows whether the controller's benefit survives outside perfect-foresight conditions.
Step-by-step guide
- 1
Define the microgrid and research questions
Specify an example site: 100 kWp PV, a 200 kWh stationary battery, a 150 kW building peak load, 20 EV chargers at 11 kW with optional V2G and a 200 kW grid connection with a time-of-use tariff and a demand charge. State the research questions, the baselines (uncoordinated, rule-based) and the metrics: cost, peak import, PV self-consumption, battery throughput and EV energy shortfall.
Checkpoint: A written study plan with research questions, metrics, baselines and a site parameter table.
- 2
Prepare data and scenarios
Collect a year of hourly or 15-minute PV and building load data, either measured or from a public source, and build EV session data with arrival, departure and energy need. Select representative days by season (for example clear summer, monsoon, winter) and weekday or weekend. Build a forecast for each: a persistence or simple regression forecast, so forecast error is realistic and measurable.
Checkpoint: A set of scenario days with actual and forecast profiles, and forecast error statistics such as RMSE for each.
- 3
Build the plant model
In Simulink, build an energy-level plant at a 1-minute step: PV power input, building load input, battery and EV SOC integrators with charge and discharge efficiency, power limits, and a grid node that balances all flows. Add a curtailment path if export is not allowed. Keep the plant separate from the controller with clear setpoint inputs, so different controllers can be swapped.
Checkpoint: With zero setpoints the grid import equals load minus PV at every step, and SOC stays constant.
- 4
Implement the baselines
Uncoordinated: EVs charge at full power on arrival; battery idle. Rule-based: battery charges from surplus PV and discharges when load exceeds a threshold; EVs charge at full power when PV surplus exists or the tariff is in the low band, otherwise at a minimum rate. Implement both as MATLAB Function blocks or a Stateflow chart. Use the same plant model, data and metrics for every controller so differences come only from the control strategy. Record any EV energy shortfall at departure as a separate metric rather than hiding it inside cost.
Checkpoint: Both baselines run for every scenario day, with EVs meeting their energy needs in the rule-based case or shortfalls reported.
- 5
Formulate the optimisation
Write a receding-horizon LP over 24 hours at 15-minute steps. Variables: battery charge and discharge power, each EV's charge and discharge power, grid import and export, and peak import. Constraints: power balance at each step, SOC dynamics and limits for the battery and each EV, availability windows, departure energy targets and the grid limit. Objective: energy cost plus demand charge plus wear cost per kWh of throughput.
Checkpoint: The LP solves for a test day with perfect forecasts, and all constraints are satisfied within tolerance.
- 6
Close the loop
Write a MATLAB loop or use a MATLAB System block that, every 15 minutes, reads plant states, updates the forecast, solves the LP and passes the first-step setpoints to the Simulink plant. Run the plant for 15 minutes with sim() and the setpoints held, or step the model with a Simulink.SimulationInput and pause, then repeat. Log solve time per step.
Checkpoint: A full-day closed-loop run completes with solve times well under the 15-minute step and the plant states tracking the optimiser's plan.
- 7
Compare strategies
Run uncoordinated, rule-based, MPC with perfect forecasts and MPC with realistic forecasts for every scenario day. Compute cost, peak import, PV self-consumption, battery and EV throughput, and EV energy shortfall. Use the perfect-forecast MPC as an upper bound on achievable benefit and report how much the realistic-forecast MPC retains. Present results per scenario day and averaged, because a controller that wins on average may still lose badly on one type of day, such as a cloudy monsoon day with high evening load.
Checkpoint: A comparison table across strategies and scenario days, with MPC ranked against the baselines on every metric.
- 8
Sensitivity studies
Vary forecast error (scale the error by 0.5, 1, 2), fleet size (10, 20, 40 EVs), V2G permission (on or off) and wear cost weight. For each, compute the same metrics. Identify where the MPC advantage disappears, for example when forecast errors are very large, and whether V2G is worth its wear cost on this site. Change one factor at a time from a base case and keep the scenario days fixed, so each sensitivity curve isolates one effect and can be interpreted without confounding.
Checkpoint: Sensitivity plots for each factor with a written interpretation of where coordination matters most.
- 9
Electrical-level validation
For one or two representative days, build or reuse a Simscape Electrical model of the microgrid's DC or AC bus with average converter models, and drive it with the MPC setpoints. Check that bus voltage stays within limits and that converter power limits are respected. This confirms the energy-level plant has not hidden electrical constraints. Use average-value converter models with their own current and voltage controllers, and a fixed-step discrete solver, so the validation run is fast enough to cover a full day.
Checkpoint: Bus voltage stays within a stated band during the validation days and setpoints are tracked within converter limits.
- 10
Statistical analysis
Run the comparison across many days (for example a month per season) rather than a few. Report mean and spread of savings, and use a paired test to check that MPC savings are statistically significant against the rule-based baseline. Report computation time statistics. Report effect sizes as well as p-values, since a statistically significant saving can still be too small to matter in practice. Check whether the savings distribution is skewed before choosing a parametric or non-parametric paired test, and state which test you used.
Checkpoint: Mean savings with confidence intervals and a significance test result for the main comparison.
- 11
Write the thesis chapter or paper
Document the plant, formulation, closed-loop method and all results. Present the trade-off between cost, peak, wear and driver service. Release scripts and anonymised data so results can be reproduced. State the limitations: energy-level plant for most runs, a simple wear model and synthetic EV behaviour if real data were unavailable. Include a clear statement of novelty against the closest published work, and present the trade-offs honestly, including any metric on which the MPC did not beat the rule-based baseline.
Checkpoint: A chapter whose every table and figure is regenerated by released scripts.
Testing & validation
- Power balance holds at every step in the plant model.
- All EV departure energy targets met or shortfall explicitly reported.
- Perfect-forecast MPC bounds the realistic MPC from below on cost.
- Electrical-level validation keeps bus voltage within limits on selected days.
Expected results
A closed-loop microgrid energy management study with a receding-horizon optimiser, compared against rule-based control over many scenario days, with quantified savings, robustness to forecast error and electrical validation.
Research angle for your PhD
Research questions: how much of the perfect-foresight benefit of coordinated EV, PV and storage scheduling can a practical MPC retain under realistic forecast error, and how does that depend on fleet size, V2G and wear cost? Baselines: uncoordinated and rule-based control, plus perfect-foresight MPC as an upper bound. Metrics: total cost, peak import, PV self-consumption, throughput-based wear and EV energy shortfall. Use measured or public PV and load data and, ideally, real charging-session data. Publishable contributions include a robust or stochastic formulation that measurably closes the gap to perfect foresight, or a careful multi-season study with released code and data.
Take it further
- Replace the deterministic LP with stochastic or chance-constrained MPC.
- Add an electrochemical ageing model for the stationary battery.
- Add a learned forecaster and compare with persistence.
- Include voltage constraints from a distribution network model.
Viva and review questions
Why apply only the first step of the optimised plan?
The plan is based on forecasts that will be wrong. Re-solving each step with fresh measurements corrects errors, which is the core idea of receding-horizon control.
Why include a wear cost in the objective?
Without it the optimiser cycles batteries for tiny cost gains, which shortens their life. A wear cost makes it cycle only when the saving justifies the degradation.
How do you know your benefit is not just from perfect forecasts?
I ran the MPC with realistic forecasts and compared it with the perfect-forecast case. The gap shows how much benefit is lost to forecast error.
Why validate with an electrical model?
The energy-level plant ignores bus voltage and converter dynamics. Running selected days through an electrical model checks the setpoints are physically achievable.
Learn it at DIYguru
These DIYguru courses and programmes teach what this project needs.
