0% found this document useful (0 votes)
20 views4 pages

NR Matlab Cod PDF

Uploaded by

Qarar Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

NR Matlab Cod PDF

Uploaded by

Qarar Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

% %ingredients

% Simple cod ONE problem NRM


f=@(x) 2^x-5*x+2;
df=@(x) log(2)*(2^x)-5;
e=10^-4;
x0=0;
n=10;
%processing
if df(x0)~=0
for i=1:n
x1=x0-f(x0)/df(x0)
fprintf('x%d=%.20d\n',i,x1)
if abs(x1-x0)<e
break
end
x0=x1;
end
else
disp('Newton Raphson failed');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%ingredients
% Simple cod Multiple problem NRM
f=input('Enter your function:');
df=input('Enter derivative of this function:');
e=input('Enter tolerance:');
x0=input('Enter initial guess:');
n=input('Enter no of iteration:');
%processing
if df(x0)~=0
for i=1:n
x1=x0-f(x0)/df(x0)
fprintf('x%d=%.20d\n',i,x1)
if abs(x1-x0)<e
break
end
x0=x1;
end
else
disp('Newton Raphson failed');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% 2nd%Matlab Cod of Newton-Raphson


Method(Table)
%%%% Method of Finding Root of Equation
%%%%%%%%% equation f(x)=0
%%% By Zamir Hussain
format short
clear all
clc
syms x
%%% Write the function here
f=@(x) x*log10(x)-1.2;
df=diff(f,x);
dfx=inline(df);
x0=1.5; % Ist intial point
n=4 ; % number of decimal place
if dfx(x0)<10^(-9)
else
Variables={'Iter','x','f_x0','Error'};
iter=1; % Set the initial iteration
Counter
err=abs(f(x0)); % Compute the initial error
epsilon=1*10^(-n-1); % maximum Tolerance
Error %n decimal accuracy
itermax=70; % Maximum Number of iterations
Performed
HG=[];
if dfx(x0)<10^(-9) % derivative should be no-
zero
disp('Wrong choice of initial Guess');
else
while (iter<=itermax && err>=epsilon)
x1=x0-f(x0)/dfx(x0); %compute New Root
err=abs(f(x0)); % Compute the initial
error
HG=[HG;iter x0 f(x0) err]; %For Printing
purpose

iter=iter+1; %Update the Iteration


Counter
x0=x1; % update point
end
end
% For Printing purpose

disp('==================================')
disp('ourput Table with Iteration
wise')
Result=array2table(HG); %Convert Aray
to Table for displaying

Result.Properties.VariableNames(1:size(HG,2))=Va
riables
x0=x0-rem(x0,10^-n); % Convert to n
decimal Places
%
% end
fprintf('Converged solution after %
iteration \n', iter);
fprintf('Root is %4f \n', x0); %Final Root
print
%end
%end
end

You might also like