作业帮 > 综合 > 作业

matlab 新手,想编一个切比雪夫多项式的程序,但运行不了,到底应该怎么改

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/18 11:00:44
matlab 新手,想编一个切比雪夫多项式的程序,但运行不了,到底应该怎么改
具体程序是:
function T=Chebyshev2(n)
syms x
T(1:n)=0;
T(1)=1;
T(2)=x;
for i=3:n
T(i)=2*x*T(i-1)-T(i-2);
end
T(n)
x=[-pi:0.01:pi];
plot(x,T(n))
运行结果是:
The following error occurred converting from sym to double:
Error using ==> sym.double at 29
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable,use the VPA function instead.
Error in ==> Chebyshev2 at 5
T(2)=x;
应该怎么改呢
问题:1、对T进行初始化要使用sym对象;2、plot的时候,要对符号表达式进行代入. 参考代码:function T=Chebyshev2(n)
syms x
T(1:n)=sym(0);
T(1)=1;
T(2)=x;
for i=3:n
    T(i)=2*x*T(i-1)-T(i-2);
end
T(n)
x=[-pi:0.01:pi];
plot(x,subs(T(n),x))调用Chebyshev2(10)的结果如图: