Im trying to use 4 point gauss quadrature method and this is my code and getting errors any help T^T?

18 views (last 30 days)
Im trying to use 4 point gauss quadrature method and this is my code and getting errors any help T^T?
when i run it im getting statement imcomplete error at line 17
sum=sum+eval(func) * wt(i)
format long
func='(exp(x)*sin(x)/(1+x^2)';
xa=0.0;
xb=3.0;
ngp = 4;
xi(1) = -0.8611363116; wt(1) = 0.3478548451;
xi(2) = -0.3399810436; wt(2) = 0.6521451549;
xi(3) = -xi(1); wt(3) = wt(1);
xi(4) = -xi(2); wt(4) = wt(2);
coeff = (xb-xa)/2.0;
sum = 0.0;
for i=1:ngp
x = (xb+xa)/2.0 + (xb-xa)/2.0 *xi(i);
sum = sum + eval(func)*wt(i);
end
sum = sum * coeff;
[sum]
  4 Comments
David Wilson
David Wilson on 22 Apr 2019
Your code seems reasonable,
>> f = @(x) exp(x).*sin(x)./(1+x.^2);
>> integral(f,0,3)
ans =
2.8816
Although you might want to double check your integrand since there is more than one way to insert a bracket.
Can't explain the TI response though.

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!