# Integration Revision

Let’s plot a curve as a a series of bars and a line.

x1=[1:10]'; y1=2*x1; figure(1); bar(x1,y1); hold('on'); plot(x1,y1,'LineWidth',3); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right';

We seen before that integration is the sum of the area under the curve (the sum of the bars). We can plot the area under the curve with respect to x1. The first integrated y2 value is going to be the sum of all the bars at x1(1) i.e. x1(1) and this is going to correspond to a new value of x2=1.5 because we are shifted over by 0.5, the mid point of the step size in x1. The next value is going to be the sum of the bars at x1=2 i.e. x1(1)+x(2) and correspond to a new value of x2=2.5 and so on and so forth.

y2=[sum(y1(1));... sum(y1(1:2));... sum(y1(1:3));... sum(y1(1:4));... sum(y1(1:5));... sum(y1(1:6));... sum(y1(1:7));... sum(y1(1:8));... sum(y1(1:9));... sum(y1(1:10))]; x2=[[1:10]+0.5]'; figure(2); scatter(x1,y1,100,'fill','r'); hold('on'); scatter(x2,y2,100,'fill','b'); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; legend('y1=2*x1','y2=int(y1,x1)','location','northwest');

We can plot x1, y1 and x2, y2 as a scatter plot. Noting that y1=x.^2 and y2=2*x and the plots are correct.

y2=[sum(y1(1));... sum(y1(1:2));... sum(y1(1:3));... sum(y1(1:4));... sum(y1(1:5));... sum(y1(1:6));... sum(y1(1:7));... sum(y1(1:8));... sum(y1(1:9));... sum(y1(1:10))];

We can use

y2=cumsum(y1);

# Differenciation

What we are now going to do is look at the plot y3=1 i.e. a constant with respect to x3

x3=[1:10]'; [m3,n3]=size(x3); y3=ones(m3,n3); figure(3); scatter(x3,y3,100,'fill','r'); hold('on'); scatter(x4,y4,100,'fill','b'); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; legend('y1=1','2=int(y1,x1)','location','northwest');

Now instead of looking at the area under the line, we are going to look at the difference between the nearest y3 values i.e. the difference between a bar with the bar on its left. For instance the tenth bar at x3=10 and the 9th bar at x3=9 is going to correspond to a new value of x4 that is 0.5 less than 9 i.e. x4=8.5. The next value is going to be the difference of the points at x3=9 and x3=8 and correspond to a new value of x4 that is 0.5 less than  i.e. x4=7.5 and so on and so forth.

We will plot this as a scatter plot on the same chart.

y4=[[y3(1)];... [y3(2)-y3(1)];... [y3(3)-y3(2)];... [y3(4)-y3(3)];... [y3(5)-y3(4)];... [y3(6)-y3(5)];... [y3(7)-y3(6)];... [y3(8)-y3(7)];... [y3(9)-y3(8)];... [y3(10)-y3(9)]]; x4=[[1:10]-0.5]'; figure(4); scatter(x3,y3,100,'fill','r'); hold('on'); scatter(x4,y4,100,'fill','b'); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; legend('y3=1','y4=diff(y3,x3)','location','NorthWest');

In this case since y3 is a constant, the difference in the y3 values y4 is zero across the board as expected.

y4=[[y3(1)];... [y3(2)-y3(1)];... [y3(3)-y3(2)];... [y3(4)-y3(3)];... [y3(5)-y3(4)];... [y3(6)-y3(5)];... [y3(7)-y3(6)];... [y3(8)-y3(7)];... [y3(9)-y3(8)];... [y3(10)-y3(9)]];

We can use

y4=[y3(1);diff(y3)]; x4=[[1:10]-0.5]';

A value for x3=0 was not selected and is the reason for y4(0)=1 opposed to its actual value of 0. As this is an outlier it is better to ignore this first value and instead look at:

y4=[diff(y3)]; x4=[[2:10]-0.5]';

Let’s instead change the y3=1 (constant) to y3=x3. Now the bar chart from right to left looks like a set of stairs and as we climb down the step size remains constant:

As a result y4 is a straight line and is independent of x4.

Okay so let’s now change y3=x3.^2. Here the bar chart from right to left stepping down we see the first step is the larges, the the second step, then the third and it is a much shallower step down on the left hand side:

Actually we can see that every value of y4 is equal to 2*x4 i.e. y4=2*x4

# Integration vs Differentiation

The integration of y1=2*x1 with respect to x1 gives y2=x2^3 and the differentiation of y3=x3^3 with respect to x3 gives y4=2*x4 i.e. as you can clearly see they are the inverse process of one another.

We can think of integration as adding the sum of all the bars as we move from left to right – climbing the stairs i.e. starting at the origin and our first plot point being 0+(step size/2).

And we can think of differentiation as being the difference in the bar height as we step down from the right to left. The first step from the right being end-(step size)/2.

Of course as we seen with integration the above is only an approximation as we used finite relatively large step sizes.

# Uncertainty in Results

In the above we had the bin width equaling to the finite bin width of 1. For integration and differentiation we use an infinitely small bin width. Let’s constrict our bin width

x1step=0.0001 [m1,n1]=size(x1); x1=[1:x1step:10]'; y1=2*x1; figure(1); bar(x1,y1); hold('on'); plot(x1,y1,'LineWidth',3); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; 

y2=[x1step]*cumsum(y1); y2=y2(2:end); x2=[x1step]*[[2:m1]+[x1step/2]]'; figure(2); scatter(x,y,100,'fill','r'); hold('on'); scatter(x2,y2,100,'fill','b'); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; legend('y1=2*x1','2=int(y1,x1)','location','northwest');

x3step=0.0001 x3=[1:x3step:10]'; [m3,n3]=size(x3); y3=x3.^2; figure(3); scatter(x3,y3,100,'fill','r'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca ax.YAxisLocation='right';

y4=[diff(y3)]./x3step x4=x3step*[[2:m3]-[x3step/2]]'; figure(4); scatter(x3,y3,100,'fill','r'); hold('on'); scatter(x4,y4,100,'fill','b'); hold('off'); grid('minor'); xlabel('x'); ylabel('y'); ax=gca; ax.YAxisLocation='right'; legend('y3=x3^2','y4=diff(y3,x3)','location','NorthWest');

# Rules of Integration and Differenciation

Taking

$\displaystyle y={{x}^{n}}$

For integration we elevate the original power by 1 and then divide by the new power.

$\displaystyle \text{int}\left( y \right)=\frac{{{{x}^{{\left( {n+1} \right)}}}}}{{\left( {n+1} \right)}}$

For differentiation we multiply by the original power and then devalue the power by 1:

$\displaystyle \text{diff}(y)=n{{x}^{{\left( {n-1} \right)}}}$

# Symbolic

We can set x to be a symbol and write down our starting equations with respect to x:

syms('x') z1=1 z2=x z3=x^2 z4=x^3

$\displaystyle z1=1$

$\displaystyle z2=x$

$\displaystyle z3={{x}^{2}}$

$\displaystyle z4={{x}^{3}}$

Now we can differentiate the functions with respect to x:

dz1=diff(z1,x) dz2=diff(z2,x) dz3=diff(z3,x) dz4=diff(z4,x)

$\displaystyle \text{dz1}=0$

$\displaystyle \text{dz2}=1$

$\displaystyle \text{dz3}=2x$

$\displaystyle \text{dz4}=3{{x}^{2}}$

Now we can integrate the functions with respect to x:

iz1=int(z1,x) iz2=int(z2,x) iz3=int(z3,x) iz4=int(z4,x)

$\displaystyle \text{iz1}=x$

$\displaystyle \text{iz2}=\frac{{{{x}^{2}}}}{2}$

$\displaystyle \text{iz3}=\frac{{{{x}^{3}}}}{3}$

$\displaystyle \text{iz4}=\frac{{{{x}^{4}}}}{4}$