# [MATLAB]多项式类加减乘除

3 篇文章 2 订阅

### 一、 问题描述

1、 用升幂排列的系数数组表示多项式，p(x)=a0+a1x +a2x2+a3x3表示为p=[a0，a1，a2，a3]；
2、 重载四个运算符+、-、*和\。以及定义积分和微分计算

### 三、 程序代码

classdef DocPolynom
% file: @DocPolynom/DocPolynom.m, define a polynomial class
% Public properties
%四则运算，导数，积分
properties
coef = [];   % double vector of polynomial coefficients [highest order ... lowest order]
end

% Class methods
methods
function obj = DocPolynom(c)
% Construct a DocPolynom object using the coefficients supplied
if isa(c,'DocPolynom')
obj.coef = c.coef;
else
obj.coef = c(:).';  % c(:) is always a column vector, no matter whether c is an array or matrix.
end     %把c变成一个列向量
obj.coef=fliplr(obj.coef);
end % DocPolynom

function c = double(obj)
c=double(obj.coef);
end % double

function s = char(obj)
% Created a formated display of the polynom
% as powers of x
obj.coef=fliplr(obj.coef);
if all(obj.coef == 0)
s = '0';
else
d = 0;  %修改d的初值
s = [];
for a = obj.coef;
if a ~= 0;
if ~isempty(s)
if a > 0
s = [s ' + '];
else
s = [s ' - '];
a = -a;
end
end
if a ~= 1 || d == 0
s = [s num2str(a)];
if d > 0
s = [s '*'];
end
end
if d >= 2
s = [s 'x^' int2str(d)];
elseif d == 1
s = [s 'x'];
end
end
d = d+1; %d递增
end
end
end % char

function disp(obj)
% DISPLAY
disp(['     ' char(obj)])
end % disp

function b = subsref(a,s)
% SUBSREF Implement obj([1 ...]),  a special subscripted assignment
switch s.type
case '()'
ind = s.subs{:};
for k = 1:length(ind)
b(k) = eval(strrep(char(a),'x',num2str(ind(k))));
end
case '.'
switch s.subs
case 'coef'
b = a.coef;
otherwise
error(['''' s.subs '''' ' is not a DocPolynom property'])
end
otherwise
error('Specify value for x as obj(x)')
end
end % subsref

function r = plus(obj1,obj2)
% PLUS  Implement obj1 + obj2 for DocPolynom
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
r = DocPolynom([obj1.coef zeros(1,k) ] + [obj2.coef zeros(1,-k)]);
end % plus

function r = mtimes(obj1,obj2)
% MTIMES   Implement obj1 * obj2 for DocPolynoms.
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
s1=length(obj1.coef);
s2=length(obj2.coef);
new=zeros(s1+s2-1,1);
%             r = DocPolynom(conv(obj1.coef,obj2.coef));  %conv返回卷积
for i=1:s1+s2-1
for j=1:s1
for k=1:s2
if (j+k-1)==i
new(i)=new(i)+obj1.coef(j)*obj2.coef(k);
end
end
end
end
r=DocPolynom(new);
end % mtimes

function r = minus(obj1,obj2)
% PLUS  Implement obj1 - obj2 for DocPolynom
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
r = DocPolynom([obj1.coef zeros(1,k)] - [obj2.coef zeros(1,-k)]);
end % minus

function r = mrdivide(obj1,obj2)
% MRDIVIDE  Implement obj1 / obj2 for DocPolynom
obj1 = DocPolynom(obj1);
obj1.coef=fliplr(obj1.coef);
obj2 = DocPolynom(obj2);
obj2.coef=fliplr(obj2.coef);
s1=length(obj1.coef);
s2=length(obj2.coef);
if s1<s2
new=0;
else
new=zeros(s1-s2+1,1);
for i=1:s1-s2+1
index=find(obj1.coef);
if length(index)>0
m=index(1);
else
break;
end
if s1-m+1<s2
break;
end
new(i)=obj1.coef(m)/obj2.coef(1);
rs=zeros(1,s1);
for k=m:m+s2-1
rs(k)=obj2.coef(k-m+1);
end
obj1.coef=obj1.coef-new(i)*rs;
end
end
newnew=fliplr(new');
r=DocPolynom(newnew);

%          r = DocPolynom(fliplr(deconv(obj1.coef,obj2.coef)));  %conv返回去卷积

end % mrdivide

function r=int(obj1)
%integral
obj1 = DocPolynom(obj1);
n=length(obj1.coef);
new_n=ones(n+1,1);
for i=2:n+1
new_n(i)=obj1.coef(i-1)/(i-1);
end
r=DocPolynom(new_n);
end

function r=diff(obj1)
%differential
obj1 = DocPolynom(obj1);
n=length(obj1.coef);
new_n=zeros(n-1,1);
for i=2:n
new_n(i)=obj1.coef(i)*(i-1);
end
r=DocPolynom(new_n);
end

function y = polyval(obj,x)
% POLYVAL  POLYVAL(obj,x) evaluates obj at the points x.
y = 0;
for a = obj.coef
y = y.*x + a;
end
end % polyval
end % methods

end % classdef

clear;
clc;
p1=DocPolynom([1,2,3]);
disp('第一个多项式:')
disp(p1)
p2=DocPolynom([0,1,2,3]);
disp('第二个多项式:')
disp(p2)
disp('多项式的和:');
disp(p1+p2)
disp('多项式的差:');
disp(p1-p2)
disp('多项式的商:');
disp(p2/p1)
disp('多项式的积:');
disp(p1*p2)
disp('多项式积分:');
p1=int(p1);
disp(p1)
disp('多项式微分:');
p1=diff(p1);
disp(p1)


### 五、 实验心得

1. 对于新的概念注意学习已有的模板
2. 对于复杂的事物必须抓住事物的关键，由浅入深
3. 类操作是面向对象的编程，一个类中首先定义变量，随后定义各类方法，定义一个类后，可以用定义的各种方法直接操作变量，在某些情况下，能够大大的提高效率。
• 4
点赞
• 10
收藏
觉得还不错? 一键收藏
• 0
评论
11-22 3091
04-16 382
03-20 1万+
05-30 4万+
07-07 2045
12-25
09-23 1702
03-22 1222

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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