I wrote a code in matlab to compute an integral using Gauss-Chebyshev quadrature , but it doesn’t work:
function int = chebquad('-1i*exp(x+3)',1e-8,-1,1);
f=inline('-1i*exp(x+3)','x')
old_int = inf;
for n=1:1000
x = cos(((2*(1:n) - 1)/(2*n))*pi);
w = pi/n;
fx = f(x);
int = sum(w.*fx);
if abs(int_old-int) < tol
break
end
old_int = int;
end
Any suggestions?
Thanks!!
for future reference, it would help us out that you didn’t display your function with the specified variable as constants:
so show:
and not:
I don’t know why you have both -1 and 1, but I’m assuming tol = 1e-8. That being said, you made a small mistake in your code by using
int_oldinstead ofold_int.Edit: so at first I thought, you just displayed the function for “our benefit”, now I think you didn’t even define a Matlab function properly. Please read this and learn about basic Matlab coding. Chebyshev–Gauss quadrature is defined over
-1 to 1, and thus doesn’t need to be in a function code, below is the revised code:at the command prompt, you call this with: