Computer Science 106: MATLAB HW 5, due 3/26 Please include your name in your homework 1. The US population figures from 1900 to 2000 (from Moler Chapter 5) t 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000 Pop (million) 75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212 226.505 249.633 281.422 a. Use the Matlab spine function to estimate the population in 2010. b. Use Matlab’s polyfit function to estimate the population in 2010 using a cubic polynomial. The actual census count for 2010 is 308,745,538 2. Solve the triangles problem from Project Euler Go to projecteuler.net and look at problem #102. A thousand triangles are scattered randomly near the origin of the plane. How many of them have the origin embedded inside? I have saved the triangles file in Ella under Resources as triangles.txt with spaces rather than commas as separators. It is very easy to load a text file of numbers (delimited by spaces) into Matlab. The file triangles.txt is stored, in the resources area of Ella, as a table 6 numbers per line (coordinates of a,b,c of triangle) separated by spaces. The way to load this file is to type: load triangles.txt and the numbers will be stored in an N x 6 table called triangles. More complex data is better stored in .mat files, so see help load for information on that. To solve this problem, look at each side of the triangle and treat it as a vector. Given two points A (Ax, Ay) and B (Bx,By) on a plane, how do you find the vector from A to B and what is the z component value? Use the cross-product to determine if the origin is to the right or left of each side of the triangle. You will need the vector from each vertex to 0 for this. If all of the cross-products are of the same sign, the origin is in the triangle. (You will need to go consistently in one direction or the other around the triangle.) The diagram below is incorrect: a,b,c are the vertices, not the sides You are to solve the problem by writing functions that 1. compute the cross-product of two vectors. (there is a Matlab function) 2. returns the Z-component of the cross product of two vectors 3. uses the Z- components of the three cross products of the three sides and the origin to determine if the origin is inside the triangle. Write these functions and test on a triangle with the origin inside and one with the origin outside, returning true or false. Show your test results (incorporate them as a few lines of comments in your last function. Then write a script to read in the triangles file and use the three functions to determine how many triangles contain the origin. Submit the code for the functions and the script and also the answer. For extra credit, try vectorizing some of the whole process. To do this you might use these statements: a = triangles(:, 1:2); b = triangles(:, 3:4); c = triangles(:, 5:6); which will read an entire column of number pairs into the variable a (and b and c) where a will be 1000x2 in size. 3. Learn about strings to decide on palindromes Write a script that asks for a string and then checks to see if it is a palindrome, a sentence or phrase in which the letters read the same forward or backwards. Some examples of palindromes: Madam I’m Adam (Adams introduction to Eve) Able was I ere I saw Elba (Napoleon’s lament on being exiled) To ask for input: pal = input(‘Type a string: ’); and type the response in single quotes like ‘Help me!’ If you have a single quote IN a string, type it twice: ‘Madam I’’m Adam’ To check for a palindrome, strip out all spaces and punctuation and test the resulting string. Takes all of six lines, perhaps fewer if you nest operations. Look at help for these functions: strcmp fliplr isletter upper logical vectors This can all be done with vector commands, no loops needed The basic idea is this: Use the isletter operation to create a logical vector with a 1 wherever the string contains a letter and a zero elsewhere. Use the ability of Matlab to use a logical vector to create a set of subscripts to extract only the letters into a new string. Use upper to convert all to uppercase. Use fliplr and strcmp to compare the forward and reverse strings to see if they are equal. See https://www.mtholyoke.edu/courses/pdobosh/cs106/features/logicalvectors.html for how to use the logical vectors