# 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

[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)

The area under the curve is:

But

So the area is

You'll see the area under the line is a rectangle and has a dependence on the variable

i.e. the longer we make

, the larger the area under the curve.

has no dependence on

and remains unchanged as

increases.

As

the area under the line is approximately

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

with these corrections i.e.:

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

Calculate the Area under the line:

Area assuming a square =

but

Therefore area =

For

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

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:

If it was a square it would be

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

Calculate the Area under the curve is:

but

Therefore

For

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

Meaning the area under the curve is:

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

Calculate the Area under the curve:

but

Therefore

For

However it is not a square:

Meaning the area under the curve is:

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

Calculate the Area under the curve:

but

Therefore

For

Calculating the area under the curve:

Meaning the area under the curve is:

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

We can see that there is no interdependence on having the two terms

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:

y1=1

y2=x

y3=x^2

y4=x^3

Now we can integrate the functions with respect to 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:

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:

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))

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))

inty2_1_10=49.5

inty3_1_10=333

inty4_1_10=2499.8

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

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:

x=1:<span style="color: #ff0000;">steps</span>: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=<span style="color: #ff0000;">steps</span>*(sum(y(2:end-1))+y(1)/2+y(end)/2)

areaundercurve2=<span style="color: #ff0000;">steps</span>*trapz(y)

As expected the value is closer:

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

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

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.