# Tutorial Video

# Finding the Area Under the Curve

Let’s visually inspect, how to calculate the area under the curve using a line plot and a bar plot.

## Constant Term

`x=[1:10]';`

[m,n]=size(x);

y=ones(m,n);

figure(1);

bar(x,y);

hold('on');

plot(x,y,'LineWidth',3);

hold('off');

grid('minor');

xlabel('x');

ylabel('y');

ax=gca

ax.YAxisLocation='right';

areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2

areaundercurve2=trapz(y)

`y=1`

The area under the curve is:

`x*y`

But

`y=1`

So the area is

`x`

You’ll see the area under the line is a rectangle and has a dependence on the variable `x`

i.e. the longer we make `x`

, the larger the area under the curve. `y`

has no dependence on `x`

and remains unchanged as `x`

increases.

As `x=10`

the area under the line is approximately `10`

Note that the red line covers the area of half the first and half last bar (so instead of taking the value for each of these bars we take half the value of these bars). This gives an area of 9 instead of 10. We can calculate the area by counting the sum of `y`

with these corrections i.e.:

`areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2`

`areaundercurve1=9`

`areaundercurve2=9`

As a practical example x may be the time in seconds and y may be your constant speed in m/s. As the time increases the distance you’ve walked in meters (s*m/s) increases.

## Linear Terms

`y=x`

Calculate the Area under the line:

Area assuming a square = `x.*y`

but `y=x`

Therefore area = `x.*x=x.^2`

For `x=10`

`x.^2=100`

However it is not a square, only half the square is taken therefore the area is:

`(1/2)*x.^2`

`(1/2)*x.^2=50`

In other words the power is raised by 1 (because y has a dependency on x) and divided by 2 because the area under the curve is a triangle and not a square. If we calculate this properly taking into account that we are summing up only half the last and first bars:

`areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2`

`areaundercurve1=49.5`

`areaundercurve2=49.5`

If it was a square it would be `x*x=10*10=100`

The results match closely as expected.

As a practical example x may be the time and with this exercise you decide to walk faster the further you go (y will therefore be proportional to x). As the time increases the distance you’ve walked in increases but as you are walking faster at each point in time, the distance you cover is much larger.

## Squared Terms

`y=x.^2`

Calculate the Area under the curve is:

`Area assuming a square = x.*y`

but `y=x.^2`

Therefore

`area = x.*x.^2=x.^3`

For `x=10`

`x.^3=1000`

However it is not a square and visually we can see that less than 1/2 of the square is under the curve.

`areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2`

`areaundercurve1=334.5000`

`areaundercurve2=334.5000`

`334.5000/1000=~1/3`

Meaning the area under the curve is:

`(1/3)*x.^3`

In other words the power is raised by 1 (because y has a dependency on x) and divided by 3.

newpower=oldpower+1 because going along the x axis gives an additional x term. We need to normalise by the newpower because the curve we get is not square.

## Cubic Terms

`y=x.^3`

Calculate the Area under the curve:

`Area assuming a square = x.*y`

but `y=x.^3`

Therefore

`area = x.*x.^3=x.^4`

For `x=10`

`x.^4=10000`

However it is not a square:

`areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2`

`areaundercurve1=2.5245e+03`

`areaundercurve2=2.5245e+03`

`2524.5/10000=~1/4`

Meaning the area under the curve is:

`(1/4)*x.^4`

In other words the power is raised by 1 (because y has a dependency on x) and divided by 4.

newpower=oldpower+1 because going along the x axis gives an additional x term. We need to normalise by the newpower because the curve we get is not square.

## Multiplication by a Scalar

`y=2*x.^2`

Calculate the Area under the curve:

`Area assuming a square = x.*y`

but `y=2*x.^2`

Therefore

`area = x.*(2*x.^2)=2*x.^3`

For `x=10`

`2*x.^3=2000`

Calculating the area under the curve:

`areaundercurve1=sum(y(2:end-1))+y(1)/2+y(end)/2`

`areaundercurve1=669`

`areaundercurve2=669`

`669/2000=(2*334.5000)/(2*1000)~1/3`

Meaning the area under the curve is:

`(2)*(1/3)*x.^3`

In other words the scalar acts as a constant. The power is still raised by 1 (because y has a dependency on x) and divided by 3.

newpower=oldpower+1 because going along the x axis gives an additional x term. We again need to normalise by the newpower as normal because the curve we get is not square.

## Two Terms

`y23=x.^2+x.^3`

We can see that there is no interdependence on having the two terms `y23=x.^2+x.^3`

and we can treat both terms separately.

Therefore for finding each term for each term

newpower=oldpower+1 because going along the x axis gives an additional x term.

We again need to normalise by dividing by newpower.

These are the rules of integration.

# Symbolic Integration

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

`syms('x')`

y1=1

y2=x

y3=x^2

y4=x^3

Now we can integrate the functions with respect to x:

`inty1=int(y1,x)`

inty2=int(y2,x)

inty3=int(y3,x)

inty4=int(y4,x)

If we wish to calculate the values between 1 and 10 we can use:

`inty1_1_10=int(y1,x,1,10)`

inty2_1_10=int(y2,x,1,10)

inty3_1_10=int(y3,x,1,10)

inty4_1_10=int(y4,x,1,10)

To get rid of fractions and display as a double we can use:

`inty1_1_10=double(int(y1,x,1,10))`

inty2_1_10=double(int(y2,x,1,10))

inty3_1_10=double(int(y3,x,1,10))

inty4_1_10=double(int(y4,x,1,10))

`inty1_1_10=double(int(y1,x,1,10))`

inty2_1_10=double(int(y2,x,1,10))

inty3_1_10=double(int(y3,x,1,10))

inty4_1_10=double(int(y4,x,1,10))

`inty1_1_10=9`

inty2_1_10=49.5

inty3_1_10=333

inty4_1_10=2499.8

`areaundercurve_y1=9`

areaundercurve1_y2=49.5

areaundercurve1_y3=334.5000

areaundercurve1_y4=2524.5

# Uncertainty in Results

The first two values match precisely but there is a deviation in the last 2 values. Why is this? If we visually inspect `y=x.^2`

and look at the bars, we can see that at some points the bar is above the curve and other points the bar is below the curve. In an ideal scenario, this should never happen and these are the source of our error.

As we only have 10 bars… our error is relatively large. If we modify our code to give 20 bars (steps of 0.5 opposed to 1) the error should decrease:

`steps=0.5`

x=1:steps:10';

y=x.^2;

figure(1);

bar(x,y);

hold on;

plot(x,y,'LineWidth',3);

hold off;

grid('minor');

xlabel('x');

ylabel('y');

ax=gca

ax.YAxisLocation='right';

areaundercurve1=steps*(sum(y(2:end-1))+y(1)/2+y(end)/2)

areaundercurve2=steps*trapz(y)

`inty3_1_10=333`

`areaundercurve1_y3_10bars=334.500`

`areaundercurve1_y3_20bars=333.375`

As expected the value is closer:

If we modify our code to give 100 bars (steps of 0.1):

`inty3_1_10=333`

`areaundercurve1_y3_10bars=334.500`

`areaundercurve1_y3_20bars=333.375`

`areaundercurve1_y3_100bars=333.150`

If we modify our code to give 1000 bars (steps of 0.01):

`inty3_1_10=333`

`areaundercurve1_y3_10bars=334.500`

`areaundercurve1_y3_20bars=333.375`

`areaundercurve1_y3_100bars=333.150`

`areaundercurve1_y3_1000bars=333.0002`

`inty3_1_10=333`

`areaundercurve1_y3_10bars=334.500`

`areaundercurve1_y3_20bars=333.375`

`areaundercurve1_y3_100bars=333.150`

`areaundercurve1_y3_1000bars=333.0002`

`areaundercurve1_y3_10000bars=333.0000`

Integration is this procedure and makes the bars “infinitely small”. Of course a computer has its limitations in terms of precision but on a practical basis this precision suffices. The reader is encouraged to continue to repeat the procedure above using y4.