Answer:
The code is given in the answer along with the algorithm and explanation. The output and the workspace variables are also attached as screen shots.
Step-by-step explanation:
The basic algorithm of the code is as follows:
- Clear the workspace, command window and prepare the format as banking.
- Set the constant value of principal amount, this is fixed as 4000 in the code and can be changed.
- Ask the user to enter the value of lowest interest rate , highest interest rate , number of interest rate values and number of years.
- Create the interest rate vector.
- For each entry of the interest rate vector, calculate the interest.
- Display the each entry of interest rate and the simple calculated interest.
- Calculate the average rate of interest and the average interest and display them.
The code is here as follows
%% Preparing the formats
clc,clear all;%Clearing command line and workspace
format bank%Formatting
format compact%Formatting
%% Setting the constants
PRINCIPAL = 4000;%Principal amount fixed as 4000.
%% Taking inputs from the user
low_val = input('Enter the low value for the rate range: ');%Lowest value of the interest
high_val = input('Enter the high value for the rate range: ');%Highest value of the interest
rate_vals = input('Enter the number of rate values including high and low values: '); %Total number of rate values
year = input('Enter number of years: ');% Number of years
%% Creating the interest vectors
rate = linspace(low_val,high_val,rate_vals);%Creating the vector
%% Initializing the array
simple_interest=zeros(length(rate),1);%initializing the array to hold the simple interest
%% Displaying the interest rate vectors
fprintf('\t\t Rate(%%)\t\tInterest($)\n');%Displaying the interest rate and interest heading
%% Calculating the interest vectors
for i = 1:length(rate)
simple_interest(i) = (rate(i)*year*PRINCIPAL)/100;%Calculating Simple rate of interest for a given rate over the principal amount
fprintf('\t\t %.2f\t\t\t%.2f\n',rate(i),simple_interest(i));% Displaying each entry interest rate and interest
end
%% Calculating and printing the average interest
fprintf('Average: Rate(%%)\t\tInterest($)\n');%Displaying the heading for average rate and interest
fprintf('\t\t %.2f\t\t\t%.2f\n',mean(rate),mean(simple_interest));%Calculating and displaying the average interest.