我使用MatLab为Horner的算法编写以下代码
function [answer ] = Simple( a,x )
%Simple takes two arguments that are in order and returns the value of the
%polynomial p(x). Simple is called by typing Simple(a,x)
% a is a row vector
%x is the associated scalar value
n=length(a);
result=a(n);
for j=n-1:-1:1 %for loop working backwards through the vector a
result=x*result+a(j);
end
answer=result;
end
Run Code Online (Sandbox Code Playgroud)
我现在需要添加错误检查以确保调用者在行向量a中使用整数值.
对于我以前使用的整数检查
if(n~=floor(n))
error(...
Run Code Online (Sandbox Code Playgroud)
但这是一个单一的值,我不确定如何检查a中的每个元素.
matlab ×1