《MATLAB7.0从入门到精通》中有个str2poly函数用于把多项式的字符串表示转换为行向量表示,输入为 X=’-10x^2+8x^2-x^3+4x^5+x^3-10+9-x^7'时运算正确,若输入为X='2x^2+3x+4'则运算不正确,求解
coefficient=[];
power=[];
for i=1:L+1,
substring=char(term(i))
index2=regexp(substring,'x\^')
if(isempty(index2)==0),
if(index2(1)==1),
coefficient=[coefficient 1]
power=[power str2num(substring((index2(1)+2):end))]
elseif(index2(1)==2),
if(substring(1)=='+'),
coefficient=[coefficient 1]
power=[power str2num(substring((index2(1)+2):end))]
elseif(substring(1)=='-'),
coefficient=[coefficient -1]
power=[power str2num(substring((index2(1)+2):end))]
end;
else
coefficient=[coefficient str2num(substring(1:(index2(1)-1)))]
power=[power str2num(substring((index2+2):end))]
end;
这是其中的一部分程序
matlab中,一个函数有时运算正确有时运算错误
答案:1 悬赏:40 手机版
解决时间 2021-02-21 15:49
- 提问者网友:回忆在搜索
- 2021-02-20 15:49
最佳答案
- 五星知识达人网友:大漠
- 2021-02-20 16:59
syms x;
X1 = -10*x^2+8*x^2-x^3+4*x^5+x^3-10+9-x^7;
X2 = 2*x^2+3*x+4;
a1 = sym2poly(X1)
a2 = sym2poly(X2)
a1 =
-1 0 4 0 0 -2 0 -1
a2 =
2 3 4
X1 = -10*x^2+8*x^2-x^3+4*x^5+x^3-10+9-x^7;
X2 = 2*x^2+3*x+4;
a1 = sym2poly(X1)
a2 = sym2poly(X2)
a1 =
-1 0 4 0 0 -2 0 -1
a2 =
2 3 4
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯