kkxiaoxian
kkxiaoxian
2017-03-11 16:29
采纳率: 62.5%
浏览 4.4k
已采纳

用matlab实现解一元二次方程的代码

想用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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_15022381
    木心魂 2017-03-14 01:44
    已采纳

    字符转数字错了,改成如下:
    A=str2num(a);
    B=str2num(b);
    C=str2num(c);
    D=str2num(d);

    点赞 3 评论
  • caozhy
    点赞 评论

相关推荐