from
Newton Raphson method for Transcendental equations
by Chandan
Evaluates the root of transcendental equation using Newton Raphson Method.
|
| NR_Transcendental()
|
% Name : Chandan Kumar
% Title : Solution of Transcendental equation by Newton Raphson Method
% Date : 24th July 2009
% Last Modified : 26th july 2009
% Disclaimer : This code has been specially designed to overcome the
% common faults of the Newton-Raphson method for example
% Infinite cycle,Zero derivative at root or non-existance
% of derivative at root etc.
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function NR_Transcendental()
clc, clear all % Clears the command window and variables
syms x % variable x declared symbol
F_x = input('Input the function f(x): ');
D_F = diff(F_x); % Obtains differentiation of function.
x_initial = input('Give an initial iteration point: '); % User input of initial guess
F_Accuracy = input('Input the tolerance needed[Default 1e-6]: ');
if isempty(F_Accuracy)
F_Accuracy = 1e-6; % No of digit for termination
end
clc
disp('Transcendental equation to be solved is:')
disp(' ');
disp(F_x);
x = x_initial; % Passing argument for eval function
for i = 1:1000 % Initialization loop for iteration
x_prev(i) = x; % Assigning previous value to variable
eval(F_x);
eval(D_F);
if (eval(F_x)~=0)&& (isempty(eval(D_F))) % Condition when First derivative DNE
[f_left,f_right] = eval(F_x(x-0.5, x+0.5)); % Calculates value of F away from point
if (abs(F_x) > abs(f_left)) % Proceeds in the direction of minimum value
x = x - 0.5; % towards left
elseif (abs(F_x) > abs(f_right)) % Towards right
x = x+0.5;
else
disp('First Derivative DOESNOT exists. Solution NOT found!!!');
break;
end
elseif (eval(F_x)~=0)&& (abs(eval(D_F))<F_Accuracy) % Derivative is close to zero
disp('First Derivative is Zero.Solution NOT found!!!');
break;
elseif (abs(eval(F_x))< F_Accuracy)&& (isempty(eval(D_F)))% Derivative DNE at root point.
disp('Root of equation found.');
disp(['Root is: ' num2str(x) '']);
disp(['But function' num2str(F_x) ' DOESNOT have a continuous derivative at root']);
break;
elseif (abs(eval(F_x))< F_Accuracy)&& (eval(D_F)==0) % Derivative is zero at root point
disp('Root of equation found.');
disp(['Root is: ' num2str(x) '']);
disp(['Function' num2str(F_x) ' has optima at root point']);
break;
elseif(abs(eval(F_x)/eval(D_F)) < F_Accuracy) % Desired accuracy for root achieved
disp('Root of equation found.');
disp(['Root is: ' num2str(x) '']);
disp(['No of iteration required is:' num2str(i) '']);
break;
elseif (i > 2)&&(x_prev(i-2) == x) % Case when solution oscillates between two values
disp('Starting point has entered in a cycle. Convergence NOT possible');
index = input('Search for solution using other starting point Y/N or y/n,[y]: ','s');
if isempty(index)||(index== 'Y')||(index=='y') % Validating the request
x = x + 0.5; % Taking starting point outside of interval of cycle
end
else
x = x - (eval(F_x)/eval(D_F)); % Used for next iteration
end
end
|
|
Contact us at files@mathworks.com