MATLAB Scientific Academy
BEGINNER · 15 min

Your first MATLAB program

Learn how MATLAB stores values, works with vectors and turns a few lines of code into a useful scientific plot.

What you will learn

  • Assign values to variables and understand the workspace.
  • Create a vector with linspace.
  • Use element-wise mathematical functions and plot results.

The concept

MATLAB is designed around numerical arrays. A scalar is a single value; a vector is a row or column of values; a matrix contains rows and columns. Many scientific tasks become simpler when an operation can be applied to an entire array at once.

A script is a sequence of commands saved in an .m file. Run it from the MATLAB editor or command window. MATLAB is case-sensitive, so x and X are different variables.

Scientific habit: Before running a program, predict what it will do. Then compare your prediction with the output and explain any difference.

Worked example

Create 200 evenly spaced points between zero and 2π, calculate their sine, and plot the result.

example.mMATLAB CODE
% first_plot.m
x = linspace(0, 2*pi, 200);
y = sin(x);
plot(x, y, 'LineWidth', 2);
xlabel('x (radians)');
ylabel('sin(x)');
title('A simple sine wave');
grid on;

linspace creates the x coordinates, sin calculates a value at each coordinate, and plot draws the relationship. The semicolon suppresses command-window output for that line.

Try it yourself

Think about how the number of sample points changes the smoothness of the plotted curve.

Quick check

What does linspace(0, 10, 6) return?

Reveal hint and answer
Hint: It creates six equally spaced values including both endpoints.
Answer: [0 2 4 6 8 10].

Where this is used

Vectorized code is useful in signal processing, spectroscopy, optical sensor calibration, parameter sweeps and numerical models.

Academy homeNext: Scripts & algorithms