Matlab

How to Solve a System of Equations in MATLAB Using solve() Function

The system of equations seems like a set of multiple equations that includes unknown variables. The process of solving the system of equations in mathematics is to find the values of those unknown variables. In general, there are two types of a system of equations; linear and nonlinear equations. Solving such a system of equations is a common problem faced by engineers and scientists. MATLAB provides us with various ways to solve a system of equations, one among them is using a built-in solve() function.

This guide will teach you how to solve the linear and nonlinear equations system in MATLAB using the solve() function.

How to Get a Solution of the System of Equations Using MATLAB solve() Function?

The solve() is a built-in function in MATLAB used for solving a system of equations with multiple variables. If the count of equations is equal to the count of unknowns, the solution of the system of equations will be numerical; otherwise, the solution will be symbolic in terms of the desired variable. Each variable in the system of equations can have one or more solutions depending on its order.

Syntax

The solve() function follows a simple syntax to solve a system of equations in MATLAB.

Y = solve(eqns,vars)
[y1,...,yN] = solve(eqns,vars)

 

Here:

The function Y = solve(eqns, vars) solves a system of equations (eqns) with respect to variables (vars) by returning a structure Y that contains the solutions of each variable. We can access the solution or solutions of a variable from the structure by using the dot notation.

The function [y1,…,yN] = solve(eqns,vars) solves a system of equations (eqns) with respect to variables (vars) by assigning the solutions to the respective variables y1, y2,…,yn.

Examples

For more understanding, follow the given examples.

Example 1: Solving System of Equations Having Two Unknowns and Two Equations

This is a basic example of having a system of linear equations having two unknowns and two unknowns. For solving this linear system of equations we utilize the solve() function by storing the solutions of the variables x and y in the structure Y.

syms x y
eqns = [(-6*x)+(y)==10, (x)+(5*y)==0];
Y =solve(eqns, x, y)

 

Example 2: Solving System of Equations Having Three Unknowns and Two Equations

In this example, we are going to solve a system of linear equations having two equations and three unknowns using the solve() function. We obtain the solutions xz and yz in terms of the z. The values of xz and yz could be changed according to the values of z. So, we can say this system obtains infinite solutions.

syms x y z
eqns = [(-6*x)+(y)-(1.6*z)==10, (x)+(5*y)==10*z];
[xz yz]=solve(eqns)

 

Example 3: Solving System of Equations Having Two Equations and Two Unknowns

In the given MATLAB code, we consider a system of nonlinear equations and use the solve() function to solve this system.

syms x y
eqns = [x^2 + y^2 == 0, x^2 - y^2 == 1];
[y1, y2] = solve(eqns, x, y)

 

Conclusion

The solve() method is a useful function in MATLAB that is used for solving the system of equations. You can use multiple equations with multiple variables to quickly find the unknown variables in the equations. This guide has provided a detailed guideline with simple examples to quickly solve different types of equations using the solve() function in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.