You are on page 1of 14

ROOT FINDING

BISECTION
x1=input('enter x1:');
x2=input('enter x2:'); %[a,b]
n=input('enter n:');
f= @(x) x.^2-5*x+6; %f=inline('x*x -5*x +6');
y1=f(x1);
y2=f(x2);
for i=1:n
x3=(x1+x2)/2; %c
y3=f(x3);
if abs(y3)<=0.0001
break;
end
if y1*y3<0.0
x2=x3; %b=x3, [a,x3]
y2=f(x2); % x3 inputedfor replace of b
else
x1=x3; %a=x3 , [x3,b]
y1=f(x1); %x3 inputed for replace of a
end
end
fprintf('Bisection Result is %f:\n ', x3);
ITERATION:
fun = @(x) 1/sqrt(x + 1);
root = 0;
prevRoot = input('enter initial value: ');
for i = 1:1:100
root = fun(prevRoot);
if abs(fun(root) - fun(prevRoot)) <= 0.0001
break;
else
prevRoot = root;
end
end
fprintf('ans = %f\n iteration: %d\n', root, i);

ANOTHER
f= @(x) x.^2-5*x+6;
phi=@(x) ((x.^2-6)/5);
x= 0;
choice = input('enter initial value: ');
for i = 1:1:100
x = f(choice);
if abs(phi(x) - phi(choice)) <= 0.0001
break;
else
choice = x;
end
end
fprintf('ans = %f\n-iteration: %d\n', x, i);
SECANT REGULA
f = @(x) x*x-5*x+6; %x*x*x - 2*x - 5;
a = input('enter a: ');
b = input('enter b: ');
x = 0;
for i = 1:1:100
x =((a*f(b)-b*f(a))/(f(b)-f(a)));
%x = a - (f(a)*(b - a))/((f(b) - f(a)));
if f(x) > 0.00001
b = x;
else
a = x;
end
if abs(f(x)) <= 0.00001
break;
end
end
fprintf('ans = %f\niteration is %d\n', x, i);

%if i > 100


fprintf('root can not be find in %d iteration',i);
else
fprintf('root is: %f\nFound by iteration %d\n',
root, i);
end
NEWTON
f=@(x) x.^2-5*x+6; %givenFun = @(x) x*x*x - 2*x - 5;
df=@(x) 2*x-5; %derivOfFun = @(x) 3*x*x - 2;

%0r z=diff(f(x)) %df=@(x) 2*x-5; %derivOfFun = @(x)


3*x*x - 2;
df= inline(z)

x0 = input('Enter the initial point: ');


root = 0;
for i = 1:1:100
if df(x0) == 0
fprintf('Root can not be found because
derivative is zero at = %f\n:', x0);
break
else
root = x0 - (f(x0)/df(x0));
end
if abs(f(x0) - f(root)) <= 0.00001
break;
else
x0 = root;
end
end
if i > 100
fprintf('root can not be find in %d iteration',
i);
else
fprintf('root is: %f\nFound by iteration %d\n',
root, i);
end
syms x;
f=inline ('x.^3-2*x-5'); %('x.^2-5*x+6');
z=diff(f(x));
df1=inline(z);
x0=input('Enter the initial value:');
x=x0;
for i=1:100
y=x;
x=y-(f(x)/f1(x));
if abs(x-y)<=.00001
break;
end
end
x
NUMERICAL INTEGRATION
:::::::::::::::::::::::::::
:::::::

TRAPEZOIDAL
f=@(x) 1/(1+x)
n=4
a=0
b=1
h=(b-a)/n
s=0
for i=1:n-1
x=a+i*h
s=s+f(x)
end
t=(h/2.0)*(f(a)+2.0*s+f(b))
t
SIMPSON 1/3
f=@(x) 1/(1+x.^2)
h=0.5
a=0
b=1
n=(b-a)/h
s1=0
for i=1:2:n-1
x1=a+i*h
s1=s1+f(x1)
end
s2=0
for i=2:2:n-1
x2=a+i*h
s2=s2+f(x2)
end
t=(h/3.0)*(f(a)+4.0*s1+2.0*s2+f(b))
t
SIMPSON3/8
f=@(x) 1/(1+x)
h=0.125
a=0
b=1
n=(b-a)/h
s1=0
for i=1:3:n-1
x1=a+i*h
s1=s1+f(x1)
end
s2=0
for i=2:3:n-1
x2=a+i*h
s2=s2+f(x2)
end
s3=0
for i=3:3:n-1
x2=a+i*h
s3=s3+f(x2)
end
t=((3.0*h)/8.0)*(f(a)+3.0*s1+3.0*s2+2.0*s3+
f(b))
t
WEDDLE’S
f=@(x) (cos(x)-log(x)+exp(x)); %exp(sin(x)); %log(x)
;

low=input('Enter the Lower limit: ');


up=input('Enter the Upper limit: ');
%n=input('Number of Subintervals: ');
h=input('h: ');
%h=abs(up-low)/n;
n=(abs(up-low)/h)+1;

m=fix(n/6 + 1e-14);
x0=low;
sum=0;

for i=1:m
f1=f(x0);
f2=f(x0+h);
f3=f(x0+2*h);
f4=f(x0+3*h);
f5=f(x0+4*h);
f6=f(x0+5*h);
f7=f(x0+6*h);
sum = sum + ( f1 + 5*f2 + f3 + 6*f4 + f5 + 5*f6 + f7
);
x0=x0+6*h;
%x0
end

fprintf('Weddle Result is %f:\n ', sum*h*3/10);


IVP
EULER METHOD
syms y(x);
eqn = diff(y, x) == 3*x + y/2;

functionalValue = @(x, y) 3*x + y/2;

soltuionByDefault = dsolve(eqn, cond, 'x')


exactResultByDefault
=inline(vectorize(soltuionByDefault));

plot(xAxisValues,exactResultByDefault(xAxisValues),'bo-
');

hold on;

plot(xAxisValues,yAxisValue,'r.-');

legend('Exact Solution','Approxi Solution');


EULER METHOD

f=@(x, y) (-y);
disp('Enter the intial condition (x0, y0):')
x0=input('Enter x0: ');
y0=input('Enter y0: ');

xp=input('Enter the value of xp to find yp ');

h=input('Enter the interval size h : ');

n=fix(abs(xp-x0)/h + 1e-14);

for i=1:n

y1 = y0 + h*f(x0,y0);

x1=x0+h;
x0=x1;
y0=y1;

end

fprintf('Euler result at %f is %f\n\n',xp, y1)

clear all
close all
RK2

f=@(x, y) y-x; %1+x*y; %x^2 + y;


disp('Enter the intial condition (x0, y0):')
x0=input('Enter x0: ');
y0=input('Enter y0: ');

xp=input('Enter the value of xp to find yp: ');

h=input('Enter the interval size h : ');

n=fix(abs(xp-x0)/h + 1e-14);

for i=1:n

k1=h*f(x0 , y0);
k2=h*f(x0+h , y0+k1);

y1= y0 + 0.5*(k1 + k2);

x1=x0+h;
x0=x1;
y0=y1;
end

fprintf('Runge-Kutta 2 result at %f is %f\n\n',xp, y1)


RK4

f=@(x, y) 3*x + y/2; %y-x; %1+x*y; %x^2 + y;


disp('Enter the intial condition (x0, y0):')
x0=input('Enter x0: ');
y0=input('Enter y0: ');

xp=input('Enter the value of xp to find yp: ');

h=input('Enter the interval size h : ');

n=fix(abs(xp-x0)/h + 1e-14);

for i=1:n

k1=h*f(x0 , y0);
k2=h*f(x0+h/2 , y0+k1/2);
k3=h*f(x0+h/2 , y0+k2/2);
k4=h*f(x0+h , y0+k3);

y1= y0 + (1/6)*(k1 + (2*k2 + 2*k3) + k4);

x1=x0+h;
x0=x1;
y0=y1;
end

fprintf('Runge-Kutta 4 result at %f is %f\n\n',xp, y1)

You might also like