RunMat
GitHub

peaks — Sample data: 3-D test surface on an n-by-n grid.

peaks evaluates the following function over an n×n grid spanning [-3, 3] × [-3, 3]:

Z = 3*(1-x)^2 * exp(-x^2 - (y+1)^2)
    - 10*(x/5 - x^3 - y^5) * exp(-x^2 - y^2)
    - 1/3 * exp(-(x+1)^2 - y^2)

It is commonly used to demonstrate 3-D surface plots. RunMat matches MATLAB's formula and default grid size.

How peaks works in RunMat

  • peaks with no arguments returns a 49×49 matrix (MATLAB default).
  • peaks(n) returns an n×n matrix evaluated on an n-point grid from -3 to 3 on both axes.
  • peaks(X, Y) evaluates the formula at caller-supplied coordinate matrices and returns a Z matrix of the same size.
  • [X, Y, Z] = peaks(…) returns all three matrices — the X and Y coordinate grids and the computed Z surface.
  • peaks(0) returns an empty 0×0 matrix.
  • When exactly two outputs are requested, [X, Y] = peaks(n) returns only the coordinate grids without Z.

How peaks runs on the GPU

RunMat computes peaks on the host and returns a host tensor. No acceleration provider hooks are defined for this builtin.

GPU memory and residency

peaks always runs on the host CPU today. If you need the result on the GPU, wrap the call in gpuArray(...).

Examples

Default 49×49 surface

Z = peaks;
surf(Z);

Custom grid size

Z = peaks(30);
surf(Z);
colormap('parula');
colorbar;

Full three-output form

[X, Y, Z] = peaks(40);
surf(X, Y, Z);
shading interp;
colorbar;
view(45, 30);

Caller-supplied coordinate matrices

[X, Y] = meshgrid(linspace(-3, 3, 50), linspace(-3, 3, 50));
Z = peaks(X, Y);
contourf(X, Y, Z);
colorbar;

Combine with contour

[X, Y, Z] = peaks(50);
subplot(1, 2, 1);
surf(X, Y, Z);
view(3);
subplot(1, 2, 2);
contourf(X, Y, Z);
colorbar;

FAQ

Why is the default size 49?

MATLAB chose 49 as the default grid size for peaks. RunMat matches this specification.

Can I get the coordinate grids back as well as Z?

Yes. Use [X, Y, Z] = peaks(n) to retrieve all three matrices.

How do I move the result to the GPU?

Call gpuArray(peaks(n)) if you need the output on the GPU. RunMat computes peaks on the host today.

What happens with peaks(0)?

peaks(0) returns an empty 0×0 tensor, consistent with how other array-creation functions handle a zero size.

These functions work well alongside peaks. Each page has runnable examples you can try in the browser.

surf, mesh, contour, meshgrid, linspace, magic

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how peaks works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.