Main Content

Generate MEX Code to Calculate Geodesics in Curved Space-Time

These examples are using Einstein's General Relativity to calculate geodesics in curved space-time.

Prerequisites

There are no prerequisites for this example.

Example: Computing the Precession of the Planet Mercury

This example computes the precession of the planet Mercury numerically. The precession is a slight rotation of the elliptical orbit around the sun. Analytically, using the equations of general relativity the value is extremely small, an extra 43" (arc seconds) per century. An arc second is 1/3600th of one degree (counting 360 degrees for a complete revolution.) Even though the extra precession is extremely small it matches exactly with observation. Pure Newtonian mechanics (if we choose to ignore all the other planets of our solar system) predicts no precession.

This application is using Euler's method with variable time step where the major time step is .5 seconds. We reduce the time step as we approach one complete revolution. The precession is computed as the planet is reaching its maximum distance from the sun for which we compute its relative angle to the coordinate axis.

Generate the MEX Function: Precession of the Planet Mercury

Generate a MEX function using the command codegen followed by the name of the MATLAB file to compile.

codegen gr_mercury_precession
Code generation successful.

The gr_mercury_precession function calls other MATLAB functions, but you need to specify only the entry-point function when calling codegen.

By default, codegen generates a MEX function named gr_mercury_precession_mex in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.

Run the MEX Function: Precession of the Planet Mercury

Run the generated MEX function.

gr_mercury_precession_mex
Progress: 5%
Progress: 10%
Progress: 15%
Progress: 20%
Progress: 25%
Progress: 30%
Progress: 35%
Progress: 40%
Progress: 45%
Progress: 50%
Progress: 55%
Progress: 60%
Progress: 65%
Progress: 70%
Progress: 75%
Progress: 80%
Progress: 85%
Progress: 90%
Progress: 95%
Progress: 100%
precession: 0.10468" (0 years 87.87009 days) => 43.481"/century

Figure mercury_plot: 0 years 88 days contains an axes object. The hidden axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

Example: Ray-tracing a Black Hole

Einstein's equations of motion in general relativity can handle any object at any speed, so let's apply it to photons that travel with the speed of light. In this configuration we have a black hole in front of a background image. To make the effect more visible, we increase the mass of the black hole to astronomical proportions as well as the background image. In this way we can study the effects of gravitational lensing; the background image becomes distorted by the curved space-time produced by the black hole.

Generate a MEX Function: Ray-tracing a Black Hole

codegen gr_raytrace
Code generation successful.

Run the MEX Function: Ray-tracing a Black Hole

Ray-tracing the picture takes a minute or two on a 2 GHz x86 machine. On your screen, you see the original picture (the Vittorio Emanuele Mall in Milano, Italy) and, to the right, the rendered image of the same picture with a black hole in front of it.

gr_raytrace_mex('mall.jpg');

Figure Original Picture contains an axes object. The hidden axes object contains an object of type image.

Progress: 5%
Progress: 10%
Progress: 15%
Progress: 20%
Progress: 25%
Progress: 30%
Progress: 35%
Progress: 40%
Progress: 45%
Progress: 50%
Progress: 55%
Progress: 60%
Progress: 65%
Progress: 70%
Progress: 75%
Progress: 80%
Progress: 85%
Progress: 90%
Progress: 95%
Progress: 100%

Figure Ray-traced with Black Hole contains an axes object. The hidden axes object contains an object of type image.