I created a tokamak fusion reactor simulation in Matlab as part of a project at university. The two code samples I attached are a simulation of a nuclear fusion reactor. Just like the one in the Culham Centre for Fusion Energy in the UK. It demonstrates how charged high-energy particles can be held in one magnetic bottle (the Tokamak), without touching its walls and melting it. The charged particles are held together by a magnetic field produced by current coils and a rotating charged plasma cloud. The magnetic field has a very complicated shape, as it is created by multiple current coils and a rotating charged plasma cloud. The calculation of the magnetic field was performed algebraically, which significantly increased the speed and precision of the simulation.
Simply run one of the simulations in Matlab. Simulation1.m is a magnetic bottle, while Simulation2.m is a Tokamak.
The functions B1.m and B2.m are there to calculate the magnetic field for Simulation1.m and Simulation2.m
The function plotCircle3D.m is there to support the 3D animation.