Introductory Programming Fall 2006 Today: 1) follow the instructions in notes09.txt to find the roots of the 6th Chebyshev polynomial 2) work on the duck problem Eval 04 Solution ---------------- Many of you did a good job on the first part and had trouble with the second part. function y = cheby6(x) y = 32 * x^6 - 48 * x^4 + 18 * x^2 - 1; end function plot_cheby6(low, high) X = linspace(low, high, 100); for i=1:length(X) Y(i) = cheby6(X(i)); end plot(X, Y) end Notice that part two is an example of BEFORE: create the vector of x values DURING: for each x value, use cheby6 to find the corresponding y value AFTER: plot Y versus X A common source of confusion was how to iterate through the elements of a vector: for i=1:length(X) Y(i) = cheby6(X(i)); end In this loop, the values of i are the indices, from 1 to length(X) Y(i) and X(i) are the elements of the vectors. The Duck Problem ---------------- The density of a duck, rho, is 0.3 g / cm^3 (0.3 times the density of water). The volume of a sphere with radius r is 4/3 pi r^3. If a sphere with radius r is submerged in water to a depth d, the volume of the sphere below the water line is volume = 1/3 pi (3r d^2 - d^3) as long as d < r An object floats at the level where the weight of the displaced water equals the total weight of the object. Assuming that a duck is a sphere with radius 10 cm, at what depth does a duck float? Suggestions: 1) Write an equation relating rho, d and r. 2) Rearrange the equation so the right-hand side is zero. Our goal is to find values of d that are roots of this equation. 3) Write a MATLAB function that evaluates this function. Test it, then make it a quiet function. 4) Make a guess about the value of d0 to use as a starting place. 5) Use fzero to find a root near d0. 6) Check to make sure the result makes sense. In particular, check that d < r, because otherwise the equation we used doesn't work! 7) Try different values of rho and r and see if you get the effect you expect. What happens as rho increases? Goes to infinity? Goes to zero? What happens as r increases? Goes to infinity? Goes to zero?