全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
求一元二次方程的根设计并编写一个程序,用来求解一元二次方程的根。答案:我们将本章开头介绍的方法进行编程。1.陈述问题这个问题的陈述非常的简单,我们要求一元二次方程的根,不管它的根是实根还是复根,有一个根还是两个根。2.定义输入和输出本程序的输入应为系数a,b,cax2 + bx + c = 0 (3.1)输出量应为两个不相等的实数。两个相等的实数或两个复数。3.写出算法本程序可分为三大块,它的函数分别为输入,运算过程和输出。我们把每一个大块分解成更小的,更细微的工作。根据判别式的值,可能有三种计算途径。读取输入的数据计算出根输入出根所以我们要用到有三种选项的if 结构。产生的伪代码如下Prompt the user for the coefficients a, b, and c.Read a, b, and cdiscriminant b2 - 4*a*cif discriminat 0x1 (-b + sqrt(discriminant) / (2*a)x1 (-b - sqrt(discriminant) / (2*a)Write msg that equation has two distinct real roots.Write out the two roots.elseif discriminant = 0x1 -b / (2*a)Write msg that equation has two identical real roots.Write out the repeated roots.elsereal_part -b / (2*a)imag_part sqrt(abs(discriminant) / (2*a)Write msg that equation has two complex roots.Write out the two roots.end4.把算法转化为MATLAB 语言%Script file: calc_roots.m% Purpose:% This program solves for the roots of a quadratic equation% of the form a*x2 + b*x + c = 0. It calculates the answers% regardless of the type of roots that the equation possesses.% Record of revisions:% Date Programmer Description of change% = = =% 12/04/98 S. J. Chapman Original code% Define variables:% a -Coefficient of x2 term of equation% b -Coefficient of x term of equation% c -Constant term of equation% discriminant -Discriminant of the equation% imag_part -Imag part of equation (for complex roots)% real_part -Real part of equation (for complex roots)% x1 -First solution of equation (for real roots)% x2 -Second solution of equation (for real roots)% Prompt the user for the coefficients of the equationdisp (This program solves for the roots of a quadratic );disp (equation of the form A*X2 + B*X + C = 0.);a = input(Enter the coefficient A: );b = input(Enter the coefficient B: );c = input(Enter the coefficient C: );% Calculate discriminantdiscriminant = b2 - 4 * a * c;% Solve for the roots, depending on the vlaue of the discriminant.if discriminant 0 % there are two real roots, so .x1 = (-b + sqrt(discriminant) / (2*a);x2 = (-b - sqrt(discriminant) / (2*a);disp(This equation has two real roots:);fprintf(x1 = %fn, x1);fprintf(x2 = %fn, x2);elseif discriminant = 0 % there is one repeated root, so .x1 = ( -b ) / (2*a);disp(This equation has two identical real roots:);fprintf(x1 = x2 = %fn, x1);else % there are complex roots, so .real_part = (-b) / (2*a);imag_part = sqrt( abs(discriminant) / (2*a);disp(This equation has complex roots:);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 = 0 x = -2, and x = -=3x2 + 4x + 4 = 0 x = -2x2 + 2x + 5 = 0 x = -1 i2如果输入上面三个方程的系数得到对应的结果,则说明程序是正确的。 calc_rootsThis program solves for the roots of a quadraticequation of the form A*X2 + B*X + C = 0.Enter the coefficient A: 1Enter the coefficient B: 5Enter the coefficient C: 6This equation has two real roots:x1 = -2.000000x2 = -3.000000 calc_rootsThis program solves for the roots of a quadraticequation of the form A*X2 + B*X + C = 0.Enter the coefficient A: 1Enter the coefficient B: 4Enter the coefficient C: 4This equation has two identical real roots:x1 = x2 = -2.000000 calc_rootsThis program solves for the roots of a quadraticequation of the form A*X2 + B*X + C = 0.Enter the co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年托育机构保健人员考核题库(含答案解析)
- 2026广西南宁市第十四中学教育集团招聘顶岗教师、后勤教辅笔试参考题库及答案解析
- 2026及未来5年营养啫喱喷雾剂项目可行性研究报告(市场调查与数据分析)
- 2026及未来5年荷兰豆种项目可行性研究报告(市场调查与数据分析)
- 2026及未来5年草药提取物项目可行性研究报告(市场调查与数据分析)
- 2026及未来5年苄脒盐酸盐一水合物项目可行性研究报告(市场调查与数据分析)
- 2026年宣化科技职业学院高职单招笔试化学历年参考题库含答案解析
- 2026年安徽黄梅戏艺术职业学院高职单招笔试英语历年参考题库含答案解析
- 2026年安徽卫生健康职业学院高职单招笔试语文历年参考题库含答案解析
- 2026年安徽住院医师-安徽住院医师中医儿科历年参考题库含答案解析
- 院内传染病培训课件
- 脑梗塞后遗症护理疑难病例讨论
- 商业门面装修管理办法
- 掀冬学高潮动员班会课件
- 公司cqc标志管理办法
- 脑电治疗仪讲课件
- CJ/T 410-2012隔油提升一体化设备
- 护士床旁综合能力操作考核评分标准
- DG-TJ 08-402-2021 生活垃圾收集站(压缩式)设置标准
- 110kv高压输电线路毕业设计样本
- (正式版)YBT 139-2024 复合铸钢支承辊
评论
0/150
提交评论