Matlab
Matlab
if f(b)*f(c)<0
else
fprintf('The guess is incorrect! Enter the new guess\n');
b=input ('Enter the first interval: \n');
c=input ('Enter the second interval: \n');
end
m= (b+c)/2;
while abs(c-b)>tol
if (f(b)*f(m))<0
c=m;
else
b=m;
end
m= (b+c)/2;
end
fprintf('\n The value of root using bisection method is %f',m);
f=inline(a);
dif=diff(str2sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
tol(i)=abs((x(i+1)-x(i))/x(i));
if tol(i)<tol
break
end
end
fprintf('\n The value of root using Newton-rapson method is %f',x(i));
%Lagrange's interpolation
x= input('Please enter x values: ');
y=input('Please enter y values: ');
s=0;
a=input('Enter the desire value: ');
for i=1:length(x)
u=1;
l=1;
for j=1:length(x)
if j~=i
u=u*(a-x(j));
l=l*(x(i)-x(j));
end
end
s=s+ u/l *y(i);
end
disp(s);
a=input ('Enter the given function:','s');
f=inline(a);
b=input ('Enter the lower limit: ');
c=input ('Enter the upper limit: ');
n=input ('Enter the number of segments : ');
h=(c-b)/n;
l=f(b)+f(c);
for i=1:n-1
l=l+2*f(b+i*h);
end
l=l*h/2;
fprintf('\n The value of integration of the function using trapezium method is %f',l);
for i=1:n-1
if(rem(n,3)==0)
l=l+2*f(b+i*h);
else
l= l+3*f(b+i*h);
end
end
l=l*3*h/8;
fprintf('\n The value of integration of the function using simson 3/8 method is %f\n',l);