# factorGraph

## Description

A `factorGraph`

object stores a bipartite graph consisting of
factors connected to variable nodes. The nodes represent the unknown random variables in an
estimation problem, such as robot poses, and the factors represent probabilistic constraints
on those nodes, derived from measurements or prior knowledge. During optimization, the factor
graph uses all the factors and current node states to update the node states.

To use the factor graph:

Create an empty

`factorGraph`

object.For each desired factor type:

Generate node IDs using the

`generateNodeID`

object function.Define factors with the desired node IDs, using any of the supported factor objects:

Add factors to the factor graph using the

`addFactor`

object function. If the factor graph does not contain a node with the specified ID, the function automatically creates a node with that ID and adds it to the factor graph when adding the factor to the factor graph. If the factor graph contains a node with the specified ID, ensure that adding the new factor does not cause a node type mismatch. For more information, see Tips. For a list of expected node types for each factor, see Expected Node Types of Factor Objects.

Check if all the nodes in the factor graph are connected to at least one other node using the

`isConnected`

object function.Create a

`factorGraphSolverOptions`

object to specify factor graph solver options.Optimize the factor graph using the

`optimize`

object function with the desired factor graph solver options.Extract factor graph node data, such as node IDs and node states, using the

`nodeIDs`

and`nodeState`

object functions.

## Creation

### Syntax

### Description

creates an empty
`fg`

= factorGraph`factorGraph`

object.

## Properties

## Object Functions

`addFactor` | Add factor to factor graph |

`fixNode` | Fix or free nodes in factor graph |

`generateNodeID` | Generate new node IDs |

`hasNode` | Check if node ID exists in factor graph |

`isConnected` | Check if factor graph is connected |

`isNodeFixed` | Check if node is fixed |

`nodeCovariance` | Get state covariance of nodes in factor graph |

`nodeIDs` | Get node IDs in factor graph |

`nodeState` | Get or set node state in factor graph |

`nodeType` | Get node type of node in factor graph |

`optimize` | Optimize factor graph |

`removeFactor` | Remove factor from factor graph |

`removeNode` | Remove node from factor graph |

`show` | Plot pose nodes, pose node edges, and landmark nodes of factor graph |

## Examples

## More About

## Tips

To specify multiple factors and nodes at once for a specific factor type, use the

`generateNodeID`

function and specify the number of factors and the factor type. See the`generateNodeID`

function for more details.`poseIDPairs = generateNodeID(fg,3,"factorTwoPoseSE2"); ftpse2 = factorTwoPoseSE2(poseIDPairs);`

You can get the states of all the pose nodes by first using the

`nodeIDs`

function and specifying the node type as`"POSE_SE2"`

for SE(2) robot poses and`"POSE_SE3"`

for SE(3) robot poses. Then, use the`nodeState`

function with those node IDs to get the node states of the robot pose nodes.`poseIDs = nodeIDs(fg,NodeType="POSE_SE2"); poseStates = nodeState(fg,poseIDs);`

Check the types of nodes that each factor creates or connects to before adding factors to the factor graph to avoid node type mismatch errors. For a list of expected node types for each factor, see Expected Node Types of Factor Objects.

## References

[1] Dellaert, Frank. *Factor
graphs and GTSAM: A Hands-On Introduction*. Georgia: Georgia Tech, September,
2012.

## Extended Capabilities

## Version History

**Introduced in R2022a**