MATLAB Answers

0

Trying to integrate sinx using the integrate function but when I plot the integral, instead of plotting from -1 to 1 it plots from 0 to 2. Does anyone know why this happens?

Asked by Guillermo Naranjo on 3 May 2019
Latest activity Commented on by Guillermo Naranjo on 6 May 2019
I am using the "integrate" function to plot the integral of sin(x) [which is -cos(x)]. When I plot the integral it should plot from -1 to 1 but instead it is shifting the integral from 0 to 2. In the code below I show what I did. The @myFunInt is the following
function fval = myFunInt(x)
fval = sin(x);
end
and the main code is
clear
a=0; % lower limit
b=30; % upper limit
n=1000; % subintervals
h = (b-a)/n; % Spacing
x = 0:30;
int = zeros(1,n+1);
for j = 0:n
x_j=a+j*h; % x values are being allocated in the empty array
x(:,j+1)=x_j;
fun = @myFunInt;
y=integral(fun,a,x(:,j+1));
int(:,j+1)=y;
end
plot(x,int);
Note that this code works with any other function exept than integrading cos(x) and -sin(x).
Thank you for your input.

  0 Comments

Sign in to comment.

2 Answers

Answer by John D'Errico
on 3 May 2019
Edited by John D'Errico
on 3 May 2019
 Accepted Answer

Actually, the integral of sin(x) is NOT -cos(x). This is a mistake that people frequently make. The integral is -cos(x), PLUS a constant of integration. That is, the indefiinite integral:
int(sin(x),x) == -cos(x) + C
Where C can be any constant number. Why is this important? Suppose you think of this as a differential equation:
df/dx = sin(x)
Now, you want to integrate that ordinary differential equation, over the interval [a,b] = [5,30]. Again, we will find the general solution as
f(x) = -cos(x) + C
However, then when you solve an ODE, you consider the initial conditions to resolve the constant. What is f(a)? In your code, you implicitly defined f(a) to be ZERO. So, implicitly, you resolved the constant C as:
C = cos(5)
C =
0.283662185463226
Now, given ths plot you created, I'll overlay the function -cos(x)+C.
plot(x,int);
hold on
xint = linspace(5,30,100);
plot(xint,-cos(xint) + C,'ro')
As you can see, the curve you generated (in blue) and the red dots I plotted overlay perfectly.
You need to remember that constant of integration, or it will hurt you when you are not looking. As far as your code having worked for you on other problems, well you may have just gotten lucky. Everything would look perfectly ok as a result of your code, IF it turned out that the initial condition of f(a)=0 just happened to have been correct. And then you ran into this problem, where that initial condition turned out to have been not what you expected.

  1 Comment

Thank you very much John.
I was not taking into account the indefinite integral. I was just assuming since I had a limited range it would behave in a different manner. Thank you for your reply.
Best,
Guillermo

Sign in to comment.


Answer by Geoff Hayes
on 3 May 2019

Guillermo - I'm guessing it is the y-axis limits that you want to be between -1 and 1? Why is the minimum value, a, fixed but the maximum is variable? Why the interval [5,30]? Is the units for this interval supposed to be radians, degrees, or does it not matter?
Note that if I use a and b as
a=0; % lower limit
b=2*pi; % upper limit
Then the figure is drawn as
which has the interval [0,2] which is what you describe. Is this expected? I think so because when you integrate like
y=integral(fun,a,x(:,j+1));
then y is the area under the curve...so on each iteration of your loop, you are calculating the area from 0 to something a little larger (a function of j and h). When x is pi, then the area will be two (since area from 0 to pi/2 is one).

  5 Comments

However, if you try integrating cos(x) instead of sin(x), the limits in the y-axis will be from -1 to 1 as expected.
Correct but again you have calculated and are plotting areas under the curve, and for cos, these limits make sense given its curve.
Geoff,
Thank you again for your answer. You are correct, when I integrate from 0 to pi and then from pi to zero the total area under the curve is 0. So yes it depends on the interval I am calculating the area under the curve.
I thought of this which is why I included the term
int(:,j+1)=y;
What this does is for every sub interval
a=0; % lower limit
b=pi; % upper limit
n=1000; % subintervals
h = (b-a)/n; % Spacing
I will save the current area and plot that value which I understand would give me the integral of sin(x). Is this not right?
Geoff,
After reading Jonh's reply I realized I did not take into account the constant the integral of sin(x). This is what is causing my integral to shift.
Thanks again for everything.

Sign in to comment.