0

Struggling with a MATLAB quadratic equation. I keep getting a complex number as my answer and other errors keep occurring.

Write a MATLAB function that solves a quadratic equation of the form a*x^2 + b*x + c = 0

The syntax of your function should take the form

[quadRoots1,quadRoots2] = Q1_quadratic (a,b,c);

where a, b and c are the quadratic coefficients; and quadRoots1 and quadRoots2 are the two determined roots. For the case where only one root is present (for example when a=1, b=2 and c=1), you should set your second output to NaN (not a number). If no roots are present, then set both outputs to NaN.

Steve
  • 1,579
  • 10
  • 23
  • It's been a while, but if memory serves, if b^2 - 4ac < 0, then your roots are complex. If you are only solving for real roots, you are going to need to take care of that. – Jason Kennaly Jan 16 '16 at 15:18
  • Your question states what to do when there are no real roots: maybe you could write an `if` statement for checking if this is the case – Steve Jan 16 '16 at 15:19
  • I'm voting to close this question as off-topic because it's homework and the OP has shown no attempt. – jub0bs Jan 16 '16 at 16:58
  • *I keep getting a complex number as my answer and other errors keep occurring.* What have you tried? Edit your question and add your best attempt so far. See http://stackoverflow.com/help/how-to-ask – jub0bs Jan 16 '16 at 17:02

1 Answers1

1

Make sure to check if the number under the root sign in your quadratic formula is:

  • Positive (>0): two distinct real roots,
  • Equal to zero (==0): single real numbered degenerate root (or, rather, two non-distinct roots).
  • Negative (<0: your roots are complex (recall sqrt(-1) = i, with our imaginary unit i). From the sound of your question specs, it seems you are to treat complex as if "no roots are present".

You can check the cases above in your function Q1_quadratic(...) using an if-elseif-else clause, e.g.:

function [quadRoots1, quadRoots2] = Q1_quadratic(a, b, c)

  d = b^2 - 4*a*c; % your number under the root sign in quad. formula

  % real numbered distinct roots?
  if d > 0
    quadRoots1 = (-b+sqrt(d))/(2*a);
    quadRoots2 = (-b-sqrt(d))/(2*a);
  % real numbered degenerate root?
  elseif d == 0 
    quadRoots1 = -b/(2*a);
    quadRoots2 = NaN;
  % complex roots, return NaN, NaN
  else
    quadRoots1 = NaN;
    quadRoots2 = NaN;
  end    
end

Test:

% distinct real roots: expects [2, -8]
[a, b] = Q1_quadratic(1, 6, -16)
    % OK!

% degenerate real root: expects [-1, NaN]
[a, b] = Q1_quadratic(1, 2, 1)
    % OK!

% complex roots: expects [NaN, NaN]
[a, b] = Q1_quadratic(2, 2, 1)
    % OK!
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • This is obviously homework, and the OP has shown no attempt. Why would you give a complete answer? – jub0bs Jan 16 '16 at 16:57
  • 1
    @Jubobs Ow my bad, I skimmed over the question to quickly to realize it's obviously a homework question (very obvious now after the quote environment edit by Steve). I agree homework questions shouldn't be answered in such completeness, my apologies, I simply missed this one out of my own obliviousness. – dfrib Jan 16 '16 at 18:49
  • That's fine; I'm glad you didn't do it on purpose. – jub0bs Jan 16 '16 at 18:57
  • @Jubobs I'm glad you told me. With this sorted out, should I delete this answer now, do you think? I presume the question will be closed sooner or later anyway. – dfrib Jan 16 '16 at 19:01
  • 1
    I'd say leave it. It's probably too late to take it down now, and it does have value. – jub0bs Jan 16 '16 at 19:18