Want to plot a sine wave online without installing a numerical computing environment? You can run a short script in our MATLAB-compatible online compiler, view the resulting graph, and download it as a PNG.

The compiler runs GNU Octave, an open-source environment that supports many MATLAB-style functions. It does not run MathWorks MATLAB, and compatibility varies. The first example in this guide was run in the online compiler before this article was prepared.

Plot a sine wave online: the quick example

Open the online compiler, paste the code into the source editor, and select Run code. Alternatively, choose Examples → Basic plotting to load this example.

t = linspace(0, 2 * pi, 300);
y = sin(t);
plot(t, y, "LineWidth", 2);
grid on;
xlabel("Time");
ylabel("sin(t)");
title("A sine wave");
disp("Sine wave plotted.");

When the run finishes, the Plots view displays the generated figure. The console shows Sine wave plotted. The curve starts at zero, rises to approximately 1, crosses zero near π, falls to approximately −1, and returns to zero near 2π: one complete cycle.

Note: The example labels the horizontal axis “Time,” but t is directly used as an angle in radians. For a time-based signal with frequency in hertz, use the example below.

How does the code work?

linspace(0, 2 * pi, 300) creates 300 evenly spaced values from 0 to 2π. Octave's sin function takes angles in radians, so sin(t) calculates one full cycle. plot(t, y, "LineWidth", 2) plots those values. The other commands add a grid, axis labels, title, and a message in the console.

To show two cycles over the same horizontal range, change the second line to y = sin(2 * t); and run the script again.

Plot a sine wave with a frequency in hertz

When the horizontal axis represents time in seconds, use sin(2 * pi * f * t), where f is the frequency in hertz. A 2 Hz wave completes two cycles per second:

f = 2;                         % Frequency in hertz
t = linspace(0, 1, 500);       % Time in seconds
y = sin(2 * pi * f * t);
plot(t, y, "LineWidth", 2);
grid on;
xlabel("Time (s)");
ylabel("Amplitude");
title("2 Hz sine wave");

You should see two complete cycles between 0 and 1 second. Change f to experiment with other frequencies.

Download your plot or save the script

After the run finishes, use Download figure in the plot viewer to save the plot as a PNG. To keep the source code, select Save .m file above the editor. You can also use Open .m file to open one existing source file.

The file is read in your browser; the editor contents are submitted when you run the code. The interface works with one source file at a time rather than importing an entire project.

Why isn't my plot appearing?

  1. Check the Console for an error. A typo or undefined variable can prevent the script from reaching plot.

  2. Make sure the script calls plot and wait for the run to finish. The compiler captures open figures after execution.

  3. Open the Plots tab. The compiler can capture up to four figures per run; figures closed by your script cannot be captured.

  4. Try Examples → Basic plotting to load the built-in example. Save any edits you want to keep before replacing your code.

The plot viewer displays captured images; interactive desktop graphics windows are not included. Scripts relying on unsupported graphics or proprietary MATLAB toolboxes may need changes.

Frequently asked questions

Can I plot MATLAB-style code online for free?

You can try supported MATLAB-style plotting code in the online compiler. It runs GNU Octave, and compatibility varies; it does not include MathWorks MATLAB or proprietary MATLAB toolboxes.

Do I need to install GNU Octave?

No. Write or paste code in the browser, run it in the online environment, and inspect the returned plot. Execution limits apply.

How do I plot more than one sine-wave cycle?

Increase the range—for example, use linspace(0, 4 * pi, 300) for two cycles of sin(t)—or change the expression to sin(2 * t) while keeping the original range.

Can I save the graph?

Yes. Use the download control in the plot viewer to save a PNG. Use Save .m file if you also want to keep the code.

Does this use the MATLAB Compiler product?

No. This is a browser-based GNU Octave code runner; it is not MathWorks MATLAB Compiler and does not package your script into a standalone application.

Try it yourself

Start with the basic example, then change the range or frequency and compare the result. Open the MATLAB-compatible online compiler to run your own plot.

MATLAB is a registered trademark of The MathWorks, Inc. TheOnlineToolset is not affiliated with or endorsed by MathWorks.