Matroids Matheplanet Forum Index
Moderiert von mire2
Mathematische Software & Apps » Matlab » Ausführen von Code Optimierungsalgorithmus
Autor
Universität/Hochschule Ausführen von Code Optimierungsalgorithmus
Aliaju
Junior Letzter Besuch: vor mehr als 3 Monaten
Dabei seit: 25.04.2019
Mitteilungen: 13
  Themenstart: 2019-09-17

Hallo, zur Minimierung der Rosenbrockfunktion $F(x,y)=100(y-x^2)^2+(^-x)^2$ mit Startpunkt $x=(1.2,1.2)$ mit Gradientenverfahren gibt es unten stehenden Code. Da ich immer mit anderen Programmiersprachen programmiere und nie mit Matlab weiß ich gar nicht wie ich den Code ausführen kann. Z.B. weiß ich auch nicht, wie ich eine Funktion mit zwei Variablen eintippen kann und deren Gradienten. Ich hab schon mehrere Sachen probiert und teilweise haben Sachen funktioniert, wie die Erkennung einer Funktion, die zwei Variablen hat. Aber insgesamt (mit dem Gradienten zb) und der der Ausführung des Codes hat es leider gar nicht funktioniert. Außerdem sagt mir mein Programm auch zb bei dem Code der function B_k, dass s_k' wegen des Simikolons ein "invalid character" sei. Vielleicht brauche ich den Teil des Codes aber auch gar nicht, um das Gradientenverfahren auf die Rosenbrockfunktion anzuwenden? Kann mir bitte jemand weiterhelfen und mir 1. sagen, was ich beispielsweise in die Kommandozeile tippen kann, um das Programm auszuführen? und 2. sagen, wie ich das Problem mit dem Zeichen" ' " lösen kann, falls ich den Teil des Codes benötige? Ich würde den Code echt gerne ausführen... :( \sourceon Matlab function [min, xhist, steplength] = ... descentdirect(f, grad_f, hessian_f, x0, maxit, tol, dir, line) %-------------------------------------------------------------------------- % General descent direction method %-------------------------------------------------------------------------- % f: object function % grad_f: gradient of f % hessian_f: hessian matrix of f %-------------------------------------------------------------------------- %-------------------------------------------------------------------------- % Parameter %-------------------------------------------------------------------------- c_1 = 10^-4; c_2 = 0.9; %c_1 = 0.4; c_2 = 0.6; alpha_max = 16; %-------------------------------------------------------------------------- % Initialization %-------------------------------------------------------------------------- i = 1; x_k = x0; stop = 1; xsize = length(x0); B_k = eye(xsize); xhist = zeros(xsize,maxit); xhist(:,i) = x_k; steplength = zeros(maxit,1); %-------------------------------------------------------------------------- while stop && i < maxit %-------------------------------------------------------------------- % Search Direction %-------------------------------------------------------------------- if (dir == 1) % Steepest descent direction p_k = steepdir(grad_f,x_k); elseif (dir == 2) % Newton’s direction p_k = newtondir(grad_f,hessian_f,x_k); elseif (dir == 3) % Quasi-Newton direction p_k = qnewtondir(grad_f,B_k,x_k); end %-------------------------------------------------------------------- % Step Length %-------------------------------------------------------------------- if (line == 1) % No stepsize control alpha = 1; elseif (line == 2) % Wolfe condition alpha = linesearch(f, grad_f, p_k, x_k, c_1, c_2, alpha_max); end steplength(i) = alpha; %-------------------------------------------------------------------- % Update %-------------------------------------------------------------------- x_old = x_k; x_k = x_k + alpha * p_k; i = i + 1; xhist(:,i) = x_k; if (norm(grad_f(x_k)) < tol) || (norm(x_k - x_old) < 1e-12) stop = 0; end %-------------------------------------------------------------------- % Updating Quasi-Newton matrix %-------------------------------------------------------------------- if (dir == 3) % Quasi-Newton direction B_k = bfgs(x_k,x_old,B_k,grad_f); end end min = x_k; steplength = steplength(1:i); xhist = xhist(:,1:i); \sourceoff \sourceon Matlab %-------------------------------------------------------------------- function p_k = steepdir(grad_f,x_k) % Steepest descent direction p_k = - feval(grad_f, x_k); %-------------------------------------------------------------------- function p_k = newtondir(grad_f,hessian_f,x_k) % Newton’s direction grad_f_k = feval(grad_f, x_k); hessian_f_k = feval(hessian_f, x_k); p_k = - hessian_f_k\grad_f_k; %-------------------------------------------------------------------- function p_k = qnewtondir(grad_f,B_k,x_k) % Quasi-Newton’s direction grad_f_k = feval(grad_f, x_k); p_k = -B_k\grad_f_k; %-------------------------------------------------------------------- function B_k = bfgs(x_k,x_old,B_k,grad_f) % BFGS s_k = x_k - x_old; y_k = feval(grad_f, x_k) - feval(grad_f, x_old); if y_k’ * s_k <= 0 return end B_k = B_k - (B_k * s_k * s_k’ * B_k) / (s_k’ * B_k * s_k) + ... (y_k* y_k’) / (y_k’ * s_k); %-------------------------------------------------------------------- function B_k = sr1(x_k,x_old,B_k,grad_f) % SR1 s_k = x_k - x_old; y_k = feval(grad_f, x_k) - feval(grad_f, x_old); if y_k’ * s_k <= 0 return end B_k = B_k + ((y_k - B_k * s_k) * (y_k - B_k *s_k)’) / ... (y_k - B_k * s_k)’*s_k; \sourceoff


   Profil
majoka
Senior Letzter Besuch: in der letzten Woche
Dabei seit: 25.02.2014
Mitteilungen: 810
  Beitrag No.1, eingetragen 2019-09-17

Du kannst den Code in etwa so aufrufen \sourceon Matlab clear all b = 100; a = 1; F = @(x) (a-x(1)).^2 + b*(x(2)-x(1).^2).^2; x0 = [2.2;2.2]; grad_F = @(x) [2*(a-x(1))*(-1) + 2*b*(x(2)-x(1).^2)*(-2*x(1)); 2*b*(x(2)-x(1).^2)]; % [min, xhist, steplength] = descentdirect(f, grad_f, hessian_f, x0, maxit, tol, dir, line) [min, xhist, steplength] = descentdirect(F, grad_F, [], x0, 100000, 1e-15, 3, 1) min \sourceoff Ich erhalte damit das Ergebnis von der Wiki-Seite. Allerdings scheinen in dem Code aus dem Themenstart noch Formatierungsfehler zu sein. (Speichere das folgende in ein File mit dem Namen "descentdirect.m" ab.) \sourceon Matlab function [min, xhist, steplength] = descentdirect(f, grad_f, hessian_f, x0, maxit, tol, dir, line) %-------------------------------------------------------------------------- % General descent direction method %-------------------------------------------------------------------------- % f: object function % grad_f: gradient of f % hessian_f: hessian matrix of f %-------------------------------------------------------------------------- %-------------------------------------------------------------------------- % Parameter %-------------------------------------------------------------------------- c_1 = 10^-4; c_2 = 0.9; %c_1 = 0.4; c_2 = 0.6; alpha_max = 16; %-------------------------------------------------------------------------- % Initialization %-------------------------------------------------------------------------- i = 1; x_k = x0; stop = 1; xsize = length(x0); B_k = eye(xsize); xhist = zeros(xsize,maxit); xhist(:,i) = x_k; steplength = zeros(maxit,1); %-------------------------------------------------------------------------- while stop && i < maxit %-------------------------------------------------------------------- % Search Direction %-------------------------------------------------------------------- if (dir == 1) % Steepest descent direction p_k = steepdir(grad_f,x_k); elseif (dir == 2) % Newton’s direction p_k = newtondir(grad_f,hessian_f,x_k); elseif (dir == 3) % Quasi-Newton direction p_k = qnewtondir(grad_f,B_k,x_k); end %-------------------------------------------------------------------- % Step Length %-------------------------------------------------------------------- if (line == 1) % No stepsize control alpha = 1; elseif (line == 2) % Wolfe condition alpha = linesearch(f, grad_f, p_k, x_k, c_1, c_2, alpha_max); end steplength(i) = alpha; %-------------------------------------------------------------------- % Update %-------------------------------------------------------------------- x_old = x_k; x_k = x_k + alpha * p_k; i = i + 1; xhist(:,i) = x_k; if (norm(grad_f(x_k)) < tol) || (norm(x_k - x_old) < 1e-12) stop = 0; end %-------------------------------------------------------------------- % Updating Quasi-Newton matrix %-------------------------------------------------------------------- if (dir == 3) % Quasi-Newton direction B_k = bfgs(x_k,x_old,B_k,grad_f); end end min = x_k; steplength = steplength(1:i); xhist = xhist(:,1:i); %-------------------------------------------------------------------- function p_k = steepdir(grad_f,x_k) % Steepest descent direction p_k = - feval(grad_f, x_k); %-------------------------------------------------------------------- function p_k = newtondir(grad_f,hessian_f,x_k) % Newton’s direction grad_f_k = feval(grad_f, x_k); hessian_f_k = feval(hessian_f, x_k); p_k = - hessian_f_k\grad_f_k; %-------------------------------------------------------------------- function p_k = qnewtondir(grad_f,B_k,x_k) % Quasi-Newton’s direction grad_f_k = feval(grad_f, x_k); p_k = -B_k\grad_f_k; %-------------------------------------------------------------------- function B_k = bfgs(x_k,x_old,B_k,grad_f) % BFGS s_k = x_k - x_old; y_k = feval(grad_f, x_k) - feval(grad_f, x_old); if y_k' * s_k <= 0 return end B_k = B_k - (B_k * s_k * s_k' * B_k) / (s_k' * B_k * s_k) + ... (y_k* y_k') / (y_k' * s_k); %-------------------------------------------------------------------- function B_k = sr1(x_k,x_old,B_k,grad_f) % SR1 s_k = x_k - x_old; y_k = feval(grad_f, x_k) - feval(grad_f, x_old); if y_k' * s_k <= 0 return end B_k = B_k + ((y_k - B_k * s_k) * (y_k - B_k *s_k)') / ... (y_k - B_k * s_k)'*s_k; \sourceoff


   Profil
Aliaju hat die Antworten auf ihre/seine Frage gesehen.

Wechsel in ein anderes Forum:
 Suchen    
 
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest © 2001-2023 by Matroids Matheplanet
This web site was originally made with PHP-Nuke, a former web portal system written in PHP that seems no longer to be maintained nor supported. PHP-Nuke is Free Software released under the GNU/GPL license.
Ich distanziere mich von rechtswidrigen oder anstößigen Inhalten, die sich trotz aufmerksamer Prüfung hinter hier verwendeten Links verbergen mögen.
Lesen Sie die Nutzungsbedingungen, die Distanzierung, die Datenschutzerklärung und das Impressum.
[Seitenanfang]