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

Program

The document contains MATLAB code for numerically evaluating integrals using trapezoidal, modified trapezoidal, Simpson's, and Gaussian quadrature rules. The code defines functions to calculate the integral using each method, as well as calculating the first and second derivatives of the integrand function. It then provides an example of using the functions to evaluate the integral of the function f(x)=x^2*sin(x) from a to b, where the user provides the limits of integration and number of intervals.

Uploaded by

Sanket Shah
Copyright
© Attribution Non-Commercial (BY-NC)
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)
40 views2 pages

Program

The document contains MATLAB code for numerically evaluating integrals using trapezoidal, modified trapezoidal, Simpson's, and Gaussian quadrature rules. The code defines functions to calculate the integral using each method, as well as calculating the first and second derivatives of the integrand function. It then provides an example of using the functions to evaluate the integral of the function f(x)=x^2*sin(x) from a to b, where the user provides the limits of integration and number of intervals.

Uploaded by

Sanket Shah
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 2

22/9/12 8:28 PM

C:\Users\SANKET\Desktop\i123100004.m

1 of 2

function i123100004()
clc;
fprintf('enter the number of segments\n');
n=input(' ');
fprintf('enter the interval of integration\n');
a=input('\a=');
b=input('\b=');
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=x(i)*x(i)*(sin(x(i)));
end
fprintf('Solution by trapezoidal rule');
T=trapez(n,h,y);
T
fprintf('Solution by modified trapezoidal rule');
MT=modtrapez(n,h,x,T);
MT
fprintf('Solution by simpson rule');
S=simp(n,h,y);
S
fprintf('Solution by gauss quadrature');
G=gaussquad();
G
fprintf('derivatives');
derivative(n,h,x,y);
end
function [T]=trapez(n,h,y)
sum=0;
for i=2:n
sum=sum+y(i);
end
T=(h/2)*(y(1)+y(n+1)+2*sum);
end
function [MT]=modtrapez(n,h,x,T)
g(1)=x(1)*x(1)*cos(x(1))+2*x(1)*sin(x(1));
g(n+1)=x(n+1)*x(n+1)*cos(x(n+1))+2*x(n+1)*sin(x(n+1));
MT=T-((h*h/12)*(g(n+1)-g(1)));
integral
end

%calculate the value of f'(a)


%calculate the value of f'(b)
%calculate the value of the

function [S]=simp(n,h,y)
sumeven=0;
sumodd=0;
for i=2:+2:n
%calculates the sum of y at even nodes
sumeven=sumeven+y(i);
end
for i=3:+2:n-1
sumodd=sumodd+y(i);
%calculates the sum of y at odd(repeated) nodes
end
S=(h/3)*(y(1)+y(n+1)+2*sumodd+4*sumeven);
end

22/9/12 8:28 PM

C:\Users\SANKET\Desktop\i123100004.m

function [G]=gaussquad()
%x(i)=node points
%y(i)=value of the modified function(to make the interval -1 to 1) at
%the node points
%w(i)=weights
x(1)=0.8611363116;
y(1)=0.125*(x(1)+1)*(x(1)+1)*sin(0.5*(x(1)+1));
x(2)=-0.8611363116;
y(2)=0.125*(x(2)+1)*(x(2)+1)*sin(0.5*(x(2)+1));
x(3)=0.3399810436;
y(3)=0.125*(x(3)+1)*(x(3)+1)*sin(0.5*(x(3)+1));
x(4)=-0.3399810436;
y(4)=0.125*(x(4)+1)*(x(4)+1)*sin(0.5*(x(4)+1));
w(1)=0.3478548451;
w(2)=0.3478548451;
w(3)=0.6521451549;
w(4)=0.6521451549;
G=0;
for i=1:4
G=G+w(i)*y(i);
end
end
function derivative(n,h,x,y)
%calculate first order derivative
for i=2:n
xs1(i)=x(i);
fd1(i)=(y(i+1)-y(i))/h;
bd1(i)=(y(i)-y(i-1))/h;
cd1(i)=(y(i+1)-y(i-1))/(2*h);
end
%calculate second order derivative
for i=3:n-1
xs2(i)=x(i);
fd2(i)=(4*y(i+1)-3*y(i)-y(i+2))/(2*h);
bd2(i)=(3*y(i)-4*y(i-1)+y(i-2))/(2*h);
cd2(i)=(-y(i+2)+8*y(i+1)-8*y(i-1)+y(i-2))/(12*h);
end
%prints the derivatives in tabular format
first_order=[xs1;fd1;bd1;cd1]'
second_order=[xs2;fd2;bd2;cd2]'
end

2 of 2

You might also like