This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 9/17/2010 Problem: A 50mm diameter cork ball (specific gravity = ρ = 0.25) is floating in water. How deep will the ball float? r x The total volume of the sphere is: The submerged volume is given by: 4 V r 3 3 1 VS x 2 3r x 3 Ball weight = weight of displaced water: Final result (in root finding form): V VS (1) x 3 3rx 2 4r 3 0 Note: problem adapted from Fausett p 48 General formulae for a cubic equation ( ax3 + bx2 + cx + d = 0): 1 9/17/2010 For cubics using a formula to find the roots is not a practical proposition. Casio calculator still a possibility (enter 3 for “Degree?” and keep in mind that there may be one real and two complex roots). In Matlab cubics are a piece of cake: >> D = 50; >> r = D/2; >> rho = 0.25; >> p = [ 1 (‐3 * r) 0 (4 * r ^3 * rho)]; >> x = roots(p) x = 71.9846 16.3176 ‐13.3022 As x must be between 0 and D the answer is 16.3176 mm. fzero is just as convenient and has the advantage of given us just the root we want: >> f = @(x) x.^3 ‐ 3 * r * x.^2 + 4 * r^3 * rho; >> depth = fzero(f, [0 D]) Two possible ways of plotting the function: >> figure (1) >> fplot (f, [‐D (2 * D)]); >> figure (2) >> x = linspace (‐D, 2 * D, 50); >> y = polyval (p, x); % “p” (polynomial coefficients) from previous slide >> plot (x, y); Function polyval accepts the coefficients of a polynomial and an x value. It produces the corresponding y value. The x value may be a vector (as it is here). In this case the result is also a vector. Note: In this case y = f(x) would have exactly the same effect. 2 9/17/2010 Script m‐files • “Canned” collections of Matlab commands • Created and modified in editor window. Stored as “script.m”, where script is a user selected name. • To execute the commands, enter “run script” (or just script) in the command window. • Running a script file is logically equivalent to typing in all of the commands contained in it. • If you are unable to execute a script check that your path includes the folder in which it is stored. • If the results seem out of date, make sure that the script was saved before being executed (Matlab runs the last SAVED version of the script). • Scripts may also be executed from the editor window (F5 or via the “Debug” menu). Results appear in the command window. Doing things this way automatically saves the script. The script file below is a “canned” solution to the floating ball problem. The fprintf command outputs a formatted message to the command window. D = 50; r = D/2; rho = 0.25; f = @(x) x.^3 ‐ 3 * r * x.^2 + 4 * r^3 * rho; % plot function using fplot figure (1) fplot (f, [‐D (2 * D)]); grid on; %find and display root of interest depth = fzero(f, [0 D]); fprintf ('The ball will float %f mm deep.\n', depth); % ball1.m 3 9/17/2010 fprintf ('The ball will float %f mm deep.\n', depth); Format string Format codes: %d %e,%E %f %g integer format scientific format(upper or lower case ‘e’) fixed point decimal format the more compsct of %e, %f Control Codes: \n \t new line tab Format codes may include a field width and the number of decimal places required: %5d display value as an integer in a field 5 characters wide %10.6f display value with 6 decimal places in a field 10 characters wide See text p 48 The script file below displays all of the roots of the function and plots the function using “plot”. It illustrates how indexing can be used to select array elements. Matlab array indexing differs from C++ indexing in two ways: round rather than square brackets are used and elements are numbered from 1 rather than from 0. D = 50; r = D/2; rho = 0.25; p = [ 1 (‐3 * r) 0 (4 * r ^3 * rho)]; x = roots(p); fprintf ('The roots are %f, %f, and %f.\n', x(1), x(2), x(3)); % plot function using plot figure (2) x = linspace (‐D, 2 * D, 50); y = polyval (p, x); plot (x, y); grid on; % ball2.m Note: Indexing isn’t actually required here. “x(1), x(2), x(3)” could be replaced with just “x”. The three values in “x” would be placed in the three value fields. 4 9/17/2010 In general array elements are selected by specifying a row number and a column number, separated by a comma and enclosed in round brackets: >> A = [ 1 2 3; 4 5 6; 7 8 9] % semi‐colons start a new row A = 1 2 3 4 5 6 7 8 9 >> A(2,3) ans = 6 >> A(3,1) ans = 9 Unlike C++, Matlab rejects attempts to access non‐existent array elements: >> A(9,9) ??? Attempted to access A(9,9); index out of bounds because size(A)=[3,3]. In the case of vectors only one index value is normally specified: >> B = [ 5 6 7 ] B = 5 6 7 >> B(1, 2) ans = 6 >> B(2) % If the array is a row vector the row number can be omitted. ans = 6 >> C = [12; 13; 14] C = 12 13 14 >> C(3,1) ans = 14 >> C(3) % If the array is a column vector the column number can be omitted. 14 5 9/17/2010 Script m‐files can be made more generally useful by having them read in values. The script m‐file below reads in the ball diameter and specific gravity, works out how deep the ball will float, and outputs the result in a nicely formatted message. % interactive version of ball script file rho = input ('Enter specific gravity: '); D = input ('Enter diameter of ball in mm: '); r = D / 2; f = @(x) x.^3 ‐ 3 * r * x.^2 + 4 * r^3 * rho; depth = fzero(f, [0 D]); fprintf ('The ball will float %f mm deep.\n', depth); % ball3.m Function input displays the specified prompt message, waits for the user to enter a value, and then returns this value. Aside: What will happen is the specific gravity entered is not between 0 and 1? 6 ...
View
Full
Document
This note was uploaded on 10/18/2010 for the course ECOR2606 2606 taught by Professor Aaaa during the Spring '10 term at Carleton CA.
 Spring '10
 aaaa

Click to edit the document details