0% found this document useful (0 votes)
6 views2 pages

Matlab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Matlab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

%bisection

a=input ('Enter the given function:','s');


f=inline(a);
b=input ('Enter the first interval: ');
c=input ('Enter the second interval: ');
tol=input ('Enter the tolarance: ');

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);

a=input ('Enter the given function:','s');


x(1)= input('Enter initial guess: ');
tol=input ('Enter the tolarance: ');

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);

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);
if (rem(n,3)==0)
else
fprintf('The guess is incorrect! Enter the new guess\n');
n=input ('Enter the number of segments : ');
end

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);

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);
if (rem(n,3)==0)
else
fprintf('The guess is incorrect! Enter the new guess\n');
n=input ('Enter the number of segments : ');
end
for i=1:2:n-1
l=l+4*f(b+i*h);
l= l+3*f(b+i*h);
end
for i=2:2:n-2
l=l+2*f(b+i*h);
end
l=l*h/3;
fprintf('\n The value of integration of the function using simson 3/8 method is %f',l);

You might also like