# Thread: Need help setting up this integral in MATLAB

1. ## Need help setting up this integral in MATLAB

I'm trying to code

$-\int_{-\pi}^0 \frac{1}{n}sin(nx)\ dx$ + $\int_0^{\pi} \frac{1}{n}sin(nx)\ dx$

in Matlab. I am using the following: - 1/n*integral(sin(n*x),x,-pi,0) + 1/n*integral(sin(n*x),x,0,pi)

But this is what Matlab thinks I'm putting in:

Why on earth is the first integral messed up but not the second?? And how do I fix the first one?

EDIT: Fixed it. I just needed to add parentheses--i.e., (- 1/n*integral(sin(n*x),x,-pi,0)) + (1/n*integral(sin(n*x),x,0,pi)). But why does this work and the original way doesn't?

2. ## Re: Need help setting up this integral in MATLAB

Hi,

You do not need a program to find it

primitve of (1/n)sin(nx) Is equal to: -(1/n^2)cos(nx)

then te second integral is: -(1/n^2)[cos(n(pi))-cos(0)]

= -1/n^2 if n is even

= 1/n^2 if n is odd

The first integral is eqal to the second one

3. ## Re: Need help setting up this integral in MATLAB

I know that; it's a rather easy integral to evaluate. But it's part of a larger function I'm trying to set up, and this is the part I'm messing up on.

4. ## Re: Need help setting up this integral in MATLAB

Try to put the fractions 1/n between ()

(1/n)*.....
(-1/n) *.....

OK thanks.