Nmo Final Programs
Nmo Final Programs
m 1 of 1
% BISECTION METHOD
% PRAJWAL THORAT
% TEMEB246
function bisec12
clc
el=input('Enter f(x)=','s');
f=inline(el);
n=input('Enter No. of Iterations =');
a=input('Enter Initial Guess a=');
b=input('Enter Initial Guess b=');
while(f(a)*f(b)>0)
disp('Initial Guesses are not correct ' );
a=input('Enter Initial Guess a=');
b=input('Enter Initial Guess b=');
end
fprintf('\n Itr.No.\t a\t\t b \t\t xr' )
for i=1:n
xr=(a+b)/2;
if(f(xr)*f(b)<0)
a=xr;
else
b=xr;
end
fprintf('\n %d \t\t %f\t %f \t %f',i,a,b,xr);
end
fprintf('\n\tRoot of equation is = %f' ,xr);
%OUTPUT
% Enter f(x)=(x*x*x)-(2*x)-5
%Enter No. of Iterations =15
%Enter Initial Guess a=0
%Enter Initial Guess b=3
%Itr.No. a b xr
% 1 1.500000 3.000000 1.500000
% 2 1.500000 2.250000 2.250000
% 3 1.875000 2.250000 1.875000
% 4 2.062500 2.250000 2.062500
% 5 2.062500 2.156250 2.156250
% 6 2.062500 2.109375 2.109375
% 7 2.085938 2.109375 2.085938
% 8 2.085938 2.097656 2.097656
% 9 2.091797 2.097656 2.091797
% 10 2.091797 2.094727 2.094727
% 11 2.093262 2.094727 2.093262
% 12 2.093994 2.094727 2.093994
% 13 2.094360 2.094727 2.094360
% 14 2.094543 2.094727 2.094543
% 15 2.094543 2.094635 2.094635
17/2/21 4:56 PM C:\Users\hp\Desktop\bisectmat.m 2 of 2
f=inline('(exp(-x/2))')
xl=input ('Enter lower limit of x : ');
xu=input ('Enter upper limit of x : ');
a=(xu-xl)/2;
b=(xu+xl)/2;
fu1=a*f(a*(-1/sqrt(3))+b);
fu2=a*f(a*(1/sqrt(3))+b);
area=fu1+fu2;
fprintf ('I= %f',area);
%OUTPUT
f =
Inline function:
f(x) = (exp(-x/2))
f=inline('((exp(x)*cos(x))-2*x)')
xl=input ('Enter lower limit of x : ');
xu=input ('Enter upper limit of x : ');
a=(xu-xl)/2;
b=(xu+xl)/2;
fu1=a*f(a*(sqrt(0.6))+b);
fu3=a*f(a*(-sqrt(0.6))+b);
fu2=f(b);
area=(5/9)*(fu1+fu3)+(8/9)*fu2*a;
fprintf ('I= %f',area);
%OUTPUT
f =
Inline function:
f(x) = ((exp(x)*cos(x))-2*x)
for i=1:n
x(i)=input("\n Enter the value of x= ");
y(i)=input("\n Enter the values of y=");
end
xg=0;
for i=1:n
nu=1;
de=1;
for j=1:n
if(i~=j)
nu=nu*(yg-y(j));
de=de*(y(i)-y(j));
end
end
L(i)=nu/de;
xg=xg+L(i)*x(i);
end
fprintf("\n xg=%f",xg);
%OUTPUT
xg=1.716138
28/5/21 12:17 PM C:\Users\hp\D...\Inverseinterpolation.m 2 of 2
28/5/21 4:08 PM C:\Users\hp\Desktop\LAPLACE.m 1 of 1
a=[ 4 -1 -1 0;
-1 4 0 -1;
-1 0 4 -1 ;
0 -1 -1 4 ] ;
b(1,1)=Tlhs+Tupp;
b(2,1)=Tupp+Trhs;
b(3,1)=Tlhs+Tlow;
b(4,1)=Tlow+Trhs;
v=linsolve(a,b);
fprintf("\n Result=\n");
for i=1:4
fprintf("\n T%d:%f",i,v(i));
end
% OUTPUT
Result=
T1:50.000000
T2:75.000000
T3:25.000000
T4:50.000000
17/2/21 10:28 PM C:\Users\hp\Desktop\NRM.m 1 of 1
function y1=f(x)
y1=(x*x*x)-(2*x)-5;
end
function y2=df(x)
y2=(3*x*x)-2;
end
function y3=ddf(x)
y3=(6*x);
end
%OUTPUT
f=inline('((x^2)+(y^2))');
n=(xg-xo)/h;
for i=1:n
k1=h*f(xo,yo);
k2=h*f(xo+(h/2),yo+(k1/2));
k3=h*f(xo+(h/2),yo+(k2/2));
k4=h*f(xo+h,yo+k3);
k=(k1+2*k2+2*k3+k4)/6;
yg=yo+k;
xo=xo+h;
yo=yg;
end
fprintf("\n The final value of yg =%f" ,yg);
% OUTPUT
f=inline('(exp(x)/x)');
x0=input('Enter value of lower limit x0 = ' );
xn=input('Enter value of upper limit xn = ' );
n=input('Enter the number of strips = ' );
while(mod(n,2)~=0)
n=input('Enter the number of strips again = ' );
end
h=(xn-x0)/n;
res=0;
for i=1:n-1
if(mod(i,2)~=0)
res=res+4*f(x0+i*h);
else
res=res+2*f(x0+i*h);
end
end
temp=f(x0)+f(xn);
area=(h/3)*(res+temp);
fprintf('Total area under the curve =%f' , area);
%OUTPUT
%PRAJWAL THORAT
% TEMEB246
% LEAST SQUARE REGRESSION TO FIT A STRAIGHT LINE
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a=da/d;
b=db/d;
% OUTPUT
% y=0.839286x+0.071429
4/6/21 4:07 PM C:\Users\hp\Desktop\trap.m 1 of 1
f=inline('(2*x-x^2)');
x0=input('Enter value of lower limit x0 : ' );
xn=input('Enter value of upper limit xn : ' );
n=input('Enter the number of strips : ' );
h=(xn-x0)/n;
res=0;
for i=1:n-1
res=res +(f(x0+i*h));
end
res=2*(res);
temp=f(x0)+f(xn);
area=(h/2)*(res+temp);
fprintf('Total area under the curve = %f' ,area);
% OUTPUT