codingt 2022-09-30 14:24 采纳率: 100%
浏览 33
已结题

matlab开发一个类

img


怎么弄这个类呢?
弄了好久都没弄出来
心疼自己的头发
头发要掉光了

  • 写回答

2条回答 默认 最新

  • slandarer MATLAB领域优质创作者 2022-10-01 17:35
    关注

    工具函数:

    classdef polynom
        properties
            coe
        end
        methods
            function obj=polynom(coe)
                obj.coe=coe;
            end
    % =========================================================================
    % 输出 --------------------------------------------------------------------
            function disp(obj)
                if all(obj.coe==0)
                disp('0')
                else
                baseStr=char(32.*ones([1,length(obj.coe)*10]));
                for i=1:length(obj.coe)
                    tStr='';
                    if obj.coe(i)~=0
                        if obj.coe(i)>0
                            oStr='+';
                        else
                            oStr='';
                        end
                        nStr=num2str(obj.coe(i));
                        if i~=length(obj.coe)
                        if str2double(nStr)==1,nStr='';end
                        if str2double(nStr)==-1,nStr='-';end
                        end
                        if i==(length(obj.coe)-1)
                            tStr=[oStr,nStr,'x'];
                        else
                            tStr=[oStr,nStr,'x^',num2str(length(obj.coe)-i)];
                        end
                    end
                    ind=find(abs(baseStr)==32,1);
                    baseStr(ind:ind+length(tStr)-1)=tStr;
                end
                baseStr(abs(baseStr)==32)=[];
                if strcmp(baseStr(end-2:end),'x^0'),baseStr(end-2:end)=[];end
                if strcmp(baseStr(1),'+'),baseStr(1)=[];end
                disp(baseStr)
                end
            end
    % 加法 --------------------------------------------------------------------
            function obj=plus(obj,arg)
                if isa(arg,'double')
                    tcoe=arg;
                else
                    tcoe=arg.coe;
                end
                L1=length(obj.coe);
                L2=length(tcoe);
                baseCoe=zeros([1,max(L1,L2)+1]);
                baseCoe(end+1-L1:end)=baseCoe(end+1-L1:end)+obj.coe;
                baseCoe(end+1-L2:end)=baseCoe(end+1-L2:end)+tcoe;
                obj.coe=baseCoe(find(baseCoe~=0,1):end);
            end
    % 减法 --------------------------------------------------------------------
            function obj=minus(obj,arg)
                if isa(arg,'double')
                    tcoe=arg;
                else
                    tcoe=arg.coe;
                end
                L1=length(obj.coe);
                L2=length(tcoe);
                baseCoe=zeros([1,max(L1,L2)+1]);
                baseCoe(end+1-L1:end)=baseCoe(end+1-L1:end)+obj.coe;
                baseCoe(end+1-L2:end)=baseCoe(end+1-L2:end)-tcoe;
                obj.coe=baseCoe(find(baseCoe~=0,1):end);
            end
            function obj=uminus(obj)
                obj.coe=-obj.coe;
            end
    % 乘法 --------------------------------------------------------------------
            function obj=mtimes(obj,arg)
                L1=length(obj.coe);
                L2=length(arg.coe);
                tmat=zeros(L2,L1+L2-1);
                for i=1:L2
                    tmat(i,i:(i+L1-1))=arg.coe(i).*obj.coe;
                end
                obj.coe=sum(tmat);
            end
    % 带余除法 -----------------------------------------------------------------
            function [obj1,obj2]=mrdivide(obj,arg)
                L1=length(obj.coe);
                L2=length(arg.coe);
    
                tCoe=obj.coe;
                obj1=polynom(0);
                for i=1:(L1+1-L2)
                    obj1.coe(i)=1./(arg.coe(1)).*(tCoe(1));
                    tCoe(1:L2)=tCoe(1:L2)-arg.coe.*obj1.coe(i);
                    tCoe(1)=[];
                end
                obj2=polynom(tCoe);
            end
    
    % 数值计算 -----------------------------------------------------------------
            function value=val(obj,x)
                value=x.^(length(obj.coe)-1:-1:0)*(obj.coe.');
            end
    % 积分 --------------------------------------------------------------------
            function output=int(obj,a,b)
                if nargin<2
                    output=polynom([obj.coe./(length(obj.coe):-1:1),0]);
                else
                    tcoe=[obj.coe./(length(obj.coe):-1:1),0];
                    output=b.^(length(tcoe)-1:-1:0)*(tcoe.')-...
                           a.^(length(tcoe)-1:-1:0)*(tcoe.');
                end
            end
    % 求导 --------------------------------------------------------------------
            function obj=diff(obj)
                obj.coe=obj.coe.*(length(obj.coe)-1:-1:0);
                obj.coe(end)=[];
            end
    % 比较 --------------------------------------------------------------------
            function bool=eq(obj,arg)
                if length(obj.coe)~=length(arg.coe)
                    bool=false;
                else
                    bool=all(obj.coe==arg.coe);
                end
            end
            function bool=ne(obj,arg)
                if length(obj.coe)~=length(arg.coe)
                    bool=true;
                else
                    bool=~all(obj.coe==arg.coe);
                end
            end
        end
    end
    

    运行示例:

    fprintf('\n创建:a(x)=')
    a=polynom([3,4,1,0]);
    disp(a)
    
    fprintf('\n创建:b(x)=')
    b=polynom([3,2]);
    disp(b)
    
    fprintf('\n加法:a(x)+b(x)=')
    disp(a+b)
    
    fprintf('\n加法:a(x)+5x+6=')
    disp(a+[5,6])
    
    fprintf('\n减法:a(x)-b(x)=')
    disp(a-b)
    
    fprintf('\n相反数:-a(x)=')
    disp(-a)
    
    fprintf('\n乘法:a(x)*b(x)=')
    disp(a*b)
    
    fprintf('\n除法:a(x)/b(x)')
    [c,d]=a/b;
    fprintf('\n商=')
    disp(c)
    fprintf('余数=')
    disp(d)
    
    fprintf('\n积分:int(a(x))=')
    disp(int(a))
    
    fprintf('\n求导:diff(a(x))=')
    disp(diff(a))
    
    fprintf('\n数值计算:a(5)=')
    disp(val(a,5))
    
    fprintf('数值积分:int(a(x),0,1)=')
    disp(int(a,0,1))
    
    fprintf('比较:a(x)==b(x)=')
    disp(a==b)
    
    fprintf('比较:a(x)~=b(x)=')
    disp(a~=b)
    

    运行结果:

    创建:a(x)=3x^3+4x^2+x

    创建:b(x)=3x+2

    加法:a(x)+b(x)=3x^3+4x^2+4x+2

    加法:a(x)+5x+6=3x^3+4x^2+6x+6

    减法:a(x)-b(x)=3x^3+4x^2-2x-2

    相反数:-a(x)=-3x^3-4x^2-x

    乘法:a(x)*b(x)=9x^4+18x^3+11x^2+2x

    除法:a(x)/b(x)
    商=x^2+0.66667x-0.11111
    余数=0.22222

    积分:int(a(x))=0.75x^4+1.3333x^3+0.5x^2

    求导:diff(a(x))=9x^2+8x+1

    数值计算:a(5)= 480

    数值积分:int(a(x),0,1)= 2.5833

    比较:a(x)==b(x)= 0

    比较:a(x)~=b(x)= 1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 9月30日

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题