Introductory Programming Fall 2006 Let's start with a little cleanup from last time: 1) The version of baseball.m I gave you _did_ have an error. It would try to use a function named proj if it existed (and it probably did) so it would work if you had a working version of proj, and fail otherwise. I have put a corrected version at http://wb/ip/code/baseball.m 2) Many of you are writing relatively big chunks of code before you start debugging. I recommend writing small, testable pieces of code and working your way up. For example, I started with function angle=optimal_angle(velocity) as = linspace(0, 90) angle = 45 end And then added the first loop: function angle=optimal_angle(velocity) as = linspace(0, 90); for i=1:length(as) distance(i) = baseball(velocity, as(i)); end plot(as, distance); end Plotting can be very helpful when you are debugging large vectors. Alternatively, you could start with small vectors and work your way up. For the next step (finding the max), you have a couple of choices 1) loop through distance to find the maximum, OR 2) use max to find the maximum Then, to find the corresponding angle, you could 1) loop through distance to find the index of the maximum, OR 2) use find to find the index of the maximum But find is confusing because it uses a feature (logical arrays) we haven't seen yet. OR If you read the help page for max, it might give you another idea. Golden ratio search ------------------- I gave you a simplified version of the golden ratio search in class, but it turns out I simplified it one step too many! 0) Given a, b, c 1) Evaluate f(a), f(b), f(c) 2) Choose a value of x that is in either [a, b] or [b, c], whichever interval is bigger. 3) Evaluate f(x) 4) Depending on f(x), move a, b, and/or c appropriately such that f(b) > f(a) and f(b) > f(c) So there are four cases we have to deal with! Here's what I started with: function angle = golden(velocity) a = 0; b = 45; c = 90; fa = baseball(velocity, a); fb = baseball(velocity, b); fc = baseball(velocity, c); if b-a > c-b x = (a+b) / 2; else x = (b+c) / 2; x angle = [a c] end I recommend that you write the code without a loop, get one iteration working, and then add the loop. (Under the Text menu, notice the "Increase indent" command)