Algorithms and control structures: part b

This week we will continue to learn about conditional structures. Namely: while loops and nested conditional statements. The portfolio tasks this week will include both content from this week’s workshop and last week’s workshop.

While Loops

A while loop will operate the same as a for loop, except it will continue to loop while a certain condition is met, rather than having a set number of loop iterations. While loops don’t automatically have a counter like for loops, so you might need to code one as part of the process if required. While loops will iterate forever if the condition is always met, so it is important to have safeguards in place in case this issue arises. Ctrl + c will stop any script from running, and an if statement could be used to stop the script if the number of iterations exceeds a certain amount by using a break command.

Example 1: Square Number DIsplay

This code displays square numbers while they are below the input N.

N = 100; %input i = 1; %counter while i^2 < N disp(i^2) %output i = i + 1; %counter goes up by 1 each iteration end

Example 2: Square Number Display with Storage

This code stores square numbers while they are below the input N within a vector, then displays the final vector.

N = 100; %input i = 1; %counter while i^2 < N square(i) = i^2; i = i + 1; %counter goes up by 1 each iteration end disp(square) %output

Example 3: Bank Account

This code determines how long it will take for your bank account to reach $A, assuming it starts at a value of $B, which increases by 5% each year. An if statement was added to prevent an infinite while loop if the starting value is greater than or equal to the starting value.

A = 1000; %input

1

B = 500; %input if B >= A error(‘already have enough saved’) end i = 0; %counter while B < A B = 1.05*B; i = i + 1; %counter increases by 1 each iteration end disp(i) %output

Activities

For each of the following activities, write an algorithm for the process, then write some code that runs the algorithm.

1. A process that counts to a certain number, N.

% write your code here

2. A process that sums up all the even numbers until the sum surpasses N.

% write your code here

3. A process that stores all the powers of two that are less than N in a vector.

% write your code here

Nested control structures

It shouldn’t be a surprise that control structures can be combined. A nested control structure is when one control structure is included inside another. For example, an if statement can be placed inside a for loop, a for loop can be nested inside another for loop or a while loop.

Nested For Loops

Nested for loops occur when there is a for loop within a for loop. These can be useful when you have a repetitive task where you would like to vary multiple inputs simultaneously. The example below performs the operation A*x, where A is a matrix and x is a vector. It assumes MATLAB can perform scalar operations, but not matrix operations (which of course isn’t true, but it makes for a simple introduction to nested loops!).

A = [1 2;3 4;5 6]; b = [7;8]; [rows,columns] = size(A);

2

MatrixMultiply = zeros(rows,1); for i = 1:rows for j = 1:columns MatrixMultiply(i) = MatrixMultiply(i) + A(i,j)*b(j); end end disp(MatrixMultiply)

Activities

1. Write a for loop that finds the maximum number in a vector, that will work for a vector of any length. Hint: you will need an if statement somewhere.

% Insert code here

2. Write a for loop that finds the maximum number in a matrix of any size. Hint: you will need nested for loops and an if statement.

% Insert code here

3. Write an algorithm, and then some code that seaches a matrix for the first column that has a vector- magnitude greater than a value N. A test matrix is provided below. If there is no column that has a magnitude greater than N, the code should produce an error message.

A = [5,5,4;4,5,4;1,4,2] N = 5. %insert code here

Portfolio 3 part b

Submission instructions

1. Questions 1 and 3 of this portfolio should be submitted as a single pdf file titled n#######Portfolio3.pdf (including that from portfolio 3 part a). Question 2 must be submitted as a .m file titled n########Portfolio3b.m.

2. Clearly indicate what part of the portfolio you are answering. 3. The assignment is DUE at 11:59pm Monday 14th May and should be submitted through

Blackboard. The IT helpdesk closes at 10pm, and so if you leave your submission to the last minute and something happens then you may not be able to recieved any assistance. Late submissions will recieve a mark of zero.

Taylor series

3

The taylor series is a special series that approximates complicated functions as a polynomial. It is given by

where is the derivative of evaluated at a. To create the approximation, we truncate the series at some maximum term Nto give

The Taylor series is used in many scenarios, including when certain mathematical methods may not applicable (such as integration). Or for creating the finite difference approximations, as seen in the week 7 worksheet.

1) Write an algorithm for computing a taylor series to N term.

2) Write a MATLAB function that accepts three inputs (FUN, a, N), where FUN is an annonymous function, a is the point the taylor series is centered around and N is the order of the taylor series. The MATLAB function should output the Nth order Taylor series for the function about a. You are not permitted to use the taylor function in MATLAB to solve the problem.

3) Apply the function to solve the 10th order Taylor series of about

hint: if you choose to test your function, you can compare it to the output from wolframalpha here.

4