Skip to main content

Command Palette

Search for a command to run...

MATLAB vs Julia: Best Programming Language for Renewable Energy Simulations

Learn how Julia blows away MATLAB in renewable energy simulations.

Updated
6 min read
MATLAB vs Julia: Best Programming Language for Renewable Energy Simulations
G

Modeling & Simulation, HPC, and Enterprise Software all under one roof.

Great Lakes Consulting Services, Inc., a premier Information Technology Consulting company, serving others in IT staffing, analytic consulting, business intelligence and application development since 2009. We now specialize in custom Julia software services as the trusted partner to JuliaHub for their Consulting Services. Since 2015, we’ve partnered together to develop high-performance Julia code for low-latency data visualization and analytic solutions, high performance financial modeling, Modeling and Simulation for multiple sciences, personal Julia training, and legacy code migration & evolution.

This post was written by Steven Whitaker.

At GLCS, we're proud to have delivered innovative projects across top industries, including renewable energy, aerospace, and biomedical engineering. Our comprehensive Modeling and Simulation Services empower clients to elevate their designs, whether rewriting models in Julia for greater efficiency or unlocking cutting-edge features to solve complex problems. With deep expertise spanning several engineering and scientific domains, including computational fluid dynamics, thermodynamics, controls, biomedical engineering, and chemistry, we are your trusted partner in pushing modeling boundaries and achieving breakthrough results.

In this post, we'll focus on systems modeling for renewable energy.

Renewable energy systems, from wind farms to wave power, require precise modeling and simulation. Selecting the optimal computational tools is crucial; it can significantly accelerate development, reduce costs, and drive the transition to a sustainable future.

Both MATLAB® and Julia are widely used in engineering, particularly for solving differential equations. This post compares how each handles a renewable energy modeling scenario, the steady axisymmetric turbulent wake behind a wind turbine, ultimately showing why Julia has the edge. Maximize efficiency and energy output with cutting-edge technologies designed for tomorrow’s energy.

Steady Axisymmetric Turbulent Wake

When air flows past a wind turbine, a wake forms downstream. The wake velocity deficit impacts turbine spacing, efficiency, and power output.

A simplified steady axisymmetric turbulent wake equation is:

\[ \frac{\partial U}{\partial x} = \nu_t \cdot \left( \frac{\partial^2 U}{\partial r^2} + \frac{1}{r} \frac{\partial U}{\partial r} \right) \]

where:

  • $ U $ is the axial velocity.
  • $ x $ is the downstream distance.
  • $ r $ is the radial coordinate.
  • \( \nu_t \) is the turbulent viscosity.

This is a reduced form of the momentum equation, capturing diffusion of momentum due to turbulence.

MATLAB vs Julia: ODE/PDE System Set-up

Let's see how to convert the math into code and solve this PDE.

  • MATLAB:

    function dUdx = wake_eq(x, U, p)
        % Radial step size
        dr = p.r(2) - p.r(1);
    
        % First derivative (central difference)
        dUdr = (U(3:end) - U(1:end-2)) / (2 * dr);
    
        % Second derivative (central difference)
        d2Udr2 = (U(3:end) - 2 * U(2:end-1) + U(1:end-2)) / dr^2;
    
        dUdx = zeros(size(U));
        dUdx(2:end-1) = p.nu_t * (d2Udr2 + dUdr ./ p.r(2:end-1));
    end
    
    U0 = initial_profile();
    xspan = [0 100];
    p.nu_t = 0.05;
    p.r = linspace(0, 1, numel(U0));
    
    prob = ode;
    prob.ODEFcn = @dUdx;
    prob.InitialTime = xspan(1);
    prob.InitialValue = U0;
    prob.Parameters = p;
    prob.Solver = "ode45";
    
    sol = solve(prob, xspan(1), xspan(2));
    
  • Julia:

    using DifferentialEquations
    
    @kwdef mutable struct WakeParams{T}
        ν_t::Float64
        const r::T
    end
    
    function wake_eq!(dU, U, p, x)
        # Radial step size
        dr = p.r[2] - p.r[1]
    
        # First derivative (central difference)
        dUdr = (U[3:end] .- U[1:end-2]) ./ (2 * dr)
    
        # Second derivative (central difference)
        d2Udr2 = (U[3:end] .- 2 .* U[2:end-1] .+ U[1:end-2]) ./ dr^2
    
        dU[1] = dU[end] = 0
        dU[2:end-1] .= p.ν_t .* (d2Udr2 .+ dUdr ./ p.r[2:end-1])
    end
    
    U0 = initial_profile()
    xspan = (0.0, 100.0)
    p = WakeParams(; ν_t = 0.05, r = range(0.0, 1.0, length(U0)))
    
    prob = ODEProblem(wake_eq!, U0, xspan, p)
    solver = Tsit5()
    
    sol = solve(prob, solver)
    

(Note that, in practice, the boundary conditions for \( r = 0 \) and \( r = 1 \) would need to be handled with more care.)

As you can see, the syntax for Julia and MATLAB is quite similar. However, there are some key differences between the two approaches:

  • Julia's broadcasting (.=, .*, etc.) is explicit and fast.
  • Julia can use in-place functions to minimize memory allocations, boosting performance.
  • The Julia code for the dynamics above was written to look more like the MATLAB code. However, additional easy performance optimizations are possible to reduce memory allocations.
  • Julia's DifferentialEquations.jl supports many solvers with a unified interface. MATLAB supports only a limited set of solvers.

Event Handling and Callbacks: Wind Gusts

Now let's add a gust at \( x = 50 \) that will change \( \nu_t \) for \( x \ge 50 \).

  • MATLAB:

    function v = events_func(x, U, p, gust_position)
        % Event occurs when `x == gust_position`.
        v = x - gust_position;
    end
    
    function [stop, U, p] = callbacks_func(x, U, ie, p)
        stop = false;
        % Check if the event occurred.
        if ismember(1, ie)
            p.nu_t = 0.08;
        end
    end
    
    gust_position = 50;
    
    event = odeEvent;
    event.EventFcn = @(x, U, p) events_func(x, U, p, gust_position);
    event.Response = "callback";
    event.CallbackFcn = @callbacks_func;
    
    prob.EventDefinition = event;
    
    sol = solve(prob);
    
  • Julia:

    function gust_affect!(integrator)
        integrator.p.ν_t = 0.08
    end
    
    gust_position = 50.0;
    callback = PresetTimeCallback([gust_position], gust_affect!)
    
    sol = solve(prob, solver; callback)
    

When it comes to events and callbacks, Julia's approach is better:

  • Julia's callbacks are better organized; events and corresponding callbacks can be defined next to each other in the code, instead of separated across files as is typical in MATLAB.
  • Multiple events can be combined cleanly with CallbackSet, no need to try to cram multiple events into a single function like you have to do in MATLAB.
  • Julia keeps track of what events are triggered. In MATLAB, you have to check what events were triggered manually.
  • Julia provides many pre-defined callbacks (in DiffEqCallbacks.jl) that often have to be manually implemented in MATLAB.

Other Considerations

In addition to the differences in solving differential equations, here are some other key differences between Julia and MATLAB:

  • Performance: Julia's JIT-compilation and method specialization yield C-like speeds. Large-scale renewable energy simulations run faster and scale better, especially for parameter sweeps. You can expect to see 50--150 times faster code with Julia.
  • Workflow: MATLAB offers a polished GUI and plotting out of the box. However, Julia offers open-source freedom, easy integration with Python/C, and a thriving ecosystem.
  • Licensing: MATLAB requires a paid license; Julia is entirely free and open-source.

Summary

In this post, we saw how Julia and MATLAB compare for defining and solving steady axisymmetric turbulent wake. Both languages can model renewable energy systems effectively. However, Julia offers:

  • Performance: JIT speed for large systems.
  • Flexibility: Powerful callbacks and open integrations.
  • Cost: No license fees.
  • Modern syntax: Designed for productivity and clarity.

Ready to revolutionize your renewable energy simulations? Transition your MATLAB models to Julia and experience unparalleled speed, flexibility, and long-term maintainability. Reach out today and let's accelerate your green energy innovations together!

Worried about the technical hurdles and costs of switching from MATLAB to Julia? Discover our cutting-edge Julia-MATLAB Integration. We develop high-performance Julia models that seamlessly connect with your existing MATLAB codebase, minimizing risks while maximizing your return on investment. Transition smarter, faster, and more cost-effectively with our expert solutions!

Additional Links

MATLAB is a registered trademark of The MathWorks, Inc.

Julia versus X

Part 1 of 1

Compare Julia Language versus other languages and tooling.