Skip to content

© 2026 Rami Kronbi · Beirut, Lebanon

Back
Simulation · automotive

Four-cylinder engine simulator

A MATLAB model of a four-cylinder engine, from the motion of its pistons to the gear ratios of the car it drives.

My partSole author · course assignment

The engine running, drawn from the simulator’s own results: four cylinders firing 1-3-4-2, slowed 133 times.

In short

A MATLAB simulator I wrote as the main project of the Automotive Engineering course at RHU, in fall 2024, since reworked into a first-law cycle simulation. It follows a 2.0 L four-stroke, four-cylinder spark-ignition engine through a full 720° cycle: the slider-crank kinematics, the gas in a cylinder with its burn, the heat it loses to the walls and its flow through the valves, the crank torque of one cylinder and of all four, a flywheel and the crank speed it allows, full-load torque and power from 1000 to 6000 rpm, and the ratios of an eight-speed gearbox.

What it is
A MATLAB model of a 2.0 L four-stroke, four-cylinder spark-ignition engine, from the geometry of its crank through the gas in its cylinders to its torque curve and the gear ratios of the car it drives.
Why it matters
An engine is one chain of dependencies: the crank geometry sets the cylinder volume, the volume and the valves set how much charge a cylinder holds, the burn and the walls set the pressure, the pressure and the moving masses set the torque, and the torque decides the flywheel and what the gearbox has to work with. Simulating it means following that chain end to end.
My part
I wrote the simulator as the main project of the Automotive Engineering course at RHU in fall 2024. The version shown here is a later rework that fixes the original’s bugs and replaces its gas cycle.
A full run of the scripts in GNU Octave: every line they print and every figure they draw.

Step by step

  1. 1

    Parameters

    One file holds every input: link lengths and masses, the design speed and the speeds to sweep, valve and spark timing in crank degrees, gas and fuel properties, heat-transfer and friction constants, and the flywheel and vehicle constants.

  2. 2

    Kinematics

    A closed-form slider-crank solution gives piston position, velocity and acceleration, the rod’s motion and the cylinder volume at every crank angle, for a cylinder axis through the crankshaft (compression ratio 9.8) and one offset by −18 mm (10.1), checked against numerical derivatives.

  3. 3

    Thermodynamic cycle

    The first law for the gas in one cylinder, in steps of a tenth of a degree: a Wiebe burn (a = 5, m = 2) over 60° from a spark at 30° before top dead centre, Woschni heat transfer to the walls, and separate properties for fresh and burned gas.

    Pressure–volume diagram of one cylinder over a full cycle, coloured by phase.
    The P–V loop of one cylinder at 2000 rpm: 501 J of net work per cycle, from a peak of 59 bar.
    Heat release rate of the Wiebe burn and the heat lost to the walls, against crank angle.
    The burn releases the fuel’s heat over 60° from the spark; about a fifth of it goes to the walls.
  4. 4

    Gas exchange

    Valves that lift and fall, with compressible flow through them, choked or subsonic, in either direction: blowdown, backflow and the residual gas left behind. The cycle repeats until it comes back to the state it started from.

    Mass flow through the intake and exhaust valves over the cycle, with the valve lifts shaded.
    Choked blowdown as the exhaust valve opens, then the piston pushes the rest out; a little backflow at each end of the intake.
  5. 5

    Torque

    At each crank angle an 8×8 linear system of forces and moments, with masses, rod inertia, gravity, the gas force and the crankcase pressure, gives the crank torque, split into its gas and inertia parts; four cylinders 180° apart are summed.

    Crank torque of one cylinder, split into its gas and inertia parts.
    One cylinder’s torque, split into the push of the gas and the inertia of the moving parts.
    Crank torque of four cylinders 180° apart, summed, with its mean.
    Four cylinders 180° apart: a mean of 160 N·m before friction.
  6. 6

    Flywheel

    The largest swing in shaft energy over the cycle sizes a steel flywheel 50 mm thick for a 4% speed fluctuation, and the crank speed it allows is worked out from the torque.

    Crank speed over the cycle with the flywheel, around 2000 rpm.
    With a 10.6 cm flywheel the crank speed swings by the 4% it was sized for.
  7. 7

    Performance and speed sweep

    IMEP, pumping and friction losses (Heywood’s correlation), BMEP, power, torque, efficiencies, BSFC and volumetric efficiency, then the cycle again from 1000 to 6000 rpm for the full-load torque and power curves.

    Full-load brake torque and brake power from 1000 to 6000 rpm.
    Full load: 138 N·m at 1500 rpm and 45.1 kW at 4000 rpm.
  8. 8

    Gearbox

    Eight progressive ratios between a top speed of 245 km/h and a first gear for a 2600 kg vehicle on a 7 % grade, solved exactly, with the road speed in each gear.

    Bar chart of the eight gear ratios from 5.48 in first to 0.64 in eighth, with the road speed in each gear.
    The eight ratios, 5.48 down to 0.64, with the road speed in each gear at 4000 rpm.
  9. 9

    Animation

    The four cylinders running from the results: pistons, valves, gas temperature and a spark at each ignition.

Results

138 N·m
Peak torque
at 1500 rpm, full load
45.1 kW
Peak power
at 4000 rpm
28.6%
Brake efficiency
at 2000 rpm, 289 g/kWh
10.6 cm
Flywheel
radius of a 50 mm steel disc, for a 4% speed fluctuation
For engineersHow it is built, its limits, and its keywords.

How it works

One file holds every parameter. A closed-form slider-crank solution gives piston motion and cylinder volume at each crank angle. A first-law model follows the gas in one cylinder over the whole cycle, with a Wiebe burn, Woschni heat transfer and compressible flow through lifting valves, and repeats until the cycle comes back to where it started. A linear force and moment solve gives the crank torque; four phase-shifted cylinders are summed, and the result sizes a flywheel. Running the cycle from 1000 to 6000 rpm gives the full-load torque and power, and an eight-speed gearbox is designed for the vehicle.

Limits

  • A single-zone model: one temperature for the whole charge, a Wiebe burn rather than a combustion model, and no knock or emissions.
  • The manifolds are held at atmospheric pressure, and one 33 mm valve per port without overlap limits the breathing, which is why power peaks at 4000 rpm.
  • Heat transfer and friction come from published correlations (Woschni, Heywood), not measurements, and every result is at full load.
  • The gearbox is designed for the assignment’s 2600 kg vehicle and its 450 N·m engine torque, more than this 2.0 L engine makes.
  • The results come from GNU Octave; the scripts use only base MATLAB, but this version has not been run in MATLAB itself.

Keywords

  • MATLAB
  • internal combustion engine
  • four-stroke engine
  • slider-crank
  • engine thermodynamics
  • Wiebe function
  • flywheel design
  • gear ratios
  • Woschni heat transfer
  • engine cycle simulation
  • torque curve
  • GNU Octave

Read the write-up →