Newton Raphson method code for matlab without 'syms x'
This Code is written without 'syms x'. This logic is applicable in any other language and compiler.
The Script Provides a demonstration of the "Newton - Raphson Method" , to solve various polynomial and transcendental equation.
Important feature added: You don't need to do the differentiation manually. Code to find the derivative value is implemented.
Source Code:
clc; clear all; close all;
f=@(x) (x*2.71828^x - cos(x));
x0=input ('x0=');
tol=0.0000001;
N=100;
sqrteps = sqrt(eps);
x(1)=x0;
for n=1:N+1
h=sqrteps;
fp=((f(x(n)+h)-f(x(n)-h))./(2*h));
if fp==0
fprintf('derivative=0')
break
end
x(n+1)=x(n)-f(x(n))/fp;
if abs(x(n+1)-x(n))<=tol
final1=x(n+1);
break
end
end
fprintf('\nThe root of the equation is=%0.5f \n No of Iteration=%d',final1,n)
Input Output:
x0=1
The root of the equation is=0.51776
No of Iteration=5>>
Comments
Post a Comment