Newtonraphson Matlab

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

%The Newton Raphson Method

clc;
close all;
clear all;
syms x;
f=((20-x)+(20+x)^(1/2)*(x+x^(1/2)))-155.55%Enter the Function here
g=diff(f) %The Derivative of the Function
n=input('Introduzca el numero de decimales:');
epsilon = 1*10^-(n+1)
x0 = input('Introduzca la aproximacion inicial:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der % The Formula
err=abs(y-x0);
i;
if err<epsilon %checking the amount of error at each iteration
break
end
x0=y;
% fprintf('i x(i) Error absoluto (i)\n');
% fprintf('%2d \t %11.7f \t %11.7f \n',i, x0(i), err(i));
end

y = y - rem(y,10^-n); %Displaying upto required decimal places

fprintf(['errorabsoluto: ',num2str(err),'iteracion: ',num2str(i),'valor p:


',num2str(y)])
fprintf('La ra�z es : %f \n',y);
fprintf('Numero de iteraciones : %d\n',i);

You might also like