Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [x,fx] = plot_minimum(f,a,b,epsilon,use_ginput)
- X = linspace(a,b,500);
- hold('off')
- plot(X, f(X), 'b-')
- hold('on')
- axis("manual")
- if use_ginput
- a_vector = ginput(1);
- plot(a_vector(1),a_vector(2), 'xk')
- b_vector = ginput(1);
- plot(b_vector(1),b_vector(2), 'xk')
- a = a_vector(1);
- b = b_vector(1);
- else
- a = -4;
- b = 2;
- end
- fprintf('a = %.2f\nb = %.2f\n', a,b)
- if a > b
- disp('Digga, der erste Punkt muss LINKS sein!!!')
- end
- [x,fx] = find_minimum(f,a,b,epsilon);
- plot(x,fx,'or')
- hold('off')
- end
- function [x,fx] = find_minimum(f,a,b,epsilon)
- c = (3-sqrt(5))/2;
- xa = a + c*(b-a);
- xb = b - c*(b-a);
- not_first_time = false;
- while abs(a-b) >= epsilon
- if f(xa) < f(xb)
- b = xb;
- xb = xa;
- xa = a + c*(b-a);
- elseif f(xa) > f(xb)
- a = xa;
- xa = xb;
- xb = b - c*(b-a);
- end
- fprintf('a = %f\nx_a = %f\nx_b = %f\nb = %f\n--------\n',a,xa,xb,b)
- hold('on')
- if not_first_time
- delete(a_plot)
- delete(xa_plot)
- delete(xb_plot)
- delete(b_plot)
- end
- a_plot = plot(a,f(a),'mo');
- xa_plot = plot(xa,f(xa), 'mo');
- xb_plot = plot(xb,f(xb), 'mo');
- b_plot = plot(b,f(b), 'mo');
- not_first_time = true;
- pause(1.5)
- end
- x = xa;
- fx = f(xa);
- end
- f=@(x) 10+x.^2-10*cos(2*pi*x); a=-10; b=10; epsilon=1e-5; plot_minimum(f,a,b,epsilon,true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement