0% found this document useful (0 votes)
6 views

practical file java

The document contains a series of practical programming exercises that demonstrate various numerical methods for evaluating integrals and solving differential equations using MATLAB. Each practical includes code snippets for methods such as the Trapezoidal rule, Simpson's rules, Boole's rule, Weddle's rule, Lagrange's interpolation, and both Euler's and Runge-Kutta methods. The exercises provide a hands-on approach to understanding these numerical techniques through coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

practical file java

The document contains a series of practical programming exercises that demonstrate various numerical methods for evaluating integrals and solving differential equations using MATLAB. Each practical includes code snippets for methods such as the Trapezoidal rule, Simpson's rules, Boole's rule, Weddle's rule, Lagrange's interpolation, and both Euler's and Runge-Kutta methods. The exercises provide a hands-on approach to understanding these numerical techniques through coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical-12

WAP to evaluate integral using Trapezoidal rule

Code:
clc;
clear all();
x0=input('enter value of x0:');
xn=input('enter value of xn:');
n=input('enter number of splits:');
h=(xn-x0)/n;
f=@(x) 1./(x.^2);
y0=f(x0);
yn=f(xn);
In=0;
for i=x0+h:h:xn-h
In=In+feval(f,i);
end
D=2*In+y0+yn;
I=D*(h/2);
fprintf('\n itegrate of function is:%f',I);

Output:
Practical-13

WAP to evaluate integral using Simpson 1/3rd rule.

Code:

clc;
clear all();
x0=input('enter value of x0:');
xn=input('enter value of xn:');
n=input('enter number of splits(which is divisble by 2):');
h=(xn-x0)/n;
f=@(x) 1./(x.^2);
y0=f(x0);
yn=f(xn);
In=0;
In1=0;
for i=x0+h:2*h:xn-h
In=In+feval(f,i);
end
for i=x0+2*h:2*h:xn-2*h
In1=In1+feval(f,i);
end
D=4*In+2*In1+y0+yn;
I=D*(h/3);
fprintf('\n integrate of function is:%f',I);

Output:
Practical-14

WAP to evaluate integral using Simpson 3/8 rule.

Code:

clc;
clear all();
x0=input('enter value of x0:');
xn=input('enter value of xn:');
n=input('enter number of splits(which is divisble by 3):');
h=(xn-x0)/n;
f=@(x) 1./(1+x.^2);
y0=f(x0);
yn=f(xn);
In=0;
In1=0;
m=0;
for i=x0+h:h:xn-h
m=m+1;
if(rem(m,3)==0)
In1=In1+feval(f,i);
else
In=In+feval(f,i);
end
end
D=3*In+2*In1+y0+yn;
I=D*(3*h/8);
fprintf('\n integrate of function is:%f',I);

Output:
Practical-15

WAP to evaluate integral using Boole’s rule.

Code:

clc;
clear all();
x0=input('enter value of x0:');
xn=input('enter value of xn:');
n=input('enter number of splits(which is divisible by 4):');
h=(xn-x0)/n;
f=@(x) 1./(4*x+5);
y0=f(x0);
yn=f(xn);
In=0;
In1=0;
In2=0;
m=0;

for i=x0+h:h:xn-h
m=m+1;
if(rem(m,4)==1||rem(m,4)==3)
In=In+feval(f,i);
elseif(rem(m,4)==2)
In1=In1+feval(f,i);
else
In2=In2+feval(f,i);
end
end
D=32*In+7*y0+7*yn+12*In1+14*In2;
I=D*(2*h/45);
fprintf('\n integrate of function is:%f',I);

Output:
Practical-16

WAP to evaluate integral using Weddle’s rule.

Code:

clc;
clear all;
x0=input('enter value of x0:');
xn=input('enter value of xn:');
n=input('enter number of splits(which is divisible by 4):');
h=(xn-x0)/n;
f=@(x) 1./(1+x.^2);
y0=f(x0);
yn=f(xn);
In=0;
In1=0;
In2=0;
In3=0;
m=0;
for i=x0+h:h:xn-h
m=m+1;
if(rem(m,6)==1||rem(m,6)==5)
In=In+feval(f,i);
elseif(rem(m,6)==2||rem(m,6)==4)
In1=In1+feval(f,i);
elseif(rem(m,6)==3)
In3=In3+feval(f,i);

else
In2=In2+feval(f,i);
end
end
D=5*In+y0+yn+In1+2*In2+6*In3;
I=D*(3*h/10);
fprintf('\n integrate of function is:%f',I);

Output:
Practical-17

WAP for Lagrange’s Interpolation formula.

Code:

clc;
clear all();
a=inputdlg({'x-coordinate points:','y coordinate points:','find value
at point:'},'lagrange interpolation',[1 50;1 50;1 50]);
x=str2num(a{1});
y=str2num(a{2});
n=size(x,2);
m=size(y,2);
if n~=m
f=errordlg('size of both sets must be same','file name');
end
d=str2double(a{3});
y1=0;
for i=1:n
p=1;
for j=1:n
if i~=j
p=p*((d-x(j))/(x(i)-x(j)));
end
end
y1=y1+y(i)*p;
end
fprintf('\n The value of y coordinate at x=%d if %f',d,y1);

Output:
Practical-18

WAP to solve differential equation using Euler’s method.

Code:

clc;
clear all()
f=@(x,y) x^2+y;
x0=0;
y0=1;
x=0.1;
h=0.01;
n=(x-x0)/h;
for i=1:n
y=y0+h*f(x0,y0);
fprintf('\n Value of y at %f is %f',x0,y);
x0=x0+h;
end

Output:
Practical-19

WAP to solve differential equation using Runga Kutta method.

Code:

clc;
clear all();
f=@(x,y)y-x;
x_0=0;
y_0=2;
x=0.2;
h=0.1;
n=round((x-x_0)/h);
y_new=0;
for i=1:n
k1=h*f(x_0,y_0);
k2=h*f(x_0+h/2,y_0+k1/2);
k3=h*f(x_0+h/2,y_0+k2/2);
k4=h*f(x_0+h,y_0+k3);
y_new=y_0+(1/6)*(k1+2*(k2+k3)+k4);
x_0=x_0+h;
fprintf('\n The value of y(%f) is:%f',x_0,y_new);
y_0=y_new;
end

Output:

You might also like