For practice purpose, I want to find minima of -humps() function.
I have written the following code but it's not giving correct answer
clear; clc;%function
f = @(x) -humps(x);dx = 0.1; %step length
x_current = 1; %starting guess
delta = 1e-4; %threshold value
alpha = 0.1; %finding optimal step length
g = inf; %starting gradient
while norm(g) > delta %gradient by finite difference
f1 = f(x_current + dx/2); f2 = f(x_current - dx/2); g = (f1-f2)/dx; x_next = x_current-alpha*g; %new solution
x_current = x_next; fprintf('%d %d\n',x_current,x_next); x_current = x_next;end
It give 5.543798e+01 as solution while the solution should either be 0.9 or 0.3 (local and global minimas, respectivily).
Whate am I missing here? can anyone help?
Best Answer