Sunday, March 19, 2023

Newton Rapson method for solving non linear equations: Matlab application

The Newton-Raphson method is a popular numerical method for finding approximate solutions to non-linear equations. It is an iterative method that involves making an initial guess and then repeatedly refining that guess until a sufficiently accurate solution is obtained.

The method involves the following steps:

  1. Choose an initial guess x0 for the solution of equation f(x) = 0.
  2. Compute the derivative f'(x0) at x0.
  3. Use the formula: x1 = x0 - f(x0)/f'(x0) to compute a new estimate x1 for the solution.
  4. Repeat steps 2 and 3, using the current estimate xn to compute the next estimate xn+1, until the desired level of accuracy is achieved.

The basic idea behind the Newton-Raphson method is to use the tangent line to the graph of f(x) at x0 as an approximation to the function near x0. The next estimate x1 is then the x-intercept of this tangent line, which is given by the formula above.

The method is often very effective for finding solutions to non-linear equations, especially when the derivative f'(x) is easy to compute. However, it can also fail to converge or converge to a wrong solution in some cases, especially if the initial guess is far from the true solution or if the function has multiple roots or is not well-behaved. Let us summarize as follows:

habti.geremew@gmail.com
Newton-Rapson's method for solving nonlinear equations using Matlab

In MATLAB, you can implement the Newton-Raphson method for solving non-linear equations by following these steps:

  1. Define the function f(x) that you want to solve for.
  2. Define its derivative f'(x).
  3. Choose an initial guess x0 for the solution.
  4. Set a tolerance level tol, which determines the accuracy of the solution.
  5. Set a maximum number of iterations max_iter, which determines the maximum number of times the algorithm will iterate before stopping.
  6. Implement the algorithm using a while loop that checks if the solution has converged or if the maximum number of iterations has been reached.
  7. Inside the loop, compute the next estimate x1 using the Newton-Raphson formula: x1 = x0 - f(x0)/f'(x0).
  8. Check if the solution has converged by computing the absolute difference between x1 and x0, and comparing it to the tolerance level. If the difference is less than the tolerance level, stop the loop and return the solution x1.
  9. If the solution has not converged, update x0 to x1 and repeat from step 7.

Here's a simple example MATLAB code that implements the Newton-Raphson method for finding the root of the function.


% Apply Newton Rapson rule to find the root of nonlinear equations
clear all, clc
syms x; % Setting x as symbolic variable
f = input('Enter non-linear equations: ');
x1 = input('Enter initial guess: ');
e = input('Tolerable error in decimal: '); % basic stopping criterion
er=1; % error initialization
df=diff(f); % differentiation
fprintf('x1\t\t\ter\n');
while er>e
x1_old=x1;
fx = eval(subs(f,x,x1));
dfx = eval(subs(df,x,x1));
x1=x1-(fx/dfx); % Newton Rapson methodology of iteration
er=abs((x1-x1_old)/x1); % new error distribution
fprintf('%f\t\t%f\n',x1,er);
end
fprintf('\nRoot is: %f\n', x1);
Here is the result

Enter non-linear equations:
exp(-x)-x
Enter initial guess:
0
Tolerable error in decimal:
0.1/100
x1 er
0.500000 1.000000
0.566311 0.117093
0.567143 0.001467
0.567143 0.000000

Root is: 0.567143
 

No comments:

Post a Comment