My program to calculate roots works incorrectly?

1 view (last 30 days)
I have the user input a, b, and c (of a quadratic equation) into a dialog box. the discriminate is calculated and if it's greater than 0, it should output "two roots", less than 0 "no roots", etc. However, everything I enter returns "no roots"
prompt={'enter a: ','Enter b: ','Enter c: '};
dlg_title='quadratic';
answer=inputdlg(prompt,dlg_title);
a = (answer{1});
b = (answer{2});
c = (answer{3});
D = b^2 - 4.*a.*c;
p = [a b c];
if D>0
disp('There are two roots')
r = roots(p)
elseif D<0
disp('There are no roots')
else
disp('There is one root')
r = roots(p)
end
any ideas how to fix it? I'm not very experience with matlab so I'm sure what I've done wrong.

Accepted Answer

Star Strider
Star Strider on 8 Apr 2015
Your ‘answer’ variable is a cell array of strings. You have to liberate the numbers from their string representation:
a = str2num(answer{1});
b = str2num(answer{2});
c = str2num(answer{3});
  6 Comments
Star Strider
Star Strider on 9 Apr 2015
Someone who seemed to be working on exactly the same problem as you were. Thought you might have been classmates.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!