Should i try doing a switch within a switch for this question?
Show older comments
Let L = bank loan at annual interest rate k
If compound interest is charged student needs to pay bank debt after t years which equals D = Lexp(kt) and if simple interest is charged then D = L(1 + kt).
If we call a function 'Loan' which compares the loan offers from three banks: bank A, bank B and bank C. The function should take three input variables: the original amount of the loan L, the period (total number of years t) of the loan and B which takes into account which bank is being considered, and returns two varibles: the total amount of debt D due at the end of the loan period and the total amount of interests I paid during this period.
Which i think is this: function [D,I]= Loan(L,t,B)
For Bank A: charges the compound interest and the interest rate is determined depending on the amount of loan borrowed as following: • 3% if the loan L is less than £7,000, • 2.8% if the loan L is more than or equal to £7,000, but less than £15,000 • 2.5% for loans greater than or equal to £15,000.
For Bank B: also charges the compound interest. It offers a low interest rate of 2.2% for the first two years and then, starting from the third year, increases the interest rate by 0.25% each year, i.e. the interest rate for the third year is 2.45% and for the fourth year is 2.7%, and so on.
Bank C charges a fixed interest rate of 2.ab% where ab are the last two digits of your student ID number, but uses a different way of calculating the total amount of debt. For the first three years the debt is computed using the simple interest method. But starting from the fourth year, the compound interest is charged and the total amount of debt is then calculated by replacing the original loan L in Eq. (1) with the amount of debt owed after the first three years.
I've been trying to figure this out for hours, the only thing i could come up with is to do a switch within a switch:
function [D,I]= Loan(L,t,'B')
switch 'B'
case {'A'}
switch L
case <7000
[D=Lexp(kt), I=0.03]
Is this completely wrong?
Please help!
Answers (1)
Walter Roberson
on 10 Dec 2018
switch true
case L < 7000
D=Lexp(kt); I=0.03;
end
Clearly this is not very nice. It is much more likely that people would use if/elseif
if L < 7000
D=Lexp(kt); I=0.03;
end
2 Comments
NOOR YOUNES
on 10 Dec 2018
Walter Roberson
on 12 Dec 2018
Edited: Walter Roberson
on 12 Dec 2018
switch 'B'
That can never have any value other than 'B' so
case 'A'
for it will never succeed.
Perhaps you meant something like
switch B
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!