horner算法matlab实现,使用Euclid和Horner算法的多项式求值

icon1.gif 使用Euclid和Horner算法的多项式求值

我正在编写这段代码,用于评估图像1中的多项式

我的代码是

function [px] = HornerEuclid(index,b,x) %write a function HornerEuclid which evaluates a %polynomial using the improved sparse Horner scheme. implementing %previously defined functions PolyEuclid, HornerSparse and FastPower g=PolyEuclid(index); y=FastPower(x,g); k=length(index); if ~isempty(index) while g~=0 for j=1:k index(j)=index(j)/g; end break end px=HornerSparse(index,b,y); elseif isinteger(x) px=[0]; else px=(zeros(size(x))); end HornerSparse的代码是

function [px] =HornerSparse(index,b,x) %evaluates a polynomial p(x) using the Horner scheme given %in Eq. (3). %The function should return the value of the polynomial for a given x %and lists i, b. if isinteger(x) || ismatrix(x) if ~isempty(b) && ~isempty(index) k=length(b); if k>1 px=b(k)*eye(size(x)); for j=k:-1:2 px= b(j-1)*eye(size(x))+ px*FastPower(x,index(j)-index(j-1)); end else px=b(k)*FastPower(x,index(k)); end elseif isinteger (x) px=[0]; else px=(zeros(size(x))); end else error('input values must be integers or square matrices') end 而PolyEuclid的代码是

function [gcd] = PolyEuclid(r) %calculates the greatest common divisor of a list r =[r1, r2, . . . , rm] %of n integer numbers using Algorithm 2. gcd=r(1); for j=2:length(r) gcd=Euclid(gcd,r(j)); end 它使用另一个函数Euclid查找2个数字的gcd,而FastPower(x,n)查找x ^ n。

测试图像2中显示的HornerEuclid代码时,出现以下错误消息,但我不知道如何解决。 HornerSparse和PolyEuclid这两个代码都是正确的。

更多&回答...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值