Wichita State University Logo

M451: Mathematical Computing with MATLAB©

Assignment 4: Functions



Instructions:

Submit your work in the form of a multiple .m files. Please name your files (first initial)(Last name)L03P(Problem Number).m. For example, my file for the first problem would be jMearsL04P01.m. If you are submitting a script file, start your file off with the command clear and please clearly label and describe each problem with comments in your file.

Exercises:

  1. Write a function that takes as input three variables $a$, $b$, and $c$ and returns as output the solutions to the quadratic equation $ax^2 + bx + c = 0$. Test your function on a couple of examples.

  2. Stability of an algorithm. Use your function from problem 1 to compute the roots of teh quadratic expression $x^2 - \left(10^{-6} + 10^{6}\right)x + 1$. This equation can be factored as $\left(x-10^{-6}\right)\left(x - 10^{6}\right)$, so the roots are $10^{-6}$ and $10^{6}$. Is this what you see as the ouput of your function?

    The difference between the actual solution and the computed solution that your function gives is caused by the fact that the arithmetic done by a computer is not exact. To covercome this issue, an alternate form of the quadratic equation can be written:

    $$ x = \dfrac{2c}{-b\pm\sqrt{b^2-4ac}} $$
    Write a function that solves a quadratic equation using this alternative form. Test the function on the quadratic equation $x^{2} - 1 = 0$. Find the roots of our "problem" quadratic above.

  3. An even better quadratic method. Even the method above is not optimal in finding the roots of a quadratic. Instead, you can compute the largest root by using the formula

    $$ x_{1} = \dfrac{-b-\text{sign}(b)\sqrt{b^{2}-4ac}}{2a}, $$
    and then use the fact that $x_{1}x_{2} = c/a$ (where does this come from?). Here $\text{sign}(b)$ is a function that returns -1 if $b < 0$ and 1 if $b > 0$. Type help sign for more information. Write a funciotn that implements this method of solving the quadratic equation, and then test it on the quadratic quesiton $x^2 - 2x + 1 = (x-1)(x-1)=0$. Now test it on $x^2-1=0$, and explain the output. Finally, test it on our "problem quadratic" from problem 2.



© 2020 Justin L. Mears

Your use of any material found at this site is subject to this Creative Commons License.