您好,欢迎访问三七文档
求一元二次方程的根设计并编写一个程序,用来求解一元二次方程的根。答案:我们将本章开头介绍的方法进行编程。1.陈述问题这个问题的陈述非常的简单,我们要求一元二次方程的根,不管它的根是实根还是复根,有一个根还是两个根。2.定义输入和输出本程序的输入应为系数a,b,cax2+bx+c=0(3.1)输出量应为两个不相等的实数。两个相等的实数或两个复数。3.写出算法本程序可分为三大块,它的函数分别为输入,运算过程和输出。我们把每一个大块分解成更小的,更细微的工作。根据判别式的值,可能有三种计算途径。读取输入的数据计算出根输入出根所以我们要用到有三种选项的if结构。产生的伪代码如下Prompttheuserforthecoefficientsa,b,andc.Reada,b,andcdiscriminant←b^2-4*a*cifdiscriminat0x1←(-b+sqrt(discriminant))/(2*a)x1←(-b-sqrt(discriminant))/(2*a)Writemsgthatequationhastwodistinctrealroots.Writeoutthetworoots.elseifdiscriminant==0x1←-b/(2*a)Writemsgthatequationhastwoidenticalrealroots.Writeouttherepeatedroots.elsereal_part←-b/(2*a)imag_part←sqrt(abs(discriminant))/(2*a)Writemsgthatequationhastwocomplexroots.Writeoutthetworoots.end4.把算法转化为MATLAB语言%Scriptfile:calc_roots.m%%Purpose:%Thisprogramsolvesfortherootsofaquadraticequation%oftheforma*x^2+b*x+c=0.Itcalculatestheanswers%regardlessofthetypeofrootsthattheequationpossesses.%%Recordofrevisions:%DateProgrammerDescriptionofchange%=================================%12/04/98S.J.ChapmanOriginalcode%%Definevariables:%a--Coefficientofx^2termofequation%b--Coefficientofxtermofequation%c--Constanttermofequation%discriminant--Discriminantoftheequation%imag_part--Imagpartofequation(forcomplexroots)%real_part--Realpartofequation(forcomplexroots)%x1--Firstsolutionofequation(forrealroots)%x2--Secondsolutionofequation(forrealroots)%Prompttheuserforthecoefficientsoftheequationdisp('Thisprogramsolvesfortherootsofaquadratic');disp('equationoftheformA*X^2+B*X+C=0.');a=input('EnterthecoefficientA:');b=input('EnterthecoefficientB:');c=input('EnterthecoefficientC:');%Calculatediscriminantdiscriminant=b^2-4*a*c;%Solvefortheroots,dependingonthevlaueofthediscriminant.ifdiscriminant0%therearetworealroots,so...x1=(-b+sqrt(discriminant))/(2*a);x2=(-b-sqrt(discriminant))/(2*a);disp('Thisequationhastworealroots:');fprintf('x1=%f\n',x1);fprintf('x2=%f\n',x2);elseifdiscriminant==0%thereisonerepeatedroot,so...x1=(-b)/(2*a);disp('Thisequationhastwoidenticalrealroots:');fprintf('x1=x2=%f\n',x1);else%therearecomplexroots,so...real_part=(-b)/(2*a);imag_part=sqrt(abs(discriminant))/(2*a);disp('Thisequationhascomplexroots:');fprintf('x1=%f+i%f\n',real_part,imag_part);fprintf('x1+%f-i%f\n',real_part,imag_part);end5.检测这个程序下一步,我们必须输入实数来检测这个程序。因这个程序有三个可能的路径。所以在我们确信每一人路径都工作正常之前,必须把这三个路径检测一遍。从式子(3.2)中,我们可以有用下面的方法来验证程序的正确性。x2+5x+6=0x=-2,andx=-=3x2+4x+4=0x=-2x2+2x+5=0x=-1±i2如果输入上面三个方程的系数得到对应的结果,则说明程序是正确的。calc_rootsThisprogramsolvesfortherootsofaquadraticequationoftheformA*X^2+B*X+C=0.EnterthecoefficientA:1EnterthecoefficientB:5EnterthecoefficientC:6Thisequationhastworealroots:x1=-2.000000x2=-3.000000calc_rootsThisprogramsolvesfortherootsofaquadraticequationoftheformA*X^2+B*X+C=0.EnterthecoefficientA:1EnterthecoefficientB:4EnterthecoefficientC:4Thisequationhastwoidenticalrealroots:x1=x2=-2.000000calc_rootsThisprogramsolvesfortherootsofaquadraticequationoftheformA*X^2+B*X+C=0.EnterthecoefficientA:1EnterthecoefficientB:2EnterthecoefficientC:5Thisequationhascomplexroots:x1=-1.000000+i2.000000x1+-1.000000-i2.000000在三种不同的情况下,程序都给出了正确的结果。
本文标题:伪代码经典案例
链接地址:https://www.777doc.com/doc-4931865 .html