简体   繁体   English

在Matlab中提高二次方程的鲁棒性

[英]Enhancing the robustness of quadratic equation in matlab

The task was to write a code in matlab that solves the quadratic equation. 任务是在matlab中编写代码以解决二次方程式。

MY code is below 我的代码在下面

a=input('a=  ');
b=input('b=  ');
c=input('c=  ');
if a==0
    x=double(-c/b);
    disp(x)
else
    discriminant=b.^2-4*a*c;
    if discriminant<0
        error('Roots are not real')
    else
        e=sqrt(discriminant);
        x1=double((-b+e)/(2*a));
        x2=double((-b-e)/(2*a));
    end
end

disp(x1)
disp(x2)

Now, this gives result. 现在,这给出了结果。 However, when confronted with questions about the "robustness" of the code to guard against division by zero, unnecesary overflow and underflow, cancellation, how can I modify the code? 但是,当遇到有关代码的“健壮性”以防止被零除,不必要的上溢和下溢,取消的问题时,如何修改代码? For the first case, division by zero, my code already works, but what do they mean by underflow/overflow? 对于第一种情况,除以零,我的代码已经可以工作,但是下溢/上溢是什么意思?

The overflow/underflow occurs when a value in programming starts occupying extra and unexpected memory cells. 当编程中的值开始占用额外和意外的存储单元时,就会发生上溢/下溢。 In that case, the program runs into a runtime error or sometimes shuts down itself to prevent harm to system. 在这种情况下,程序会遇到运行时错误,或者有时会自行关闭以防止对系统造成损害。 To avoid such severe cases, a program should be managed to exit or return from a bad-valued function or bad code execution whenever needed. 为了避免发生这种严重情况,应在需要时管理程序退出或返回值不正确的函数或执行错误的代码。 This ,of course, should be done using if-else commands, return or break or while loops. 当然,这应该使用if-else命令, returnbreakwhile循环来完成。

ps your program has still a flaw. ps您的程序仍然存在缺陷。 It malfunctions when a=b=0 and c!=0. 当a = b = 0和c!= 0时,它将发生故障。 I converted your code to a function in MATLAB (which can now be called in Command Window) and added a MAX_VALUE to avoid out-of-bound variables. 我将代码转换为MATLAB中的函数(现在可以在“命令窗口”中调用),并添加了MAX_VALUE以避免变量越界。

Here is the code 这是代码

function x=quadroots(a,b,c,MAX_VALUE)
if nargin == 3
    MAX_VALUE=1e50;
end
if abs(a)>MAX_VALUE||abs(b)>MAX_VALUE||abs(c)>MAX_VALUE
    error('Out of bound variables')
end
if a==0 && b~=0
    x=double(-c/b);
    disp(x)
elseif a==0 && b==0 && c==0
    error('0=0')
elseif a==0 && b==0 && c~=0
    error('Infeasible solutions')
else
    discriminant=b.^2-4*a*c;
    if discriminant<0
        error('Roots are not real')
    else
        e=sqrt(discriminant);
        x(1)=double((-b+e)/(2*a));
        x(2)=double((-b-e)/(2*a));
    end
end
end

Let's say I have a variable that is a 2-bit integer. 假设我有一个2位整数的变量。 Now, for my variable I can only hold 2^2 in decimal as my max number. 现在,对于我的变量,我只能以小数形式保留2 ^ 2作为最大数。 Let's take a look at how 2 bits count: 让我们看一下2位如何计数:

0 0 (add 1) --> 0 1
0 1 (add 1) --> 1 0
1 0 (add 1) --> 1 1
1 1 (add 1) --> 0 0

It wraps back around to 0 0 because I ran out of bits to hold my number so it starts over. 它回绕到0 0因为我用完所有位数来保存我的数字,所以它重新开始。 This is called overflow. 这称为溢出。 The same is similar for underflow except it wraps back to the max. 下溢相同,除了下溢回最大值。 For matlab, the number of bits can be larger like 32-bits or 64-bits. 对于matlab,位数可以更大,例如32位或64位。 So what happens when you get a very large number input and you perform math on it? 那么,当您获得大量输入并对其进行数学运算时会发生什么呢? Will it overflow because you ran out of bits? 它会因为您用完所有位而溢出吗? or underflow? 还是下溢?

FYI: If you don't know binary, you should take a quick look on wiki to help out because all digits in any computer language will be expressed as bits but luckily we deal with the decimal values. 仅供参考:如果您不知道二进制,则应该快速浏览Wiki以寻求帮助,因为任何计算机语言中的所有数字都将表示为位,但是幸运的是我们处理十进制值。

Also, some hand waving here. 另外,有些手在这里挥舞着。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM