QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1221|回复: 0
打印 上一主题 下一主题

二次方程的求解(重写)---例题

[复制链接]
字体大小: 正常 放大
回帖奖励 1 点体力 回复本帖可获得 1 点体力奖励! 每人限 1 次

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-9-22 21:45 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta




    二次方程的求解(重写)
    复数的价值体现在它能使运算简化。
    例如,我们在例3.2中已解决的二次方程的求解问题,但它根据判别式用到3个选项的选择结构,由于复数的出现,负数的平方根的处理将不困难。所以能够大大简化我们的计算。编写一个普通的程序,解一元二次方程的根,不管是什么类型的。用复变量,而不用选择结构。
    1. 陈述问题
    编写一个程序,解一元二次方程的根,不管有两个不同的实根,还是用两个相同的实根或两个不同复根。不需要检测判别式。
    2. 定义输入输出
    本程序所需要方程式
    ax2 + bx + c = 0                   (3.1)
    的三个系数a,b,c。输出是这个方程式的所有根。
    3. 设计算法
    这个程序从整体上可以分为三大步,即输入,计算,输出
    Read the input data
    Calculate the roots
    Write out the roots
    我们现在把每一步进行逐步细化。这时判别式的值对程序的执行过程不产生影响。伪代码如下:
    Prompt the user for the coefficients a, b, and c.
    Read a, b, and c
    discriminant ← b^2 - 4 * a * c
    x1 ←( -b +sqrt(discriminant) ) / (2*a)
    x2 ←( -b -sqrt(discriminant) ) / (2*a)
    Print 'x1 = ' , real(x1), ' + i ', imag(x1)
    Print 'x2 = ' , real(x2), ' + i ', imag(x2)
    4. 将算法转化为MATLAB语句
    % Script file: calc_roots2.m
    %
    % Purpose:
    % This program solves for the roots of a quadratic equation
    % of the form a*x**2 + 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/06/98 S. J. Chapman Original code
    %
    % Define variables:
    % a -- Coefficient of x^2 term of equation
    % b -- Coefficient of x term of equation
    % c -- Constant term of equation
    % discriminant -- Discriminant of the equation
    % x1 -- First solution of equation
    % x2 -- Second solution of equation
    % Prompt the user for the coefficients of the equation
    disp ('This program solves for the roots of a quadratic ');
    disp ('equation of the form A*X^2 + B*X + C = 0. ');
    a = input ('Enter the coefficient A: ');
    b = input ('Enter the coefficient B: ');
    c = input ('Enter the coefficient C: ');
    % Calculate discriminant
    discriminant = b^2 - 4 * a * c;
    % Solve for the roots
    x1 = ( -b + sqrt(discriminant) ) / ( 2 * a );
    x2 = ( -b - sqrt(discriminant) ) / ( 2 * a );
    % Display results
    disp ('The roots of this equation are:');
    fprintf ('x1 = (%f) +i (%f)\n', real(x1), imag(x1));
    fprintf ('x2 = (%f) +i (%f)\n', real(x2), imag(x2));

    5.检测程序下一步,我们必须输入检测来检测程序。我们要有三组数据进行检测,其判别式分别大于0,等于0,小于0。根据方程式(3。1),用下面的方程式验证程序。
    x2 + 5x + 6 = 0             x= -2, x = -3
    x2 + 4x + 4 = 0             x= -2
    x2 + 2x + 5 = 0             x= -1 ± 2i
    我们把它们的系数分别输入程序,结果如下
    >> calc_root2
    This program solves for the roots of a quadratic
    equation of the form A*X^2 + B*X + C = 0.
    Enter the coefficient A: 1
    Enter the coefficient B: 5
    Enter the coefficient C: 6
    The roots of this equation are:
    x1 = (-2.000000) +i (0.000000)
    x2 = (-3.000000) +i (0.000000)
    >> calc_root2
    This program solves for the roots of a quadratic
    equation of the form A*X^2 + B*X + C = 0.
    Enter the coefficient A: 1
    Enter the coefficient B: 4
    Enter the coefficient C: 4
    The roots of this equation are:
    x1 = (-2.000000) +i (0.000000)
    x2 = (-2.000000) +i (0.000000)
    >> calc_root2
    This program solves for the roots of a quadratic
    equation of the form A*X^2 + B*X + C = 0.
    Enter the coefficient A: 1
    Enter the coefficient B: 2
    Enter the coefficient C: 5
    The roots of this equation are:
    x1 = (-1.000000) +i (2.000000)
    x2 = (-1.000000) +i (-2.000000)

    在三种不同的情况下,程序均给出了正确的结果。注意此程序与例3.2中的程序相比有多简单。复数数据的应用可大大简化我们的程序。



    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-5-21 07:01 , Processed in 0.443805 second(s), 51 queries .

    回顶部