Solving Systems of Non-Linear Equations

In today’s lesson we will be discussing how to solve a system of non-linear equations. We will be discussing the following commands:

1. Fsolve

2. Solve and Vpasolve

Recall that we discussed solving a system of linear equations in a previous lesson – click here if you missed it. Linear equations are those that graph a straight line, in the form y = m*x+b.

Now we will look at solving a system of non-linear equations. This means finding the roots/zeroes, which are the values of unknown variables that will make both sides of all equations equal one another. Non-linear equations are those that do not have a straight line when graphed, such as parabolas, exponential, logarithmic, square root, or other functions. Now there are two ways to solve a system of non-linear equations (or just a single equation too!) – either use the built-in function fsolve from the Optimization Toolbox, or use solve from the Symbolic Toolbox.

You can go to your Command Window and type ver to see which toolboxes you have installed. This will come up:

Image 1

A quick note before we get started – you can use these commands to solve a system of linear equations as well, but they are not optimized to do so.

Solving a System of Non-Linear Equations Using Fsolve

We can use the built-in function fsolve to solve this type of system in MATLAB. Say we have these three non-linear equations (this becomes a non-linear system of equations) and we set each side of the equation = 0.

Image 2

Now write a function file that will take one variable, let’s call it x, as an input. It must be a regular M-File function. Click here if you missed the lesson on functions. This is what it will look like when you create a new function and add x as an input. My function is called equationsolver and the output is the variable output.

Image 3

Now add in one line that is equal to the output of your function – mine is called the variable output so I set that equal to the equations I would like to solve from Image 2. I have removed the equals sign and the zero on the right side of each equals sign, and just kept the equations. I use square brackets to define them, and semicolons between them.

Now replace each of the variables – x, y, and z – with x(1), x(2), and x(3), in that order. So x = x(1), y = x(2), z = x(3). We will need to replace all the variables in terms of one variable, so I have chosen x. This will be used as x(1) x(2) x(3) and so on as I just mentioned above.

Image 4

To use fsolve, the syntax is fsolve(@function,variable). Replace the word function with the name of the function you created above (mine is called equationsolver) – keep the @ symbol. Replace the world variable with a vector of initial guesses for the variables you are trying to solve for (let’s guess that the answers are x = 0, y = 0 and z = 0. Then the vector would be [0; 0; 0;], and I will call this x0). fsolve works by using this initial vector as the starting guess to solve the equations in your function.

So first we will define this vector of guesses for each variable in the Command Window – this will become a column vector, so we will use semicolons ([0; 0; 0;]) between each number in the vector (remember the semicolon with vectors make each number go onto a new line, so it will output as a column of numbers and not a row).

Image 5

Then we will use fsolve(@equationsolver, x0). This needs to be set equal to an output, let’s say [x, y, z]. This will match with the variables in the vector of guesses – if you had guessed x0 = [0; 0; 0;] like I had, that would have meant x = 0, y = 0, z = 0, and the answers to your variables will appear in the same order in the Command Window, so that’s the order in which they should be defined when you set them equal to fsolve – which is [x, y, z] as I mentioned above.

This is what you will type in the Command Window:

Image 6

Running the code gives this output. This means x = 1.5708, y = 1, and z = -2.

Image 7

Now MATLAB was able to find the correct answers, and there are multiple solutions.

The first solution is x = 1.3198, y = 10^-12 * 0.6008, z = 1

The second solution is x = 0, y = 10^-12 * 0.0866, z = 1

The third solution is x = -1.0313, y = 10^-12 * 0.0968 and z = 1

Solving a System of Non-Linear Equations Using Solve

Using the symbolic toolbox, we will first define the unknown variables with syms. These variables are a, x, y, and z.

Image 8

Then define the equations you want to solve. I set this equal to the variable equations and then defined all the equations (I had 3) inside square brackets. Use two equal signs (==) within each equation.

Image 9

Next define the variables you are solving for. I set this equal to the variable variables, then used square brackets to define the unknowns, separated by commas.

Image 10

Now use the command solve with the inputs of the equations and variables lines. I had just called these equations and variables. Set it equal to a vector which will hold the answers. I called this vector [solve_a,solve_x,solve_y,solve_z] because I needed 4 inputs for each unknown variable. It should be in order of the variables you assigned in the variables line above.

Image 11

I added an extra fprintf line to be able to format the answers with 2 decimal places after them, because otherwise they will be very large numbers.

Image 12

Running the code gives a vector with the answers, and they will be in the order defined in the last line. This vector of solutions is in the order of the variables I used in the variables line which was [a,x,y,z]. The output from this code indicates the following:

a = -226.21, x = 400.95, y = 0.03, z = 113.10

Image 13

Notice how I am getting a warning in orange. A way to avoid this is to use vpasolve (a numeric solver) rather than just solve (a symbolic solver) in the second last line of code from Image 9. The difference between a numeric and a symbolic solver is that a symbolic solver will output answers in symbols – for example, if the answer was pi, then it will show as pi and not the value which is 3.14… With a numeric solver, it will display the value of the symbol (eg. rather than pi it will show 3.1415…). Vpasolve is also faster than solve. Generally you can use solve, but if it gets stuck then use vpasolve.

Now this is what comes up in the output, which is the same answer as when using solve, but without the warning:

Image 14