ENGR 17 A 5 Functions and Recursion Due: next Tuesday Create folder A5 and set your workspace to it. Do 1 – 4 for 10 pts, #5 for extra credit 1) Create a function file tension.m to compute tension as a function of D. Hardcode the values of Lb, Lc, and W, within the function. Vectorize the function. Then, on the command line, write statements to solve part a) finding the D that results in minimum tension using a vector of D values and your function. After solving a) on the command line, copy your correct statements from the history window into the bottom of the tension.m file. Also copy the answer for minimum T and the D that produces it. Then comment all these items out in your function file using Ctrl-R. For b) create the plot indicated from the command line. Save it as tensionPlot.jpg. After examining the plot, enter your answer for b) at the bottom of the tension.m file, also enclosed in comments. 2) Create a script file airplane.m, in which we will solve the following: Start by entering a set of comments at the top of your program, such as the following: % airplane.m A script to determine the distance between two airplanes % Your Name % Assignment 5 problem 2 Notice that both planes fly at right angles to each other and their distance can be determined using Pythagorean’s theorem applied to the distance each one is from the origin. A) To begin solving this problem, we can estimate the time of closest approach as being about 3 hours after 1 PM. So let’s create a vector of time values that covers that range: t = [1:0.1:4] ; Now, we must determine the X position relative to the origin of A versus time, we’ll call this vector XA. This plane (A) flies at 320 mph and at 1pm it is 800 miles W of the origin. Our job is to convert this information to the equation of a line. A table can help: time 1 2 3 4 position -800 -480 -160 160 Now, on a piece of paper, derive the equation of the line that satisfies these points. Then fill in the blanks to create the distA vector using your formula: XA = ____ * t – ________ ; Repeat this calculation to determine YB; Now we can create a formula for distance using the Pythagorean theorem: dist = sqrt ( ____________ ); Find the minum of dist and the time at which that occurs. Then plot distance for only the time from 1 up to the closest approach. Save this plot as airplanePlot.jpg B) [continue adding to airplane.m] To solve this problem we can model the aircraft position with a polynomial. Your answer for XA probably involved an expression such as 320 t – 1120. Instead of a vector of position values like we created for part A), we’ll create a much simpler and smaller vector representing this expression as a polynomial. In other words, pXA = [320 - 1120] This is a matlab vector representing the airplanes position (as a function of time, which is assumed but not shown in the vector) in a much more compact form than a vector of 40 numbers we created in part A. Make a similar polynomial vector for pYB. We can compute a polynomial vector for distance squared by squaring each polynomial using the conv function, for example to square the pXA polynomial we could write: conv(pXA,pXA) and this would give us a polynomial representing the distance from the origin squared for airplane A. With this in mind, create a polynomial distSq that is the sum of the square of the distance of both planes from the origin (in other words, hypoteneuse squared). Still in polynomial form. If we thought the planes were going to crash we could now just type roots( distSq) and it would tell us what t value gives us a distance squared of zero. But the problem asks when the planes come within 30 miles of each other. In other words, we want distSq = 900. How could we modify distSq so we could use roots to find the times we are looking for? It’s worth printing out the polynomial vector distSq to make sense of what is going on here. For example, suppose distSq has the numbers: [12800 -899200 1579300] This would represent the polynomial distanceSq = 12800 t2 -899200 t + 1579300 We would like to find the times t when this polynomial evaluates to 900. If we set the polynomial = 900 then we can see if we subtract 900 from both sides (in effect the last term of the polynomial, the 1579300 value) we can then take the roots of the result. So we have to subtract 900 from the 3rd cell of the distSq vector before doing the roots command. Do that now. When you get the answer confirm that it matches about where it should be on the plot from part a), then copy it in to your script and comment it out. 3) Create a function file gcd.m in which we will compute the Greatest Common Divisor of 2 numbers using the following recursive definition (The Euclidean Algorithm): Matlab has a rem function which works just like the mod function we’ve used already (it calculates the remainder of a division operation), making this a very simple problem to solve. All we need to do is set up the function and put an if statement in it. Think about what to call the return value...perhaps d ? Write the first line of the function (the header) and add a comment describing the function and what it needs as input. The if statement will just check if y is 0 and if so, assign the value of x to d. Otherwise assign d the gcd of the expression shown. Test your function with a few values. Does it matter if we type gcd(60,24) or gcd(24, 60) ? If so, can you make the function work the same in either case? 4) A palindrome is a number or word that reads the same forwards or backwards, such as 236545632 and madamImadam In this problem you will write a recursive function isPalindrome that takes a vector of digits and determines whether it is a palindrome. If it is a palindrome it returns the value 1, if it is not, it returns 0. For example isPalindrome( [ 1,3,4,4,3,1]) will return 1 while isPalindrome([1,2,3,4,5]) will return 0. A recursive approach to this problem is to examine the beginning and ending cells in the vector you are given. For example if v(1) and v(end) are not the same, then we know this isn’t a palindrome so we can set result to 0. However, if they are the same, then we can set result = isPalindrome of the vector made from stripping the end points from our original vector. Another possibility is that we have a vector of one cell (always a Palindrome!!) or a vector of two cells (no further recursive calls needed). These should both be handled separately in your program. You can check the lengh of a vector with the command length(v) When you have a solution, try it out on a few vectors to see if it works properly and debug if necessary. Here are some test cases: isPalindrome([1]) isPalindrome([1 2]) isPalindrome([2 2]) isPalindrome([1 2 3]) isPalindrome([1 2 1]) You may want to put these statements in a script (along with a few more) to speed testing When finished, zip your folder up and turn it in to the uploader. Make sure your name is in each file. 5. CHALLENGE: Run the stampCount program from the website, which calculates the number of stamps to return if the post office gave change in stamps. Right now the program says how many stamps. Modify the program so it prints the actual values of each stamp returned.
© Copyright 2026 Paperzz