# Need help setting up this integral in MATLAB

• Oct 6th 2012, 03:30 PM
phys251
Need help setting up this integral in MATLAB
I'm trying to code

$\displaystyle -\int_{-\pi}^0 \frac{1}{n}sin(nx)\ dx$ + $\displaystyle \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: http://www4a.wolframalpha.com/Calcul...=48&w=328&h=37

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?
• Oct 6th 2012, 04:05 PM
MathTutor2013
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
• Oct 6th 2012, 05:24 PM
phys251
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.
• Oct 6th 2012, 06:04 PM
MathTutor2013
Re: Need help setting up this integral in MATLAB
Try to put the fractions 1/n between ()

(1/n)*.....
(-1/n) *.....
• Oct 6th 2012, 07:20 PM
phys251
Re: Need help setting up this integral in MATLAB
OK thanks.