Verify a MATLAB Algorithm
This tutorial shows how to use MATLAB® Test™ to verify a MATLAB algorithm. This tutorial uses a project that contains a MATLAB algorithm, MATLAB tests, and requirements. In this tutorial, you follow these steps to run the tests, collect coverage, verify requirements, and deploy the algorithm as C code:
Verify Code by Running Tests: Verify the algorithm by using the MATLAB Test Manager to run tests and view results.
Collect Coverage for Tests and Address Missing Coverage: Collect coverage and add coverage for lines of source code that are uncovered by writing new tests.
Verify Requirements and Address Missing Traceability: Verify requirements that are linked to tests and add traceability for unlinked requirements.
Generate C Code and Test for Equivalence: Deploy the algorithm as C code and test the generated code for functional equivalence.
This tutorial uses the
The project contains:
Requirement sets, located in the
A MATLAB algorithm, located in the
MATLAB tests, located in the
A script to demonstrate running the MATLAB algorithm, located in the
Links from MATLAB code lines to requirements, stored in SLMX files in the
Use the Shortest Path Function
shortest_path function tests the validity of the inputs to the function and then uses the Djikstra algorithm to calculate the number of edges in the shortest path between two nodes on a graph. The inputs to the function are an adjacency matrix that represents a graph, the starting node, and the ending node. For example, consider this adjacency matrix that represents a graph with six nodes.
A = [0 1 0 0 1 0; 1 0 1 0 0 0; 0 1 0 1 0 0; 0 0 1 0 1 1; 1 0 0 1 0 0; 0 0 0 1 0 0];
Create a graph from the matrix and plot it.
G = graph(A); plot(G,EdgeLabel=G.Edges.Weight)
Calculate the number of edges in the shortest path between nodes 1 and 6.
pathLength = shortest_path(A,1,6)
pathLength = 3