xi=-5:5;
yi=1./(1+25*x.*x);
polyfit(x,y,2);
y2=p(1)*x.^2+p(2)*x+p(3);
subplot(2,2,3);plot(x,y2);
hold on
polyfit(x,y,3);
y3=p(1)*x.^3+p(2)*x.^2+p(3)*x+p(4);
subplot(2,2,3);plot(x,y3);
hold on
polyfit(x,y,4);
y4=p(1)*x.^4+p(2)*x.^3+p(3)*x.^2+p(4)*x+p(5);
subplot(2,2,3);plot(x,y4);
hold on
polyfit(x,y,5);
y5=p(1)*x.^5+p(2)*x.^4+p(3)*x.^3+p(4)*x.^2+p(5)*x+p(6);
subplot(2,2,3);plot(x,y5);
hold on
polyfit(x,y,6);
y6=p(1)*x.^6+p(2)*x.^5+p(3)*x.^4+p(4)*x.^3+p(5)*x.^2+p(6)*x+p(7);
subplot(2,2,3);plot(x,y6);
hold on
polyfit(x,y,7);
y7=p(1)*x.^7+p(2)*x.^6+p(3)*x.^5+p(4)*x.^4+p(5)*x.^3+p(6)*x.^2+p(7)*x+p(8);
subplot(2,2,3);plot(x,y7);
hold on
polyfit(x,y,8);
y8=p(1)*x.^8+p(2)*x.^7+p(3)*x.^6+p(4)*x.^5+p(5)*x.^4+p(6)*x.^3+p(7)*x.^2+p(8)*x+p(9);
subplot(2,2,3);plot(x,y8);
hold on
polyfit(x,y,9);
y9=p(1)*x.^9+p(2)*x.^8+p(3)*x.^7+p(4)*x.^6+p(5)*x.^5+p(6)*x.^4+p(7)*x.^3+p(8)*x.^2+p(9)*x+p(10);
subplot(2,2,3);plot(x,y9);
hold on
polyfit(x,y,10);
y10=p(1)*x.^10+p(2)*x.^9+p(3)*x.^8+p(4)*x.^7+p(5)*x.^6+p(6)*x.^5+p(7)*x.^4+p(8)*x.^3+p(9)*x.^2+p(10)*x+p(11);
subplot(2,2,3);plot(x,y10);
hold on
chatgpt:对这个语音不熟悉,不对还请见谅
xi = -5:5;
yi = 1./(1+25*xi.*xi);
for n = 2:10
p = polyfit(xi, yi, n);
y = polyval(p, xi);
subplot(2,2,3);
plot(xi, y);
hold on;
end
这个代码首先定义了 xi 和 yi 数组,然后使用一个 for 循环来迭代多项式的次数,从 2 到 10。在每次循环中,使用 polyfit 函数拟合多项式,并使用 polyval 函数计算拟合的多项式值。最后,在 subplot 中绘制多项式曲线,并使用 hold on 函数保留绘图状态以便在下一次循环中使用。
不知道你这个问题是否已经解决, 如果还没有解决的话: