数学实验-比较幂法和秦九韶算法的效率

附有完整代码,但不建议你直接copy

MATLAB安装移步

题目

编写MATLAB程序分别应用幂法和秦九韶算法求多项式的值,并比较分析两种算法的效率;

$$P_n(x)=a_0 x^n+a_1 x^{n-1}+\cdots+a_{n-1} x+a_n$$

分析

我们需要比较两种算法的效率,最简单的办法就是用两种算法计算同一个多项式,然后比较两者的时间差距,所以我们需要写两个函数,一个是幂法,一个是秦九韶,这两个函数用同样的输入,同时输出同样格式的结果比较就可以

两个函数我们实现的是输入多项式系数和x的值,通过计算和计时之后,返回结果和运算耗时。

耗时函数

$tic$是$MATLAB$中的计时函数,在$MATLAB$中,$tic$和$toc$函数一起使用,用来测量代码运行的时间。这对于性能分析,比如确定某个特定代码段的执行效率,是非常有用的。

$tic$函数:当你想开始计时时,调用$tic$。它会记录当前时间。

$toc$函数:在代码的结尾处调用$toc$,它会返回从最近一次调用$tic$以来经过的时间(以秒为单位)。

幂法

幂法其实就是最传统的求幂指数计算,在MATLAB中,^就是幂方,MATLAB中代码就可以这样写

$result$为计算结果,$time\_taken$为计算耗时,$coefficients$为主函数中传入的系数值,$x$就是多项式中的x

通过length获取多项式的n值,然后迭代循环,计算出幂值,相加

function [result, time_taken] = power_method(coefficients, x)
    tic;
    n = length(coefficients);
    result = 0;
    for i = 1:n
        result = result + coefficients(i) * x^(n-i);
    end
    time_taken = toc;
end

秦九韶算法

$$
P_n(x)=a_0 x^n+a_1 x^{n-1}+\cdots+a_{n-1} x+a_n
$$
将它改写成等价形式
$$
\begin{aligned}
P_n(x) & =\left(a_0 x^{n-1}+a_1 x^{n-2}+\cdots+a_{n-2} x+a_{n-1}\right) x+a_n \\
& =\left(\left(a_0 x^{n-2}+a_1 x^{n-3}+\cdots+a_{n-2}\right) x+a_{n-1}\right) x+a_n \\
& =\cdots=\left(\cdots\left(\left(a_0 x+a_1\right) x+a_2\right) x+\cdots+a_{n-1}\right) x+a_n,
\end{aligned}
$$

就可以得到递推公式
$$
\begin{aligned}
& v_0=a_0, \\
& v_k=x v_{k-1}+a_k, \quad k=1,2, \cdots, n .
\end{aligned}
$$

递推结果 $v_n=P_n(x)$.

我们就可以通过这个递推结果进行编写函数计算,和幂法同样的输入,不同的是我们需要把之前每一步的结果进行记录,然后带入下一次计算中

function [result, time_taken] = horner_method(coefficients, x)
    tic;
    n = length(coefficients);
    result = coefficients(1);
    for i = 2:n
        result = result * x + coefficients(i);
    end
    time_taken = toc;
end

主脚本

主脚本中我们只需要设置多项式系数值,和x的值,然后调用上面俩个函数即可

coefficients = [1, 48,468,468,684,648,4684,68,3, 2,]; % 多项式
x = 2; % value of x,

[result_power, time_power] = power_method(coefficients, x);
[result_horner, time_horner] = horner_method(coefficients, x);

disp(['幂法', num2str(result_power)]);
disp(['泰九韶: ', num2str(result_horner)]);

disp(['幂法时间: ', num2str(time_power), ' seconds']);
disp(['泰九韶时间: ', num2str(time_horner), ' seconds']);

代码

代码需要分三个文件来写,函数脚本注意命名

function [result, time_taken] = power_method(coefficients, x)
    tic;
    n = length(coefficients);
    result = 0;
    for i = 1:n
        result = result + coefficients(i) * x^(n-i);
    end
    time_taken = toc;
end
function [result, time_taken] = horner_method(coefficients, x)
    tic;
    n = length(coefficients);
    result = coefficients(1);
    for i = 2:n
        result = result * x + coefficients(i);
    end
    time_taken = toc;
end
coefficients = [1, 48,468,468,684,648,4684,68,3, 2,]; % 多项式
x = 2; % value of x,

[result_power, time_power] = power_method(coefficients, x);
[result_horner, time_horner] = horner_method(coefficients, x);

disp(['幂法', num2str(result_power)]);
disp(['泰九韶: ', num2str(result_horner)]);

disp(['幂法时间: ', num2str(time_power), ' seconds']);
disp(['泰九韶时间: ', num2str(time_horner), ' seconds']);
版权声明:除特殊说明,博客文章均为栋dong原创,依据CC BY-SA 4.0许可证进行授权,转载请附上出处链接及本声明。
如有需要,请在留言板留言,或者添加我的QQ或者微信
我只是一个学生,如有错误或者侵权,请联系我,谢!
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇