How To Solve Non-Linear Equations Using Matlab?
How to Solve Non-Linear Equations Using Matlab

A step-by-step guide on how to find roots of non-linear equations using the bisection method on Matlab.

We all know that calculators have pre-installed general equations which give output basis on the inputs of the equation. The results are quicker and have fewer errors as compared to human solving the equation.

Similarly, solving equations on computers is faster and even have fewer errors as compared to calculators. Computers can perform multiple functions, whereas the latest calculators are limited to a few.

Photo by Antoine Dautry on Unsplash

What is linear and non-linear equation?

Sometimes remembering the definitions can be a really hard task, at such times remembering small indicators is beneficial. How I remember the linear and non-linear equation is through their graphs.

Linear equations look like a straight line when sketched, whereas non-linear equations look like a curve. Linear equations have a constant slope, whereas non-linear equations have a variable slope. Furthermore, the output is directly proportional to its input for linear equations and vice versa for non-linear equations.

For better understanding, let me give an example of a simple linear and non-linear equation.

ax+by+cz=0 — Linear Equation x²-4 — Non Linear Equation

How To Solve Non-Linear Equations Using Matlab?

Algorithm & Source Code:

Algorithm And Source Code of Matlab Program (Bisection Method)

For better understanding, I shall explain how the program works.

As we all know, in order to solve any non-linear equation we need a code which asks the user for the equation. Using the input function, the display asks the user to enter the function and stores it as string because we used a syntax of the function in which we can tell the data type to store in as well. Here it was string s . The function inline converts the string into function.

After the user has entered the non-linear equation, he/she needs to input the limits of x; upper and lower, and tolerance error value.

The program checks if the user enters the correct limits, when the condition is true then the program continues, if not then the program asks user to input the limits again.

The formula for finding the root value of x is “xr=(xu+xl)/2” where xu is the upper limit and xl is the lower limit. After calculating xr, the program will increment tolerance error with 1 and store it in “aerror” variable.

The function fplot will plot the graph of non-linear equation with range of minimum limit (xl) and maximum limit (xu).

There is a pre-condition loop which runs until it fulfils the set condition. The condition is when approximation error is greater than tolerance. After entering the loop, there is an increment for the counter. In each loop, the program checks if the multiple of f(xl) and f(xr) is greater than 0. If the multiple is greater than 0, then the upper limit is stored in the variable of lower limit, whereas if multiple is less than 0, then the lower limit is stored in the variable of upper limit.

After which it recalculates the root value with a new set of limits. The value of approximation error is also recalculated. Then whatever programmer asked for is displayed on screen.

Output:

The graph above is of the non-linear equation “x²-4”, the limits are; 5 as the upper limit and 0 as the lower limit. The root is at x=2, it is circled because of the function plot .

The program will also output a set of values for;
['Syed Zaidi']
['Software Development', 'Coding', 'Programming', 'Matlab', 'Technology']