Matlab HW help
John Doe and Jane Doe
CS 122L-1
Lab 1: Quadratic Equation Demo Lab
7-14-2014
1. Task Description
The main task in this lab is to construct a program that finds the roots of a quadratic function. The program will ask the user for the constants in the quadratic function, calculate and print the values of the roots, and plot the quadratic function so that the user can visually confirm the roots. Additionally, the program should re-prompt the user if the value entered for a quadratic function constant would result in a division by zero error.
2. Learning Objectives
There are several important concepts to learn from this lab. First, the lab teaches us how to translate the solution for finding the roots of a quadratic function from a mathematical statement into MATLAB code. This generally helps us understand how to break down mathematical equations such that they can easily be written in terms of variable expressions. Second, the lab teaches us several important aspects of MATLAB syntax such as user interaction, loops, conditionals, and plots. Third, the lab gives us experience with using header and inline comments.
3. Approach
Our approach is to break down the main task into smaller subtasks and then complete each subtask in order. These subtasks include reading input from the user, checking for valid input, calculating the roots, displaying the values of the roots, and plotting the quadratic equation. The program needs to re-prompt the user if the constants entered by the user would result in a division by zero error when calculating the roots. The display of the root values should change based on whether the roots are real or complex.
4. Mathematical Concepts
The main task of the lab is to write a program that calculates the roots of a quadratic function. We found details on calculating the roots of a quadratic function on a Wolfram MathWorld site (http://mathworld.wolfram.com/QuadraticEquation.html).
A quadratic function can be written in the form
where a, b, and c are real or complex constants. The roots of the quadratic function are the values x for which f(x) = 0. An equation of this form, as shown below, is called the quadratic equation.
The general solution of the quadratic equation, known as the quadratic formula, is shown below. The derivation of the quadratic formula, which involves completing the square, can be found on the Wolfram MathWorld site.
The discriminant is the expression under the square root sign in the quadratic formula, and it indicates the number of roots and whether they are real or complex. There are two real roots if the discriminant is positive. There is one real root if the discriminant is zero. There are two complex roots if the discriminant is negative.
5. Program Inputs
· The quadratic function constants: a, b, and c
6. Program Outputs
· The roots of the quadratic function
· The plot of the quadratic function
7. Program Description
Below is a description of the steps our program uses to accomplish the main task for this lab.
· Read the quadratic function constants as input from the user. Check if the user entered 0 for the value of ‘a’. If so, re-prompt the user to enter a new value for ‘a’.
· Calculate the roots of the quadratic function. Store the value of the discriminant so that it can be referred to later.
· Display the values of the roots. The display should change based on whether the roots are real (positive or 0 value for the discriminant) or complex (negative value for the discriminant).
· Plot the quadratic function.
8. Source Code
Below is the source code from the M-file for the lab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Authors: John Doe and Jane Doe
% Lab Section: CS122L-1
% Lab Number: 1
% Lab Name: Quadratic Equation Demo Lab
% Date: 7-14-14
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program finds the roots of a quadratic function in the form
% f(x) = ax^2 + bx + c. It prompts the user to enter the constants in the
% quadratic function and then calculates and displays the roots. The
% program also plots the quadratic function so that the user can visually
% confirm the values of the roots.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Clear the workspace and the command window.
clc
clear
%Display a startup message.
fprintf('Please enter values for the quadratic function constants.\n')
%Read in the quadratic function constants. Make sure that the value of 'a'
%is not 0.
readAFromUser = 1;
while readAFromUser
a = input('Please enter a value for ''a'': ');
if a == 0
fprintf('The value of ''a'' cannot be 0.\n')
else
readAFromUser = 0;
end
end
b = input('Please enter a value for ''b'': ');
c = input('Please enter a value for ''c'': ');
%Store variables for calculating the roots of the quadratic function.
discriminant = b^2 - 4*a*c;
sqrt_discriminant = sqrt(discriminant);
denominator = 2*a;
%Calculate the roots of the quadratic function.
root1 = (-b + sqrt_discriminant) / denominator;
root2 = (-b - sqrt_discriminant) / denominator;
%Display the values of the roots.
fprintf('The roots of the quadratic equation are ')
if discriminant >= 0 %for real roots
fprintf('%.2f and %.2f.\n', root1, root2)
else %for complex roots
fprintf('%.2f + %.2fi and %.2f + %.2fi.\n', ...
real(root1), imag(root1), real(root2), imag(root2))
end
%Plot the quadratic function.
syms x
ezplot(a*x^2 + b*x + c)
grid
9. Code Execution Results
Use the program to find the roots and make a plot for each of the following quadratic functions.
I. f(x) = x2 – x – 6
Roots: -2, 3
II. f(x) = 2x2 + 3x + 10
Roots: -0.75 + 2.11i, -0.75 – 2.11i
III. f(x) = -x2 + 2x - 1
Root: 1 (Note that there is only 1 root for this function)
10. Question Responses
For the following questions, assume a, b, and c are real numbers, and the domain of the quadratic function is the set of real numbers.
I. How many points in the graph touch the x-axis in the plot of a quadratic function for each of the following cases?
Ia. two real roots: 2
Ib. one real root: 1
Ic. two complex roots: 0
II. What shape does the graph have when a is positive?
The parabola opens upward and has a global minimum.
III. What shape does the graph have when a is negative?
The parabola opens downward and has a global maximum.
11. Conclusions
We can use the concepts we learned in this lab to help us with translating mathematical formulas with certain enforced conditions into MATLAB code. For example, we can see how we might store pieces of a formula in variables so that they can be used later. The proper use of temporary variables can help to reduce complexity and improve efficiency of the program. Concepts such as loops, conditionals, and plots will be very useful going forward. Additionally, it is useful to know the syntax for asking a user for input until he or she enters an acceptable value.
The program for this lab is very helpful for quickly calculating the roots of a quadratic function. It is much more efficient than attempting to calculate the roots by hand. The uses of this particular application are limited since it can only be used to calculate the roots of second order polynomials. However, we could easily modify the program to find the roots of higher degree polynomial functions.
We ran into a couple of issues while implementing the code for this lab. First, we forgot to implement the code to check that a is not zero, so we ended up with a divide by zero error. We wrapped a while statement around an if check so that the program continues to read from the user until he or she enters a non-zero value for a. Second, we forgot to consider the special case of displaying complex root values. When you pass a variable that holds a complex number as an argument to an fprintf statement without specifying whether you want the real or complex part of the number, MATLAB only prints out the real part of the complex number. However, there is a way to explicitly retrieve the real and imaginary parts of a complex number. We introduced an if check to print out the real and imaginary parts of the roots if the discriminant is negative.