You are on page 1of 2

Assignment-No 1

Newton Raphson:
clc;close all;
syms x
f(x)=('enter any non linear equation in the form of x:')
a=input('enter the first value of any interval:');
b=input('enter the second value of any interval:');
df(x)=diff(f(x),x);
while(f(a)*f(b))>0||(f(a)*f(b))==0
a=input('enter the first value of interval again:');
b=input('enter the second value of interval again:');
end
x0=(a+b)/2;
n=0
x(n+1)=x0-(f(x0)/df(x0));
while abs(f(x(n=1)))>0.0001
n=n+1;
x(n=1)=x(n)-(f(x)(n)/df(x(n))/df(x(n)));
end
disp(double(x(n+1)))
run code

Regula falsi:
clear all;
close all;
clc;
f=inline('x^2-2');
x0=input('Enter x0=');
x1=input('Enter x1=');
tol=input('Enter tolerance=');
itr=input('Enter number of iteration=');
p=0;
for i=1:itr
x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
if abs(x2-x1) < tol
p=1;
k=i;
break;
else
if f(x0)*f(x2)<0
x1=x2;
else
x0=x2;
end
end
end

if p==1
fprintf('Solution is %f at iteration %i',x2,k)
else
fprintf('No convergent solution exist in the given number iteration')
end

You might also like