Program: assign03a.m Write a MATLAB script file that uses principal (or amount), rate of interest, and time to compute and display the simple interest with appropriate labeling. Create a vector of rates using the following information entered from user. Ask the user to enter the low and high values for the rate range and the number of rates. Ask the user for the number of years.

Respuesta :

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:

  1. Clear the workspace, command window and prepare the format as banking.
  2. Set the constant value of principal amount, this is fixed as 4000 in the code and can be changed.
  3. Ask the user to enter the value of lowest interest rate , highest interest rate , number of interest rate  values and number of years.
  4. Create the interest rate vector.
  5. For each entry of the interest rate vector, calculate the interest.
  6. Display the each entry of interest rate and the simple calculated interest.
  7. 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.

Ver imagen danialamin
Ver imagen danialamin
Ver imagen danialamin