Last active
May 9, 2016 03:43
-
-
Save chaonan99/b1b50c5ee8d92421ba847977b1877eb7 to your computer and use it in GitHub Desktop.
运筹学的一次作业,用精确直线搜索的梯度法求解非线性规划问题。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function []=OR_HW1() | |
x=[0,0]; | |
dfx_val = dfx(x); | |
normdfx = norm(dfx_val); | |
k = 0; | |
f_val = zeros(1000,1); | |
while normdfx>1e-6 | |
t = newton(x,-dfx_val); | |
x = x-t*dfx_val; | |
dfx_val = dfx(x); | |
normdfx = norm(dfx_val); | |
k = k+1; | |
f_val(k) = f(x); | |
end | |
format long; | |
disp('最优解:');x | |
disp('最优函数值:');f(x) | |
fprintf('总迭代次数:%d\n',k); | |
plot((1:k),f_val(1:k));title('函数值与迭代次数关系图'); | |
end | |
function [val] = f(x) | |
val=(1-x(1))^2+2*(x(2)-x(1)^2)^2; | |
end | |
function [val] = dfx(x) | |
val(1)=-2*(1-x(1))-8*x(1)*(x(2)-x(1)^2); | |
val(2)=4*(x(2)-x(1)^2); | |
end | |
function [a] = newton(x,p) | |
syms t; | |
ff = (1-(x(1)+t*p(1)))^2+2*((x(2)+t*p(2))-(x(1)+t*p(1))^2)^2; | |
df = diff(ff); | |
phi = t-df/diff(df); | |
a=10;nexta=0; | |
while abs(a-nexta)>1e-4 | |
a=nexta; | |
nexta = double(subs(phi,t,a)); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment