1. ## Please correct the program

Can somebody please correct my MATLAB program. I am new to matlab and I am taking a course (not matlab) where it requires some matlab.

the code below should return answer of integral...

Code:
% MATLAB Codes for GAUSS QUADRATURE
% Shakil Ahmed
clear all
p=4; %Degree of Polynomial
n=(p+1)/2; %To find number of Gauss points, ngp
ngp=round(n)
b=4; %Upper Limit
a=0; %Lower Limit
J=(b-a)/2; %Jacobian
% get gauss points in the parent element domain [-1, 1] and the corresponding weights
% function [w,gp] = gauss(ngp)
if (ngp == 1)
gp= 0;
w= 2;
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
I=[I J*w*f]
A=sum(I)
if (ngp == 2)
gp = [-0.57735027, 0.57735027];
w = [1, 1];
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
I=[I J*w*f]
A=sum(I)
if (ngp == 3)
gp = [-0.7745966692, 0.7745966692, 0.0];
w = [0.5555555556, 0.5555555556, 0.8888888889];
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
I=[I J*w*f]
A=sum(I)
end
end
end

2. Originally Posted by shakil
Can somebody please correct my MATLAB program. I am new to matlab and I am taking a course (not matlab) where it requires some matlab.

the code below should return answer of integral...

Code:
% MATLAB Codes for GAUSS QUADRATURE
% Shakil Ahmed
clear all
p=4; %Degree of Polynomial
n=(p+1)/2; %To find number of Gauss points, ngp
ngp=round(n)
b=4; %Upper Limit
a=0; %Lower Limit
J=(b-a)/2; %Jacobian
% get gauss points in the parent element domain [-1, 1] and the corresponding weights
% function [w,gp] = gauss(ngp)
if (ngp == 1)
gp= 0;
w= 2;
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
I=[I J*w*f]
A=sum(I)
if (ngp == 2)
gp = [-0.57735027, 0.57735027];
w = [1, 1];
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
I=[I J*w*f]
A=sum(I)
if (ngp == 3)
gp = [-0.7745966692, 0.7745966692, 0.0];
w = [0.5555555556, 0.5555555556, 0.8888888889];
for nn=1:ngp
x=((a+b)/2)+(gp(nn)*((b-a)/2))
%f(nn)=(x^2+1); %Problem 4.6(a)
f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
end