poster
2019-12-07, 01:00
我有这个硬编码版本,可将数据拟合为线性,二次和三次多项式的曲线:
对于一些数据x和一个函数y
M=[x.^0 x.^1]; L=[x.^0 x.^1 x.^2]; linear = (M'*M)\(M'*y); plot(x, linear(1)+linear(2)*x, ';linear;r'); deg2 = (L'*L)\(L'*y); plot(x, deg2(1)+deg2(2)*x+deg2(3)*(x.*x), ';quadratic plot;b'); 我想知道如何将其转换为for循环以绘制n次多项式的曲线?我卡住的部分是绘图部分,如何将系数数量的增加转换为for循环?
是)我有的:
for i = 1:5 % say we're trying to plot curves up to degree 5 polynomials... curr=x.^(0:i); degI = (curr'*curr)\(curr'*y); plot(x, ???) % what goes in here
对于一些数据x和一个函数y
M=[x.^0 x.^1]; L=[x.^0 x.^1 x.^2]; linear = (M'*M)\(M'*y); plot(x, linear(1)+linear(2)*x, ';linear;r'); deg2 = (L'*L)\(L'*y); plot(x, deg2(1)+deg2(2)*x+deg2(3)*(x.*x), ';quadratic plot;b'); 我想知道如何将其转换为for循环以绘制n次多项式的曲线?我卡住的部分是绘图部分,如何将系数数量的增加转换为for循环?
是)我有的:
for i = 1:5 % say we're trying to plot curves up to degree 5 polynomials... curr=x.^(0:i); degI = (curr'*curr)\(curr'*y); plot(x, ???) % what goes in here