Command Sequence Controls

Debugging

 

Exercise:

> Complete the exercises in the script M-file for this week. Run each cell separately and follow the instructions, making sure that you also add in the required code marked by starred lines.

wink Tip: If you are having trouble finding an error, try commenting out a few lines to determine the line that is causing i. If you comment out a line containing for, while or if-else, don’t forget to comment out the corresponding end statements.

> Fill in the correspondent commands doing what required within **....**.

%% Practical 4

% Enter your name, student ID and the date here

%---------------------------------------------------------

%% Exercise 1

% Use a for- loop to print out the square of integers from 1 up to %maxValue.

maxValue = 10;
for num = % ** increment num from 1 to maxValue **
    num % this line simply prints num
    square = num^2 % this line prints the square
end

> Use array operations from Practical 3 to solve the problem in Exercise 1.

%% Exercise 2

% Use a while- loop to print out the square of integers from 1 up to

% maxValue.

maxValue = 10;
num=1;
while % ** num is not bigger than maxValue **
   
num % this line simply prints num
    square = num^2 % this line prints the square
   
num=num+1;
end

%--------------------------------------------------------

%% Exercise 3

% Sam gets paid compound interest at a rate defined at 5% per annum.

% Calculate his resulting investment each year and after 10 years.

% Change the rate on the 8th year to 5.75%

 

rate = 0.05;
investment = 5000;
for year = % ** increment the year from 1 to 10 **
    if % ** year is 8 **
       rate = 0.0575
    end
    year % this line simply prints the year
    investment = (1+rate)* investment % compounding function
end

%---------------------------------------------------------

%% Exercise 4

% create a graph that draws a straight line from the point (0,0) to

% every other

% point of the set (1,0), (1,1), (1,2), (1,3), (1,4).

hold on %allows us to plot multiple lines on a graph
for y = %** increment y **
    plot([0,1],[0,y]); % plot x coordinate and y coordinate arrays
end

hold off

%---------------------------------------------------------

%% Exercise 5A

% Use an if-else statement to check if an integer is either a prime % or a square number.

% Make sure you change the value of the integer.

% Check the help file to find a function that checks if a number is

% prime.

% note that this function works on both arrays and scalars.

integer = 4;
if % ** check if integer is a prime **
    Prime = integer % => If condition is met
elseif % ** check if the number is square **
   
Square = integer % => Else condition is met
end

%--------------------------------------------------------------

%% Exercise 5B

% Add some lines of code in the correct place above to check if the

% number is divisible by 6 and assign it to variable FactorOf6.

%% Exercise 5C

% Modify the above code as follows:

% Use a for-loop to check if each integer from 1 to 100 is a prime, % square or divisible by 6.

% Collect the integers that meet the above conditions to the % correspondent arrays Prime, Square or FactorOf6.

% Create count variables pCount, sCount and fCount which are updated % each time you find a new Prime, Square or FactorOf6 element and use % these counters to add new elements to your arrays.

% Print Prime, Square & FactorOf6 after your for- loop is finished.