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

Bisec

The document describes experiments using numerical integration techniques like Trapezoidal Rule, Simpson's 1/3 Rule, and Simpson's 3/8 Rule to evaluate definite integrals. It also uses the Runge-Kutta method of order 2 and 4 to solve initial value problems.

Uploaded by

tanjimalmahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Bisec

The document describes experiments using numerical integration techniques like Trapezoidal Rule, Simpson's 1/3 Rule, and Simpson's 3/8 Rule to evaluate definite integrals. It also uses the Runge-Kutta method of order 2 and 4 to solve initial value problems.

Uploaded by

tanjimalmahi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

% Experiment No: 04

% Experiment Name: Newton’s Forward Interpolation Method


% Date: 18.01.2024
Code:

x=input('Enter list of abscissa:');


y=input('Enter list of ordinates:');
p0=input('Enter point of approximation:');
n=length(x);
h=x(2)-x(1);
f=zeros(n,n);
f(:,1)=y;
for j=2:n
for i=j:n
f(i,j)=f(i,j-1)-f(i-1,j-1);
end
end
f;
c=f(n,n);
for k=n-1:-1:1
p=poly(x(1))/h;
p(2)=p(2)-(k-1);
c=conv(c,p)/k;
m=length(c);
c(m)=c(m)+f(k,k);
end
c;
a=polyval(c,p0);
fprintf('Approximate value at given data point
is:%.4f\n',a);
x=linspace(x(1),x(n),100);
y=polyval(p,x);
plot(x,y,'r')
hold on
plot(x,y,'o')

Output:
Enter list of abscissa:[5 10 15 20]

Enter list of ordinates:[4 4.5 4.6 5]

Enter point of approximation:4

Approximate value at given data point is:3.7904


% Experiment No: 05
% Experiment Name: Fit a straight line and parabola best fits
the given data

x 0.1 0.4 0.5 0.7 0.7 0.9


y 0.61 0.92 0.99 1.52 1.47 2.03

% Date: 08.02.2024
Code:
X=input('Enter list of abscissa:');
Y=input('Enter list of ordinates:');
F=input('Enter 1 for linear fit, 2 for parabolic and so on:');
n=length(X);
N=F+1; %For dimension of matrix A
A= zeros(N,N);
for i=1:N
for j=1:N
A(i,j)=sum(X.^(i+j-2));
end
end
B=zeros(N,1);
for k=1:N
B(k)=sum((X.^(k-1)).*Y);
end
U=A\B;
disp('Your polynomial is:')
for k=N:-1:2
fprintf('+(%.2fx^%d)',U(k),k-1)
end
fprintf('(%.4f)\n',U(1))
%plotting
P= flip(U);
X= linspace (X (1),X (n),100);
Y= polyval(P,X);
plot (X, Y, 'r')
hold on
plot(X,Y,'o')

Output:

Enter list of abscissa:[0.1 0.4 0.5 0.7 0.7 0.9]

Enter list of ordinates:[0.61 0.92 0.99 1.52 1.47 2.03]

Enter 1 for linear fit, 2 for parabolic and so on:2

Your polynomial is:

+(1.73x^2)+(0.06x^1)(0.5871)
% Experiment No: 4.1
1 1
% Experiment Name: Evaluate I=∫0 1+𝑥 𝑑𝑥 by Trapezoidal Rule.

% Date: 29.02.2024

Code:
f= input('Enter your function: f(x)=');
a= input('Enter your lower limit a=');
b= input('Enter your upper limit b=');
n= input('Enter your number of stripe n=');
h= (b-a)/n;
y= zeros(1,n-1);
for i=1:1:n-1
y(i)=f(a+i*h);
end
i= 1:1:n-1;
I= h/2*(f(a)+2*(sum(y(i)))+f(b));
fprintf('the value of integral is %0.3f\n',I);

Output:
Enter your function: f(x)=@(x) 1/(1+x)
Enter your lower limit a=0
Enter your upper limit b=1
Enter your number of stripe n=4
the value of integral is 0.697
% Experiment No: 4.2
7
% Experiment Name: Evaluate I=∫3 x 2 log10 (x) 𝑑𝑥 by Simpson's 1/3
Rule.
% Date: 29.02.2024

Code:
f= input('Enter your function: f(x)=');
a= input('Enter your lower limit a=');
b= input('Enter your upper limit b=');
n= input('Enter your number of stripe n=');
h= (b-a)/n;
y= zeros(1,n-1);
for i=1:1:n-1
y(i)=f(a+i*h);
end
i= 2:2:n-2;
k=1:2:n-1;
I= h/3*(f(a)+2*(sum(y(i)))+4*(sum(y(k)))+f(b));
fprintf('the value of integral is %0.3f\n',I);

Output :
Enter your function: f(x)=@(x) (x^2)*log10(x)
Enter your lower limit a=3
Enter your upper limit b=7
Enter your number of stripe n=4
the value of integral is 77.079
% Experiment No: 05
7
% Experiment Name: Evaluate I=∫3 x 2 log10 (x) 𝑑𝑥 by Simpson's 3/8
Rule.
% Date: 07.03.2024

Code:
f= input('Enter your function: f(x)=');
a= input('Enter your lower limit a=');
b= input('Enter your upper limit b=');
n= input('Enter your number of stripe n=');
h= (b-a)/n;
y= zeros(1,n-1);
for i=1:1:n-1
y(i)=f(a+i*h);
end
i= 3:3:n-3;
k= 1:1:n-1;
I= 3*h/8*(f(a)+2*(sum(y(i)))+3*((sum(y(k)))-
(sum(y(i))))+f(b));
fprintf('the value of integral is %0.3f\n',I);

Output:
Enter your function: f(x)=@(x) (x^2)*log10(x)
Enter your lower limit a=3
Enter your upper limit b=7
Enter your number of stripe n=4
the value of integral is 79.150
% Experiment No: 6.1
% Experiment Name: Runge-Kutta 2nd order method.
% Date: 14.03.2024
Code:

f=input('Enter your x and y function: ');


h=input('Enter your interval length: ');
x0=input('Enter initial value of x: ');
y0=input('Enter initial value of y: ');
x1=input('Enter final value of x: ');
n=(x1-x0)/h;
x=zeros(1,n);
y=zeros(1,n);
x(1)=x0;
y(1)=y0;
for i=1:n
x(i+1)=x0+i*h;
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h,y(i)+k1);
y(i+1)=y(i)+(1/2)*(k1+k2);
fprintf('the required value of y(%.2f) is
%.4f\n',x(i+1),y(i+1));
end

Output:

Enter your x and y function: @(x,y) y-x

Enter your interval length: 0.05

Enter initial value of x: 0

Enter initial value of y: 2

Enter final value of x: 0.1

the required value of y(0.05) is 2.1012

the required value of y(0.10) is 2.2051


% Experiment No: 6.2
% Experiment Name: Runge-Kutta 4th order method.
% Date: 14.03.2024
Code:

f=input('Enter your x and y function: ');


h=input('Enter your interval length: ');
x0=input('Enter initial value of x: ');
y0=input('Enter initial value of y: ');
x1=input('Enter final value of x: ');
n=(x1-x0)/h;
x=zeros(1,n);
y=zeros(1,n);
x(1)=x0;
y(1)=y0;
for i=1:n
x(i+1)=x0+i*h;
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2,y(i)+k1/2);
k3=h*f(x(i)+h/2,y(i)+k2/2);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(1/6)(k1=+2(k2+k3)+k4);
fprintf('the required value of y(%.2f) is
%.4f\n',x(i+1),y(i+1));
end

Output:

Enter your x and y function: @(x,y) y-x

Enter your interval length: 0.05

Enter initial value of x: 0

Enter initial value of y: 2

Enter final value of x: 0.1

the required value of y(0.05) is 2.1013

the required value of y(0.10) is 2.2052

You might also like