Matlab 根据系数输出多项式

print.m :

function [s t]=print(a) 
s='';
t='';
len=length(a);
if (len==0)
    errordlg('矩阵不能为空');
else
    for b=1:len
        if b~=len&&b~=len-1
            if a(b)<0 && a(b)~=-1
                s=strcat(s,num2str(a(b)),'x^',num2str(len-b));
                t=strcat(t,num2str(a(b)),'*x.^',num2str(len-b));
            elseif a(b)==-1
                s=strcat(s,'-','x^',num2str(len-b));
                t=strcat(t,'-','x.^',num2str(len-b));
            elseif a(b)>0 && a(b)~=1
                if b==1
                    s=strcat(s,num2str(a(b)),'x^',num2str(len-b));
                    t=strcat(t,num2str(a(b)),'*x.^',num2str(len-b));
                else
                s=strcat(s,'+',num2str(a(b)),'x^',num2str(len-b));
                t=strcat(t,'+',num2str(a(b)),'*x.^',num2str(len-b));
                end
            elseif a(b)==1
                if b==1
                  s=strcat(s,'x^',num2str(len-b));
                  t=strcat(t,'x.^',num2str(len-b));
                else 
                    s=strcat(s,'+','x^',num2str(len-b));
                    t=strcat(t,'+','x.^',num2str(len-b));
                end
            else
            end
        elseif b==len
            if a(b)<0 && a(b)~=-1
                s=strcat(s,num2str(a(b)));
                t=strcat(t,num2str(a(b)));
            elseif a(b)==-1
                s=strcat(s,'-1');
                t=strcat(t,'-1');
            elseif a(b)>0 && a(b)~=1
                s=strcat(s,'+',num2str(a(b)));
                t=strcat(t,'+',num2str(a(b)));
            elseif a(b)==1
                  s=strcat(s,'+1');
                  t=strcat(t,'+1');
            else
            end
        else
             if a(b)<0 && a(b)~=-1
                s=strcat(s,num2str(a(b)),'x');
                t=strcat(t,num2str(a(b)),'*x');
            elseif a(b)==-1
                s=strcat(s,'-','x');
                t=strcat(t,'-','x');
            elseif a(b)>0 && a(b)~=1
                if b==1
                    s=strcat(s,num2str(a(b)),'x');
                    t=strcat(t,num2str(a(b)),'x');
                else
                s=strcat(s,'+',num2str(a(b)),'x');
                t=strcat(t,'+',num2str(a(b)),'*x');
                end
            elseif a(b)==1
                if b==1
                  s=strcat(s,'x');
                  t=strcat(t,'x');
                else 
                    s=strcat(s,'+','x');
                    t=strcat(t,'+','x');
                end
            else
            end
        end
    end
end
end

main.m :

clear all;
clc;
x=-10:1:10;
a=[-2 -10 0 5 0 -1 -17];
[m n]=print(a)
y=eval(n);
figure(1);
plot(x,y,'LineWidth',3,'Marker','diamond','MarkerEdgeColor','k', ...
 'MarkerFaceColor','y','MarkerSize',2,'Color','r');
title('多项式');
xlabel('X');
ylabel(m);
legend(m);

运行结果:


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值