kkxiaoxian 2017-03-11 16:29 采纳率: 0%
浏览 4844
已采纳

用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条回答 默认 最新

  • 木心魂 2017-03-14 01:44
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?