function [ J ] = cost_chain( z ) % [Z,e] = fminsearch('cost_chain', -5*rand(5,1)) % ECE 463 Lecture #23: Calculus of Variations % Shape of a hanging chain that's 12 meters long Y = [0,z(1),z(2),z(3),z(4),z(5),z(4),z(3),z(2),z(1),0]'; PE = sum(Y); L = 0; for i=2:11 L = L + sqrt(1 + (Y(i) - Y(i-1))^2); end E = 12-L; J = PE + 1000*E*E; plot([0:10]', Y, '.-'); ylim([-5,1]); pause(0.01); end