10.07.2015 Views

MTH 510 - Lab 2 - Fall 2012 Example 1 - Matlab if and case ...

MTH 510 - Lab 2 - Fall 2012 Example 1 - Matlab if and case ...

MTH 510 - Lab 2 - Fall 2012 Example 1 - Matlab if and case ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>MTH</strong> <strong>510</strong> - <strong>Lab</strong> 2 - <strong>Fall</strong> 2013<strong>Example</strong> 1 - <strong>Matlab</strong> <strong>if</strong> <strong>and</strong> <strong>case</strong> structuresDue to floating-point errors, computing the roots of the quadratic equationby means of the st<strong>and</strong>ard quadratic formulaax 2 + bx + c = 0 (1)x 1,2 = −b ± √ b 2 − 4ac2amay not always be the best way to compute them. In particular, <strong>if</strong> 4|ac| ≪ b 2 , better expressions arex 1 = − b + √ b 2 − 4ac2ax 1 = −b + √ b 2 − 4ac, x 2 =2a2c, x 2 = −b + √ , <strong>if</strong> b > 0,b 2 − 4ac(2)2c−b + √ b 2 − 4ac , <strong>if</strong> b < 0,These formulas can be derived by rationalizing the numerator in the root that leads to the biggest error.Write an M-file to compute the roots of (1) using (2) for the <strong>case</strong> when 4|ac| < b 2 . For values of a, b, c outsidethis range, output the comment “a,b,c outside range for this example”. Test your function for the equationusing MATLAB’s double precision format.x 2 + 9 12 x = 3<strong>Example</strong> 2 - Other <strong>Matlab</strong> Structures - Brief DemoFor loops:for i=1:0.1:3disp(i)endWhile structure:x=1while (x


Exercise (Problem 4.4)Using double precision, write an M-file to determine the machine espilon based on the following algorithm:Step 1: Set ɛ = 1Step 2: If 1 + ɛ is less than or equal to 1, then go to Step 5. Otherwise go to Step 3.Step 3: ɛ = ɛ/2Step 4: Return to Step 2.Step 5: ɛ = 2 ∗ ɛCompare your result with the value returned from the built-in eps function.2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!