8.9. Exercise Solutions#

Solution to Exercise 8.1

>> 2 - (3 + 6)

ans =

    -7

>> 2 * (5 - 8 * (3 + 6))

ans =

  -134

>> 2 * (2 - 2 * (3 - 6 + 5 * (4 - 7)))

ans =

    76

>> (2 * (5 - 4 * (3 + 8))) / (3 * (4 - (3 - 5)))

ans =

   -4.3333

>> 2 * 4 ^ 5 / (81 - 5 ^ 2)

ans =

   36.5714

>> mod(14151, 571)

ans =

   447

Solution to Exercise 8.2

>> centigrade = 37.8;
>> fahrenheit = 9 / 5 * centigrade + 32

fahrenheit =

  100.0400

>> centigrade = 100;
>> fahrenheit = 9 / 5 * centigrade + 32

fahrenheit =

   212

>> centigrade = - 40;
>> fahrenheit = 9 / 5 * centigrade + 32

fahrenheit =

   -40

Solution to Exercise 8.3

>> string1 = "Your mother was a hamster";
>> string2 = "and your father smelt of elderberries!";
>> lower(string1)

ans = 

    "your mother was a hamster"

>> upper(string2)

ans = 

    "AND YOUR FATHER SMELT OF ELDERBERRIES!"

>> replace(string2, "elderberries", "roses")

ans = 

    "and your father smelt of roses!"

>> string3 = string1 + " " + string2

string3 = 

    "Your mother was a hamster and your father smelt of elderberries!"

>> string3 = char(string3);
>> length(string3)

ans =

    64

>> string3(1:30)

ans =

    'Your mother was a hamster and '

Solution to Exercise 8.4

% 8. MATLAB Basics Exercises

clear % Clear all variables
clc   % Clear command window

% Exercise 8.4
fprintf("\nExercise 8.4\n------------")

fahrenheit = 100;
centigrade = 5 / 9 * (fahrenheit - 32);

fprintf("\n%0.2f in Fahrenheit is equivalent to %0.2f in centigrade\n", ...
    fahrenheit, centigrade)

Output

Exercise 8.4
------------
100 degrees Fahrenheit is equivalent to 37.78 in centigrade

0 degrees Fahrenheit is equivalent to -17.78 in centigrade

Solution to Exercise 8.5

% Exercise 8.5
fprintf("\nExercise 8.5\n------------")

% Enter the number of seconds
initial_seconds = 1000000000;

% Calculate conversion values
seconds = initial_seconds;
seconds_in_a_minute = 60;
seconds_in_an_hour = 60 * seconds_in_a_minute;
seconds_in_a_day = 24 * seconds_in_an_hour;
seconds_in_a_week = 7 * seconds_in_a_day;
seconds_in_a_year = 365 * seconds_in_a_day;

% Number of years in the seconds
years = floor(seconds / seconds_in_a_year);
seconds = seconds - years * seconds_in_a_year;

% Number of weeks in the seconds remaining
weeks = floor(seconds / seconds_in_a_week);
seconds = seconds - weeks * seconds_in_a_week;

% Number of days in the seconds remaining
days = floor(seconds / seconds_in_a_day);
seconds = seconds - days * seconds_in_a_day;

% Number of hours in the seconds remaining
hours = floor(seconds / seconds_in_an_hour);
seconds = seconds - hours * seconds_in_an_hour;

% Number of minutes in the seconds remaining
minutes = floor(seconds / seconds_in_a_minute);
seconds = seconds - minutes * seconds_in_a_minute;

% Print the result
fprintf("\nThe are %d years, %d weeks, %d days, %d hours, %d minutes" + ...
    " and %d seconds in %d seconds\n", years, weeks, days, hours, ...
    minutes, seconds, initial_seconds)

Output

Exercise 8.5
------------
The are 31 years, 37 weeks, 0 days, 1 hours, 46 minutes and 40 seconds in 1000000000 seconds

Solution to Exercise 8.6

% Exercise 8.6

% Details of the loan
value    = 200000;
years    = 20;
interest = 4;

% Calculate number of months and montly interest
months           = 12 * years;
monthly_interest = interest / 100 / 12;

% Calculate repayment
repayment = monthly_interest * value / (1 - (1 + monthly_interest) ^ (-months));

% Print loan details and repayment amount
fprintf("\nLoan repayment calculator\n-------------------------\n")
fprintf("Loan ammount         : £%0.2f\n", value)
fprintf("Loan duration        : %i\n", years)
fprintf("Annual interest rate : %0.2f%%\n\n", interest)
fprintf("Monthly repayment    : £%0.2f\n", repayment)

Output

Exercise 8.5
------------
The are 31 years, 37 weeks, 0 days, 1 hours, 46 minutes and 40 seconds in 1000000000 seconds

Loan repayment calculator
-------------------------
Loan ammount         : £200000.00
Loan duration        : 20
Annual interest rate : 4.00%

Monthly repayment    : £1211.96