I asked this question to help me understand what is going on in one of Maxwell's equations. I am happy with following through the maths on paper now, but would like to use MATLAB to take it one step further, and actually DO the maths on a computer. This is where I really struggle, because most of what I have used computers for is curve fitting, numeric or symbolic integration, and data processing.

So in this case, I have the equation:

$$ \nabla \times E = -\frac{\partial \textbf{B}}{\partial t} $$

and would like to DO this equation in MATLAB, in full 3D, using the proper term for the electric and magnetic counterparts:

$$ E \left( r, t \right) = E_{0} \exp \left[ i \left( k_{0} \cdot r - \omega_{0} t \right) \right] $$

to eventually obtain the canonical diagram of an electromagnetic wave (thank you THIS thread for this pic):

enter image description here

Now I am pretty sure it has something to do with meshgrid() and MATLAB's curl() function but have no idea where to start. I hope this question makes sense. Thanks

  • $\begingroup$ Are you trying to reproduce that plot? $\endgroup$
    – nicoguaro
    Aug 10, 2014 at 18:33
  • $\begingroup$ Yes, by using the plane wave equation, I would like to have a vector field showing that if we take the curl of E, we get a flipped out of phase cos wave. And then integrating that gets us to our E-M plot. But all of this in MATLAB. $\endgroup$ Aug 11, 2014 at 7:40

2 Answers 2


Here I have an example:

x = linspace(-5,5,100);
y = linspace(-5,5,100);
z = linspace(-5,5,100);

[X, Y, Z] = meshgrid(x, y, z);

Ex = sin(2*pi/5*Z);
Ey = 0*X;
Ez = 0*X;

[Bx, By, Bz, V] = curl(X, Y, Z, Ex, Ey, Ez);

Eplot = 0*x;
Bplot = 0*x;
for i=1:100  %% Integration-like procedure
    Eplot(i) = mean(mean(Ex(:,:,i),1),2);
    Bplot(i) = mean(mean(By(:,:,i),1),2);

plot3(0*x, y, Eplot, 'b', 'LineWidth', 2); hold on
h = quiver3(0*x(1:3:100), y(1:3:100), 0*z(1:3:100), 0*x(1:3:100), 0*y(1:3:100), Eplot(1:3:100), 0, 'b', 'LineWidth', 1);
set (h, "maxheadsize", 0.0);
plot3(Bplot, y, 0*z, 'g', 'LineWidth', 2);
h = quiver3(0*x(1:3:100), y(1:3:100), 0*z(1:3:100), Bplot(1:3:100), 0*y(1:3:100), 0*z(1:3:100), 0, 'g', 'LineWidth', 1);
set (h, "maxheadsize", 0.0);
grid on, axis square

I don't understand what do you mean with integration, so I computed the average over each plane (that is the same that plotting over a line in this case). This was done in Octave since I don't have a Matlab license in my laptop, but it should work. Here is the image that I obtained: enter image description here

  • 1
    $\begingroup$ That's incredible. Exactly what I was after, Thanks so much! The main reason I asked for integration was because when we take the curl(E) we get -dB/dt. So our magnetic field currently is a quarter of a wavelength out of Phase. The integration of that puts in in phase. But with your excellent code I can probably figure out out to do this. Thanks again $\endgroup$ Aug 12, 2014 at 5:34

From browsing some forum posts, it appears that the numerical solution to these equations is not trivial to obtain. See, for example, this discussion (admittedly dated now). You indicate in your post that you are relatively new to numerical PDE solvers, and so the following references may be more involved than you were hoping for. In particular, they require you to write your own solver, as opposed to calling ode45 or some other canned routine.

Reference 1: Following a brief theoretical discussion of the system, this document presents several explicit schemes for solving the system, beginning with a 1D wave equation. These solutions are complete with psuedo-code and thorough discussion, and proceed from the simpler finite-difference schemes for a 1D problem to more advanced 2D and 3D eigenvalue problems. Note that there is no Matlab code here, so you will need to translate the pseudo-code into Matlab if this is the language/software you wish to use. There are several exercises on convergence rates and stability as you proceed through the various solutions. Following this text will likely take you several hours (I know it would for me), but if you are new to numerical PDEs this is a fantastic way to learn a lot relatively quickly. I haven't read through all of it but I believe the numerics are strictly finite differences.

Reference 2: This is more advanced, and presents the finite element method (FEM) to solving Maxwell's equations. It also appears to deal with some advanced applications that are probably not of interest to you. Nonetheless, the original system is completely solved in the first chapter using FEM. If you haven't heard of FEM or finite differences, I recommend starting with the latter as it tends to be more intuitive (although tedious at times).

Please let me know if you would like any references to texts on finite differences or FEM. Also feel free to ask me to look at any code if you decide to try to write it and get stuck anywhere.

  • $\begingroup$ Dear Eric, thanks a lot for your post. That first link looks very helpful for me, I will definitely learn a lot from it. But for what I am after, (and I may well be wrong), we can obtain a simplistic analytical approach. It is my understanding that any real world solution attempt will involve complicated boundary conditions of which there is no simple way to solve the PDE's except numerically. But in this case, I am pretty sure we can somehow specify a vector field in MATLAB of which its curl its analytically obtainable to get that E-M wave.... (I think) $\endgroup$ Aug 10, 2014 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.