Question 6: 4 marks Write a MATLAB function which will calculate the roots of a
ID: 2292275 • Letter: Q
Question
Question 6: 4 marks Write a MATLAB function which will calculate the roots of a quadratic equation. Notes: MATLAB has a built in function called roots to calculate the roots of a quadratic equation. However, you are required to create a MATLAB function which performance that calculation Make sure to state all the assumptions, write all the equations and present flow of your function in your report Pick an example of a quadratic functionland compare the results of the function you created with the results of the MATLAB built in function.Explanation / Answer
Background
We need to have the equation in the following form:
ax2 + bx + c = 0
where a, b, and c are real coefficients.
The formula used to calculate the roots is:
Let us consider the following quadratic equation which needs to be solved.
2x2 + 4x - 3 = 0, we identify the coefficients a, b and c as 2, 4 and -3 respectively. These coefficients should be given to the program.
Following MATLAB program calculates the roots of a quadratic equation.
In the command window of MATLAB, Open a new m-file and write the following codes.
function x = rqe(a,b,c)
x 1 = (-b + sqrt (b^2 - 4 * a * c))/(2*a);
x 2 = (-b - sqrt(b^2 - 4 * a * c))/(2*a);
x = rqe(2, 4, -3)
then MATLAB returns
x = 0.5811 -2.5812
We assign to variables x1 and x2 the calculated values using the formula, and the returning result x is a vector containing x 1 and x 2.The returning result is a vecor which gives the values of x1, x2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.