0. Introduction

Support Vector Machines, usually called SVM, are among the most useful supervised machine learning methods for classification and regression. In MATLAB, SVM is mainly available through the Statistics and Machine Learning Toolbox, with core functions such as fitcsvm for binary classification, fitcecoc for multiclass classification, and fitrsvm for regression. MATLAB also provides app-based workflows through Classification Learner and Regression Learner.

This tutorial explains what SVM is, when to use it, how it works conceptually, and how to implement it step by step in MATLAB with practical examples.

1. What is SVM?

SVM is a supervised learning algorithm that tries to find a decision boundary that separates classes as well as possible. For classification, the idea is to build a hyperplane that maximizes the margin between classes. For nonlinear problems, SVM uses a kernel function to transform the data into a space where separation becomes easier. MATLAB supports linear and kernel-based SVM workflows in its SVM training functions.

In simple terms:

2. Why use SVM?

SVM is often a strong choice because it:

However, SVM is not always the best option. It can become slow on very large datasets, can require parameter tuning, and is often less interpretable than a simple linear model or decision tree.

3. Main SVM concepts

3.1 Hyperplane

A hyperplane is the boundary used to separate classes. In 2D it is a line, in 3D it is a plane, and in higher dimensions it is called a hyperplane.

3.2 Margin

The margin is the distance between the separating boundary and the nearest training samples from each class. SVM tries to maximize this margin because a larger margin usually improves generalization.

3.3 Support vectors

The most important data points in SVM are the ones closest to the boundary. These are called support vectors. They define the position of the separating hyperplane.

3.4 Kernel trick

If the data is not linearly separable, SVM can use a kernel such as:

In MATLAB, kernel-based classification is handled through fitcsvm, and multiclass SVM commonly uses fitcecoc with an SVM template.

3.5 Box constraint

This controls the tradeoff between having a large margin and penalizing classification errors.

3.6 Standardization

SVM is sensitive to feature scale. In MATLAB, you will often use 'Standardize', true during training. MathWorks examples and options for SVM training commonly show standardization as part of the workflow.

4. MATLAB functions you need to know

For SVM in MATLAB, the most important functions are:

Part I — Binary Classification with SVM in MATLAB

5. First simple example: linear SVM

Let us start with a very small binary classification example.

clc;
clear;
close all;

% Example data
X = [1 2;
     2 3;
     2 1;
     3 2;
     6 7;
     7 8;
     8 7;
     7 6];

Y = [1; 1; 1; 1; 2; 2; 2; 2];

% Train linear SVM
Mdl = fitcsvm(X, Y, ...
    'KernelFunction', 'linear', ...
    'Standardize', true);

% Predict labels on training data
[label, score] = predict(Mdl, X);

disp('Predicted labels:');
disp(label);

disp('Scores:');
disp(score);

Explanation

Here:

predict returns class labels for classification SVM models in MATLAB.

6. Visualizing the data

gscatter(X(:,1), X(:,2), Y, 'rb', 'ox');
xlabel('Feature 1');
ylabel('Feature 2');
title('Training Data');
grid on;

This allows you to see whether the two classes look linearly separable.

7. Example with train/test split

In practice, we do not evaluate the model only on training data.

clc;
clear;
close all;

% Data
X = [1 2;
     2 3;
     2 1;
     3 2;
     6 7;
     7 8;
     8 7;
     7 6;
     1.5 2.5;
     6.5 7.5];

Y = [1;1;1;1;2;2;2;2;1;2];

% Random split
rng(1);
cv = cvpartition(Y, 'HoldOut', 0.3);

XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);

XTest = X(test(cv), :);
YTest = Y(test(cv), :);

% Train model
Mdl = fitcsvm(XTrain, YTrain, ...
    'KernelFunction', 'linear', ...
    'Standardize', true);

% Test prediction
YPred = predict(Mdl, XTest);

% Accuracy
accuracy = mean(YPred == YTest) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);

This introduces a basic validation strategy. In real projects, this is much better than checking only the training set.

8. Confusion matrix

A confusion matrix shows how many examples were correctly or incorrectly classified.

cm = confusionmat(YTest, YPred);
disp('Confusion Matrix:');
disp(cm);

confusionchart(YTest, YPred);
title('Confusion Matrix');

9. Nonlinear SVM with RBF kernel

Some datasets are not linearly separable. In such cases, an RBF kernel is often a good starting point.

clc;
clear;
close all;

% XOR-like dataset
X = [0 0;
     0 1;
     1 0;
     1 1;
     0.1 0.2;
     0.2 0.9;
     0.9 0.1;
     0.8 0.8];

Y = [1; 2; 2; 1; 1; 2; 2; 1];

% Train nonlinear SVM
Mdl = fitcsvm(X, Y, ...
    'KernelFunction', 'rbf', ...
    'KernelScale', 'auto', ...
    'Standardize', true);

YPred = predict(Mdl, X);

accuracy = mean(YPred == Y) * 100;
fprintf('Accuracy = %.2f%%\n', accuracy);

Why RBF?

The RBF kernel is useful when the boundary between classes is curved or complex. MATLAB supports kernel settings such as kernel function and kernel scale in SVM training. Hyperparameter optimization can also tune choices such as KernelFunction, KernelScale, BoxConstraint, and related options.

10. Cross-validation in MATLAB

Cross-validation gives a more reliable estimate of model performance than a single train/test split.

clc;
clear;
close all;

load ionosphere

% Train binary SVM
SVMModel = fitcsvm(X, Y, ...
    'Standardize', true, ...
    'KernelFunction', 'rbf', ...
    'KernelScale', 'auto');

% 5-fold cross-validation
CVSVMModel = crossval(SVMModel, 'KFold', 5);

% Classification loss
classLoss = kfoldLoss(CVSVMModel);

fprintf('5-Fold Cross-Validation Loss = %.4f\n', classLoss);
fprintf('5-Fold Cross-Validation Accuracy = %.2f%%\n', (1-classLoss)*100);

MathWorks documents the use of crossval with SVM models, including examples with the ionosphere dataset and standardized predictors.

11. Hyperparameter tuning

SVM performance depends strongly on its parameters. Important ones include:

MATLAB supports hyperparameter optimization directly in fitcsvm.

clc;
clear;
close all;

load ionosphere

Mdl = fitcsvm(X, Y, ...
    'Standardize', true, ...
    'OptimizeHyperparameters', {'BoxConstraint','KernelScale','KernelFunction'}, ...
    'HyperparameterOptimizationOptions', struct( ...
        'AcquisitionFunctionName', 'expected-improvement-plus', ...
        'ShowPlots', false));

YPred = predict(Mdl, X);
acc = mean(YPred == Y) * 100;
fprintf('Training Accuracy = %.2f%%\n', acc);

MathWorks explicitly states that fitcsvm can optimize hyperparameters such as BoxConstraint, KernelFunction, KernelScale, PolynomialOrder, and Standardize using optimization workflows.

12. Binary classification example with a real MATLAB dataset

The ionosphere dataset is a classic binary classification dataset often used in MATLAB examples.

clc;
clear;
close all;

load ionosphere

% Split data
rng(10);
cv = cvpartition(Y, 'HoldOut', 0.25);

XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);

XTest = X(test(cv), :);
YTest = Y(test(cv), :);

% Train SVM
Mdl = fitcsvm(XTrain, YTrain, ...
    'KernelFunction', 'rbf', ...
    'KernelScale', 'auto', ...
    'Standardize', true);

% Predict on test set
YPred = predict(Mdl, XTest);

% Accuracy
accuracy = mean(YPred == YTest) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);

% Confusion matrix
confusionchart(YTest, YPred);
title('Ionosphere SVM Classification');

Part II — Multiclass SVM in MATLAB

13. Why multiclass needs a special approach

A standard SVM is naturally designed for binary classification. For multiclass problems, MATLAB uses Error-Correcting Output Codes (ECOC) through fitcecoc. By default, for K classes, one-versus-one coding uses K(K−1)/2 binary learners.

14. Multiclass example with the iris dataset

clc;
clear;
close all;

load fisheriris

X = meas;
Y = species;

% Define SVM learner template
t = templateSVM('KernelFunction', 'rbf', ...
                'KernelScale', 'auto', ...
                'Standardize', true);

% Train multiclass ECOC model
Mdl = fitcecoc(X, Y, 'Learners', t);

% Predict
YPred = predict(Mdl, X);

% Accuracy
accuracy = mean(strcmp(YPred, Y)) * 100;
fprintf('Training Accuracy = %.2f%%\n', accuracy);

% Confusion chart
confusionchart(Y, YPred);
title('Iris Multiclass SVM');

Explanation

Here:

MathWorks documents templateSVM for creating reusable SVM learner templates and fitcecoc for multiclass classification using SVM learners.

15. Multiclass train/test example

clc;
clear;
close all;

load fisheriris

X = meas;
Y = species;

rng(2);
cv = cvpartition(Y, 'HoldOut', 0.3);

XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);

XTest = X(test(cv), :);
YTest = Y(test(cv), :);

t = templateSVM('KernelFunction', 'rbf', ...
                'KernelScale', 'auto', ...
                'Standardize', true);

Mdl = fitcecoc(XTrain, YTrain, 'Learners', t);

YPred = predict(Mdl, XTest);

accuracy = mean(strcmp(YPred, YTest)) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);

confusionchart(YTest, YPred);
title('Multiclass SVM Test Results');

Part III — SVM Regression in MATLAB

16. What is SVM regression?

SVM is not only for classification. It can also be used for regression, where the goal is to predict a numeric value. MATLAB uses fitrsvm for this. For low- to moderate-dimensional data, fitrsvm is the standard SVM regression function; for high-dimensional linear regression cases, MathWorks points users toward fitrlinear.

17. Simple regression example

clc;
clear;
close all;

% Create sample data
X = (1:10)';
Y = [1.2; 1.9; 3.1; 4.2; 5.0; 5.8; 7.1; 8.2; 8.9; 10.1];

% Train SVM regression model
Mdl = fitrsvm(X, Y, ...
    'KernelFunction', 'gaussian', ...
    'Standardize', true);

% Prediction
XTest = (1:0.1:10)';
YPred = predict(Mdl, XTest);

% Plot
plot(X, Y, 'o');
hold on;
plot(XTest, YPred, 'LineWidth', 2);
xlabel('X');
ylabel('Y');
title('SVM Regression');
legend('Training Data', 'SVR Prediction');
grid on;

18. Regression evaluation metrics

For regression, accuracy is not used. Instead, we often compute:

Example:

clc;
clear;
close all;

X = (1:10)';
Y = [1.2; 1.9; 3.1; 4.2; 5.0; 5.8; 7.1; 8.2; 8.9; 10.1];

Mdl = fitrsvm(X, Y, ...
    'KernelFunction', 'gaussian', ...
    'Standardize', true);

YPred = predict(Mdl, X);

MAE = mean(abs(Y - YPred));
MSE = mean((Y - YPred).^2);
RMSE = sqrt(MSE);
R2 = 1 - sum((Y - YPred).^2) / sum((Y - mean(Y)).^2);

fprintf('MAE  = %.4f\n', MAE);
fprintf('MSE  = %.4f\n', MSE);
fprintf('RMSE = %.4f\n', RMSE);
fprintf('R^2  = %.4f\n', R2);

19. Larger regression example

clc;
clear;
close all;

% Synthetic nonlinear regression data
x = linspace(-3, 3, 100)';
y = sin(x) + 0.2*randn(size(x));

% Train/test split
rng(3);
cv = cvpartition(length(x), 'HoldOut', 0.2);

XTrain = x(training(cv), :);
YTrain = y(training(cv), :);

XTest = x(test(cv), :);
YTest = y(test(cv), :);

% Train SVR
Mdl = fitrsvm(XTrain, YTrain, ...
    'KernelFunction', 'gaussian', ...
    'KernelScale', 'auto', ...
    'Standardize', true);

% Predict
YPred = predict(Mdl, XTest);

% Error metrics
MSE = mean((YTest - YPred).^2);
RMSE = sqrt(MSE);

fprintf('Test RMSE = %.4f\n', RMSE);

% Plot
plot(XTrain, YTrain, 'o');
hold on;
plot(XTest, YPred, '.', 'MarkerSize', 15);
xlabel('x');
ylabel('y');
title('Nonlinear SVM Regression');
legend('Training Data', 'Predictions');
grid on;

Part IV — Using SVM from MATLAB Apps

20. Classification Learner app

If you prefer a graphical interface, MATLAB provides Classification Learner. It lets you import data, choose classifiers, validate models, optimize hyperparameters, and inspect results. For two-class SVM it uses fitcsvm; for multiclass classification it uses fitcecoc. It can also generate MATLAB code from the trained workflow.

Typical steps:

  1. Open MATLAB.
  2. Type:

    classificationLearner
  3. Import your dataset.
  4. Choose the response variable.
  5. Train one or more SVM models.
  6. Compare validation results.
  7. Export the best trained model or generated code.

This is very useful for beginners because it reduces coding effort and helps compare models quickly.

21. Regression Learner app

For regression problems, MATLAB provides Regression Learner. It uses fitrsvm for SVM regression models and lets you configure kernel and box-constraint-related options from the app interface.

Typical use:

regressionLearner

Then:

  1. import the dataset,
  2. choose the numeric target,
  3. train SVM regression,
  4. compare results,
  5. export model or code.

Part V — Important Practical Advice

22. Always scale your features

SVM usually performs much better when predictors are on similar scales. That is why using:

'Standardize', true

is strongly recommended in many SVM workflows. MathWorks examples and templates repeatedly include standardized predictors in SVM training.

23. Choosing the kernel

A simple rule:

24. Do not evaluate only on training data

A model that performs perfectly on training data may still fail on new unseen data. Use:

MATLAB provides built-in cross-validation support for SVM models.

25. Tune the parameters

For good SVM performance, tune:

MathWorks documents these as eligible hyperparameters for optimization in fitcsvm, and SVM-related options are also exposed through the learner apps.

26. Binary vs multiclass

Remember:

This is one of the most common beginner mistakes in MATLAB SVM work.

Part VI — End-to-End Example Project

27. Full multiclass project using iris

Here is a more complete project-style workflow.

clc;
clear;
close all;

%% Load data
load fisheriris
X = meas;
Y = species;

%% Explore data
disp('First 5 observations:');
disp(X(1:5,:));
disp('First 5 labels:');
disp(Y(1:5));

%% Train/test split
rng(42);
cv = cvpartition(Y, 'HoldOut', 0.2);

XTrain = X(training(cv), :);
YTrain = Y(training(cv), :);

XTest = X(test(cv), :);
YTest = Y(test(cv), :);

%% Define learner
t = templateSVM( ...
    'KernelFunction', 'rbf', ...
    'KernelScale', 'auto', ...
    'Standardize', true);

%% Train model
Mdl = fitcecoc(XTrain, YTrain, 'Learners', t);

%% Predict
YPred = predict(Mdl, XTest);

%% Evaluate
accuracy = mean(strcmp(YPred, YTest)) * 100;
fprintf('Test Accuracy = %.2f%%\n', accuracy);

%% Confusion chart
figure;
confusionchart(YTest, YPred);
title('Iris Multiclass SVM');

%% Predict a new flower
newSample = [5.1 3.5 1.4 0.2];
predictedClass = predict(Mdl, newSample);

disp('Predicted class for new sample:');
disp(predictedClass);

What this project teaches

This project includes:

That is very close to what you would do in a real beginner project.

Part VII — Common Errors Beginners Make

28. Using fitcsvm for three or more classes

For multiclass classification, use fitcecoc with templateSVM, not only fitcsvm.

29. Forgetting standardization

If one feature ranges from 0 to 1 and another from 0 to 10000, SVM can behave poorly.

30. Using training accuracy as the final score

Always validate on unseen data.

31. Choosing a complex kernel without testing

Start simple, then move to nonlinear kernels if needed.

32. Ignoring hyperparameter tuning

Default settings are not always optimal.

Part VIII — When should you use SVM?

SVM is a good choice when:

You might avoid SVM when:

Part IX — Summary

SVM is one of the most important supervised learning methods and MATLAB makes it relatively easy to use. For binary classification, fitcsvm is the main function. For multiclass classification, MATLAB uses fitcecoc with SVM learners. For regression, fitrsvm is the standard approach. MATLAB also provides learner apps that help train, compare, validate, and export SVM models visually.

A strong practical workflow is:

  1. prepare and inspect the data,
  2. standardize features,
  3. choose binary, multiclass, or regression SVM correctly,
  4. split into training and testing sets,
  5. train the model,
  6. validate with cross-validation,
  7. tune hyperparameters,
  8. evaluate with proper metrics,
  9. deploy or save the trained model.

Part X — Short MATLAB cheat sheet

Binary classification

Mdl = fitcsvm(X, Y, 'KernelFunction', 'rbf', 'Standardize', true);
YPred = predict(Mdl, XTest);

Multiclass classification

t = templateSVM('KernelFunction', 'rbf', 'Standardize', true);
Mdl = fitcecoc(X, Y, 'Learners', t);
YPred = predict(Mdl, XTest);

Regression

Mdl = fitrsvm(X, Y, 'KernelFunction', 'gaussian', 'Standardize', true);
YPred = predict(Mdl, XTest);

Cross-validation

CVSVMModel = crossval(Mdl, 'KFold', 5);
loss = kfoldLoss(CVSVMModel);

Practice exercises

Exercise 1

Train a linear SVM on a small binary dataset and compute test accuracy

Exercise 2

Replace the linear kernel with the RBF kernel and compare results

Exercise 3

Use the fisheriris dataset and build a multiclass SVM model

Exercise 4

Use crossval to estimate model performance

Exercise 5

Create a simple regression dataset and train an SVM regression model with fitrsvm

Short recap

Exercise 1

Linear SVM for binary classification.

Exercise 2

Comparison between linear SVM and RBF SVM.

Exercise 3

Multiclass SVM with fitcecoc on iris data.

Exercise 4

Performance estimation with cross-validation.

Exercise 5

SVM regression with fitrsvm.