function [initw] = lineGred()
x = randi(10,20,2)
realw = [2 3]
y = x* realw'
initw = [1 1]
j = sum(power((x * initw' - y),2)) / size(x,1)
while j > 0.001
swapw = sum((x * initw' - y).*x) / size(x,1)
initw = initw - 0.01*swapw
j = sum(power((x * initw' - y),2)) / size(x,1)
end
disp(initw)
end
结果:
initw = [2.0087 2.9896]
多项式:
function [initw] = lineGredPow2()
realw = [2 3]
x1 = randi(10,20,1)
x2 = x1 .^2;
x = [x1,x2]
y = x* realw'
initw = [1 2]
j = sum(power((x * initw' - y),2)) / size(x,1)
while j > 0.001
swapw = sum((x * initw' - y).*x) / size(x,1)
initw = initw - 0.0001*swapw
j = sum(power((x * initw' - y),2)) / size(x,1)
end
end
结果:1.9787 3.0026
直接求解
function [initw] = normalLine()
x = randi(10,20,2)
realw = [2 3]
y = x* realw'
w = pinv(x'*x)*x'*y
end