lab
lab
%Newton Raphson
f = input ('Enter your function: ');
df = input ('Enter derivative of this fuction: ');
e = input ('Enter tolerance: ');
x0 = input ('Enter the initial guess: ');
n = input ('Enter the no of iterration: ');
%processing
if df(x0)~=0
for i=1:n
x1 = x0 -f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1)
if abs(x1-x0)<e
break
end
x0 = x1;
end
else
disp('Newton Raphson Failed')
end
%bisection method
f = input ('Enter yout function: ');
a = input ('Enter left side of interval: ');
b = input ('Enter Right side of interval: ');
n = input ('Enter no of iterations: ');
e = input ('Enter tolerance: ');
if f(a)*f(b)<0
for i=1:n
c = (a+b)/2;
fprintf('p%d=%.4f\n',i,c)
if abs(c-b)<e || abs(c-a)<e
break
end
if f(a)*f(c)<0
b=c;
elseif f(b)*f(c)<0
a=c;
end
end
else
disp ('No root between given method')
end
%Regula Falsi
f = input ('Enter yout function: ');
a = input ('Enter left side of interval: ');
b = input ('Enter Right side of interval: ');
n = input ('Enter no of iterations: ');
e = input ('Enter tolerance: ');
if f(a)*f(b)<0 && a<b
for i=1:n
c = (a*f(b)-b*f(a))/(f(b)-f(a));
fprintf('p%d=%.4f\n',i,c)
if abs (f(c))<e
break
end
if f(a)*f(c)<0
b=c; % putting the value of "c" to "b"
elseif f(b)*f(c)<0
a=c; % putting the value of "c" to "a"
end
end
else
disp ('No root between given method')
end
%Secant method
f=input('Enter the function:');
x0=input('Enter the first intial guess:');
x1=input('Enter the 2nd intial guess:');
e=input('Enter the tolerance:');
n=input('Enter no of iteration:');
%processing
for i=2:n
x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
fprintf('x%d=%0.10f\n',i,x2)
if abs(x2-x1)<e
break
end
x0=x1;
x1=x2;
end