Have your vectors of weights be the same size as your vectors of locations, ordered so that corresponding weights and locations are in the same position:
a = -1;
b = 1;
n = [-1/sqrt(3) 1/sqrt(3)];
n1 = [-0.86113 -0.33998 0.33998 0.86113];
n2 = [-0.93246 -0.66120 -0.23861 0.23861 0.66120 0.993246];
w = [1 1];
w1 = [0.34785 0.625214 0.625214 0.34785];
w2 = [0.17132 0.36076 0.6791 0.6791 0.36076 0.17132];
f = @(x) (1)./(x.^2 .* (sqrt(x.^2 +1)));
gaussleg = sum(w.*f(n));
gaussleg1 = sum(w1.*f(n1));
gaussleg2 = sum(w2.*f(n2));
Higher precision in your weights and locations will give more accurate results.
(edit) I also changed f slightly so that you could pass in the entire vector of locations at once.
Best Answer