0% found this document useful (0 votes)
8 views16 pages

Nmo Final Programs

The document contains multiple MATLAB scripts for various numerical methods including least squares regression, bisection method, Gauss quadrature, Newton-Raphson method, and Runge-Kutta method. Each script prompts the user for input values and performs calculations to solve mathematical problems, providing outputs such as roots of equations and areas under curves. The scripts are authored by Prajwal Thorat and are part of a course labeled TEMEB246.

Uploaded by

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

Nmo Final Programs

The document contains multiple MATLAB scripts for various numerical methods including least squares regression, bisection method, Gauss quadrature, Newton-Raphson method, and Runge-Kutta method. Each script prompts the user for input values and performs calculations to solve mathematical problems, providing outputs such as roots of equations and areas under curves. The scripts are authored by Prajwal Thorat and are part of a course labeled TEMEB246.

Uploaded by

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

16/6/21 11:06 PM C:\Users\hp\Desktop\abraisex.

m 1 of 1

n=input("\n Enter the number of elements=" );


for i=1:n
x(i)=input("\n Enter the values of x= " );
temp=input("\n Enter the value of y=");
y(i)=log(temp);
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+y(i);
S2=S2+x(i)*y(i);
S3=S3+x(i)*x(i);
end
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a1=da/d;
b1=db/d;
b=exp(a1);
a=exp(b1);
fprintf("\ny=(%f)(%f)^x",a,b);
17/2/21 4:56 PM C:\Users\hp\Desktop\bisectmat.m 1 of 2

% 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

%Root of equation is = 2.094635


16/6/21 10:35 PM C:\Users\hp\Desktop\eponent.m 1 of 1

n=input("\n Enter the number of elements=" );


for i=1:n
x(i)=input("\n Enter the values of x= " );
temp=input("\n Enter the value of y=");
y(i)=log(temp);
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+y(i);
S2=S2+x(i)*y(i);
S3=S3+x(i)*x(i);
end
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a1=da/d;
b1=db/d;
b=a1;
a=exp(b1);
fprintf("\n y=(%f)e^(%f)x",a,b);
4/6/21 4:17 PM C:\Users\hp\Desktop\GL2.m 1 of 1

% Prajwal Vijay Thorat


% TEMEB246
% Gauss Legendre/Quadrature 2 point formula

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

Enter lower limit of x : -2


Enter upper limit of x : 2
I= 4.685392
4/6/21 4:19 PM C:\Users\hp\Desktop\GL3.m 1 of 1

% Prajwal Vijay Thorat


% TEMEB246
% Gauss Legendre/Quadrature 3 point formula

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)

Enter lower limit of x : 0


Enter upper limit of x : 1
I= 0.378021
28/5/21 12:17 PM C:\Users\hp\D...\Inverseinterpolation.m 1 of 2

% Prajwal Vijay Thorat


% TEMEB246
% PROGRAM FOR INVERSE INTERPOLATION

n= input("\n Enter the number of elements = " );


yg=input("\n Enter the values of yg= " );

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

Enter the number of elements = 4

Enter the values of yg= 2

Enter the value of x= 0

Enter the values of y=0

Enter the value of x= 1

Enter the values of y=1

Enter the value of x= 2

Enter the values of y=7

Enter the value of x= 3

Enter the values of y=25

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

% Prajwal Vijay Thorat


% TEMEB246
% PROGRAM FOR TWO DIMENSIONAL HEAT EQUATION ( LAPLACE EQUATION )

Tlhs=input(" Enter the value of Tlhs=");


Trhs=input(" Enter the value of Trhs=");
Tupp=input(" Enter the value of Tupp=");
Tlow=input(" Enter the value of Tlow=");

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

Enter the value of Tlhs=0


Enter the value of Trhs=100
Enter the value of Tupp=100
Enter the value of Tlow=0

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

%NEWTON RAPHSON METHOD


% PRAJWAL THORAT
% TEMEB246

x1=input('Enter initial guess =' );


acc=input('Enter given accuracy =');
while ((f(x1)*ddf(x1)/(df(x1)^2)))>1
x1=input('Enter initial guess again = ' );
end
x2=x1-f(x1)/df(x1);
while abs(x2-x1)>acc
x1=x2;
x2=x1-f(x1)/df(x1);
end
fprintf('Root of equation is %f',x2);

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

%Enter initial guess =0.5


%Enter given accuracy =0.01
%Root of equation is 2.094556
28/5/21 12:45 PM C:\Users\hp\Desktop\RKFOURTHORDER.m 1 of 1

% Prajwal Vijay Thorat


% TEMEB246
% PROGRAM TO SLOVE ORDINARY DIFFERENTIAL EQUATIONS BY RUNGE KUTTA 4TH ORDER

f=inline('((x^2)+(y^2))');

xo=input("\n enter the value of xo=");


yo=input("\n enter the value of yo=");
xg=input("\n enter the value of xg=");
h=input("\n enter the value of step size h=" );

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

enter the value of xo=0

enter the value of yo=0

enter the value of xg=0.4

enter the value of step size h=0.2

The final value of yg =0.021360


4/6/21 4:11 PM C:\Users\hp\Desktop\simp13.m 1 of 1

% Prajwal Vijay Thorat


% TEMEB246
% Simpson’s 1/3 rd Rule

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

Enter value of lower limit x0 = 1


Enter value of upper limit xn = 2
Enter the number of strips = 6
Total area under the curve =3.059142
18/5/21 11:31 AM C:\Users\hp\Desktop\Straigth.m 1 of 2

%PRAJWAL THORAT
% TEMEB246
% LEAST SQUARE REGRESSION TO FIT A STRAIGHT LINE

n=input(" Enter the number of inputs =" );


for i=1:n
x(i)=input(" Enter the values of x =");
y(i)=input("Enter the values of y =");
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+y(i);
S2=S2+x(i)*y(i);
S3=S3+x(i)*x(i);
end
fprintf("-----------------------------\n" );
fprintf(" x y x^2 xy\n" );
fprintf("------------------------------\n" );
for i=1:n
fprintf("%f %f %f %f\n" ,x(i),y(i),x(i)^2,x(i)*y(i));
end
fprintf("-----------------------------\n" );

d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a=da/d;
b=db/d;

fprintf(" Ans a=%f b=%f ",a,b);


fprintf("\n\t\ty=%fx+%f",a,b);

% OUTPUT

%Enter the number of inputs =7


%Enter the values of x =1
% Enter the values of x =2
% Enter the values of x =3
% Enter the values of x =4
%Enter the values of y =4
%Enter the values of x =5
%Enter the values of y =3.5
% Enter the values of x =6
%Enter the values of y =6
% Enter the values of x =7
18/5/21 11:31 AM C:\Users\hp\Desktop\Straigth.m 2 of 2

%Enter the values of y =5.5


-----------------------------
% x y x^2 xy
------------------------------
%2.000000 2.500000 4.000000 5.000000
%4.000000 4.000000 16.000000 16.000000
%6.000000 6.000000 36.000000 36.000000
%7.000000 5.500000 49.000000 38.500000
-----------------------------
%Ans a=0.839286 b=0.071429

% y=0.839286x+0.071429
4/6/21 4:07 PM C:\Users\hp\Desktop\trap.m 1 of 1

% Prajwal Vijay Thorat


% TEMEB246
% Trapezoidal rule

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

Enter value of lower limit x0 : 0


Enter value of upper limit xn : 3
Enter the number of strips : 6
Total area under the curve = -0.125000
16/6/21 10:47 PM C:\Users\hp\Desktop\yxraiseb.m 1 of 1

n=input("\n Enter the number of elements=" );


for i=1:n
x(i)=input("\n Enter the values of x= " );
y(i)=input("\n Enter the value of y=");
X(i)=log(x(i));
Y(i)=log(y(i));
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+X(i);
S1=S1+Y(i);
S2=S2+X(i)*Y(i);
S3=S3+X(i)*X(i);
end
A=(n*S2-S0*S1)/(n*S3-S0*S0);
B=(S1-A*S0)/n;
b=A;
a=exp(B);
fprintf("\ny=(%f)x^(%f)",a,b);

You might also like