Trial and Error method - Matlab Program code

Trial and error method is very useful concept in science when your are solving difficult equations. It is not a direct method to find a unknown value and consumes lot of time to solve the equation. Also, it will not give the exact answer. But, it will give a reasonable answer to your equation. In this tutorial, we will learn, what is trial and error method and how to apply it to solve difficult equations using manual mdthods and using Matlab.




When we have an equation with higher order such as x7, x6, .. we use trial and error method to solve the equation.



How to solve an equation using trial and error method?

Think we have an equation of x and equation like following one.

x3 + 3x + 1 = 0


Steps of solving

There are several steps to follow to solve an equation using trial and error method. Later, we discuss these steps in detail.

  • In this method we assume a value for x and substitute it in the equation and check the final value.
  • If it is not equal or close to zero (like 0.001,0.01, 0.04) get another value for x and substitute and check whether it is close to the zero. Continue this process until you get a value close to the zero.
trial and error method

Assume a value for x and substitute it in the equation

As the first assumption, we can assume x as 1. Then substitute it in left side of our equation.

  • Left side = 13 + 3*1 + 1
  • Left side = 5
  • Left side is not equal to the zero. Therefore, x = 1 is not an answer to our equation.

Because, x = 1 was not an answer, lets try another value, x = -1

  • Left side = (-1)3 + 3*(-1) + 1
  • Left side = -3
  • Left side is not again equal to the zero. Therefore, x = -1 is not an answer to our equation.

Because, x = -1 was not an answer too, lets try another value, x = -0.5

  • Left side = (-0.5)3 + 3*(-0.5) + 1
  • Left side = -0.75
  • Left side is not again equal to the zero. Therefore, x = -1 is not an answer to our equation.

However, you can see, our value is being reached towards zero. Let's try one more assumption (x = -0.2) to make sure whether we are going to the right direction.

  • Left side = (-0.2)3 + 3*(-0.2) + 1
  • Left side = 0.392
  • Left side is not again equal to the zero. Therefore, x = -1 is not an answer to our equation.


Now, we can make a table of x values and left side values.

root of an equation lies between sign changes

Let's draw a graph using x values and given values for x.

x values and trial and error method

You can see a sign change, when x goes from -0.5 to -0.2. In that range left side value should be zero at some value of x (between -0.5 to -0.2). See the graph to get clear. There may be more sign changes in different ranges like this range.


After, you found a sign change, you can assume numbers between that range to get the optimum answer for x. In this example, now you should assume values for x between -0.5 to -0.2.

This is the procedure of solving equations using trial and error method.




Download Trial and error method - Matlab Code