peaks — Generate the standard peaks test surface in MATLAB and RunMat.
peaks(n) evaluates the standard peaks 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. Formula and default grid size follow MATLAB semantics.
Syntax
Z = peaks()
Z = peaks(n)
Z = peaks(X, Y)
[X,Y] = peaks(n)
[X,Y,Z] = peaks(n)
[X,Y] = peaks(X, Y)
[X,Y,Z] = peaks(X, Y)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n | IntegerScalar | Yes | 49 | Grid size n for n-by-n sampling. |
X | NumericArray | Yes | — | X coordinate matrix. |
Y | NumericArray | Yes | — | Y coordinate matrix (same size as X). |
Returns
| Name | Type | Description |
|---|---|---|
Z | NumericArray | Sampled peaks surface values. |
X | NumericArray | Grid coordinates along X-axis. |
Y | NumericArray | Grid coordinates along Y-axis. |
Returned values from peaks depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
| — | More than three outputs are requested. | peaks: too many output arguments; maximum is 3 |
| — | The builtin is called with more than two input arguments. | peaks: expected 0, 1, or 2 input arguments |
| — | The n argument is not a numeric scalar. | peaks: n must be a numeric scalar |
| — | The n argument is non-finite. | peaks: n must be finite |
| — | The n argument is not an integer. | peaks: n must be an integer |
| — | The n argument is negative. | peaks: n must be non-negative |
| — | The n argument exceeds platform limits. | peaks: n is too large for this platform |
| — | X/Y inputs are not numeric matrix values. | peaks: X and Y must be numeric matrices |
| — | X/Y inputs are not 2-D matrices. | peaks: X and Y must be 2-D matrices |
| — | X and Y inputs do not have the same shape. | peaks: X and Y must have the same size |
How peaks works
peakswith 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.
Does RunMat run peaks 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;Using peaks with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how peaks changes the result.
Run a small peaks example, explain the result, then change one input and compare the output.
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.
Related Array functions
Creation
colon · eye · false · fill · inf · linspace · logspace · magic · meshgrid · nan · ones · rand · randi · randn · randperm · range · true · zeros
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how peaks is executed, line by line, in Rust.
- View the source for peaks in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.