想用if语句实现解一元二次方程,运算结果总是出错。个人认为是字符串与数字转化有问题,但是未能解决,求大神帮忙看看代码
代码:
fprintf('we design the equation as "ax^2+bx+c=d"\n')
a=input('Enter the number of a=','s');
b=input('Enter the number of b=','s');
c=input('Enter the number of c=','s');
d=input('Enter the number of d=','s');
A=num2str('a');
B=num2str('b');
C=num2str('c');
D=num2str('d');
det=B^2-4*A*C
if det
disp('there is not real number to answer this equation')
elseif det==0
x=-B/(2*A)
disp('the answer is')
elseif det>0
x1=(-B+det)/(2*A)
x2=(-B-det)/(2*A)
else
disp('unknown wrong')
end
输入
we design the equation as "ax^2+bx+c=d"
Enter the number of a=1
Enter the number of b=2
Enter the number of c=1
Enter the number of d=0
输出
det =
-28808
there is not real number to answer this equation