# Calculate Simple Interest and Compound Interest – easy exercises with Matlab

We’ll see how to calculate the simple interest and the compound one in this article. We have to start with some definitions, though.

 Interest is money paid by an individual or organization for the use of a sum of money called the principal. The interest is usually paid at the end of specified equal periods of time (such as monthly, quarterly, or annually). The sum of the principal and the interest is called the amount.

### 1.- Simple Interest

To help us work and calculate the simple interest, we have these two easy formulas:

I = P r t
A = P + P r t

where
I = simple interest
P = principal
r = interest rate per year
t = time in years
A = amount

We can also conclude that
A = P(1 + r t) and
I = A - P

Examples

If an individual borrows \$1000 at 5% per year for 1.5 years, how much interest must be paid on the loan?

I = P r t
I = 1000 (0.05) (1.5)
I = \$75

If an organization invests \$13000 at 4% per year for 3 years, how much will the investment be worth at the end of the 3 years?

A = P + Prt
A = \$13000 + \$13000 (0.04) (3)
A = \$14560

### 2.- Compound Interest

Compound interest means that the interest is paid periodically over the term of the loan which results in a new principal at the end of each interval of time.

The ending balance is given by:

where
A = amount, or ending balance
P = principal
r = annual interest rate
n = compounded times per year
t = number of years

The following video shows an example and a solution using a calculator specially prepared for such purpose. After the video, we show how to solve the problem by creating in Matlab our own function for the task...

Let's create our code! - Example

Find the amount of an investment if \$10,000 is invested at 5% compounded monthly for three years.

Fortunately, we can create a function in Matlab for the compound interest formula, like this:

function A = comp_int(P, r, n, t)
A = P*(1 + r/n)^(n*t);

and we can call it from another m-file, script, or from the command window, in this way:

P = 10000;
r = 0.05;
n = 12;
t = 3;

format bank
A = comp_int(P, r, n, t)

A = 11614.72

### 3.- Continuously Compounded Interest

When the interest is compounded more frequently, we get to a situation of continuously compounded interest. This formula works it out:

A = Pert

where
A = amount, or ending balance
P = principal
e = 2.718281...
t = number of years
r = annual interest rate

Example

Find the amount of an investment if \$10000 is invested at 5% compounded continuously for three years.

Fortunately again, we can create another function to calculate the formula above, like this:

function A = comp_int_2(P, r, t)
A = P * exp(r*t);

and we can call it from another script or from the command window, in this way:

P = 10000
r = 0.05
t = 3
A = comp_int_2(P, r, t)