1 / 31

Optimization

Optimization. MATLAB Exercises Assoc. Prof. Dr. Pelin GÜNDEŞ. Introduction to MATLAB. Introduction to MATLAB. Introduction to MATLAB. % All text after the % sign is a comment. MATLAB ignores anything to the right of the % sign.

fordl
Download Presentation

Optimization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Optimization MATLAB Exercises Assoc. Prof. Dr. Pelin GÜNDEŞ

  2. Introduction to MATLAB

  3. Introduction to MATLAB

  4. Introduction to MATLAB • % All text after the % sign is a comment. MATLAB ignores anything to the right of the % sign. • ; A semicolon at the end of a line prevents MATLAB from echoing the information you enter on the screen • … A succession of three periods at the end of the line informs MATLAB that code will continue to the next line. You can not split a variable name across two lines. You can not continue a comment on another line.

  5. Introduction to MATLAB • ^c You can stop MATLAB execution and get back the command prompt by typing ^c (Ctrl-C) – by holding down ‘Ctrl’ and ‘c’ together. • help command_name • helpwin Opens a help text window that provides more information on all of the MATLAB resources installed on your system. • helpdesk Provides help using a browser window.

  6. Introduction to MATLAB • = The assignment operator. The variable on the left-hand side of the sign is assigned the value of the right-hand side. • == Within a if construct • MATLAB is case sensitive. An a is different than A. All built-in MATLAB commands are in lower case. • MATLAB does not need a type definition or dimension statement to introduce variables.

  7. Introduction to MATLAB • Variable names start with a letter and contain up to 31 characters (only letters, digits and underscore). • MATLAB uses some built in variable names. Avoid using built in variable names. • Scientific notation is expressed with the letter e, for example, 2.0e-03, 1.07e23, -1.732e+03. • Imaginary numbers use either i or j as a suffix, for example 1i, -3.14j,3e5i

  8. Introduction to MATLAB Arithmetic Operators • + Addition • - Subtraction • * Multiplication • / Division • ^ Power • ‘ Complex conjugate transpose (also array transpose)

  9. Introduction to MATLAB • In the case of arrays, each of these operators can be used with a period prefixed to the operator, for example, (.*) or (.^) or (./). This implies element-by-element operation in MATLAB. • , A comma will cause the information to echo

  10. Exercise >> a=2;b=3;c=4,d=5;e=6, c = 4 e = 6 % why did only c and e echo on the screen?

  11. Exercise >> who % lists all the variables on the screen Your variables are: a b c d e >> a % gives the value stored in a a = 2

  12. Exercise >> A=1.5 % Variable A A = 1.5000 >> a,A % Case matters a = 2 A = 1.5000

  13. Exercise >> one=a;two=b;three=c; >> % assigning values to new variables >> four=d;five=e;six=pi; % value of pi available >> f=7; >> A1=[a b c;d e f]; % A1 is a 2 by 3 matrix % space seperates columns % semi-colon seperates rows >> A1(2,2) % accesses the matrix element on the second raw and second column ans = 6

  14. Exercise >> size(A1) % gives you the size of the matrix (row, columns) ans = 2 3 >> AA1=size(A1) % What should happen here? From previous % statement the size of A1 contains two numbers arranged as a row % matrix. This is assigned to AA1 AA1 = 2 3

  15. Exercise >> size(AA1) % AA1 is a one by two matrix ans = 1 2 >> A1’ % this transposes the matrix A1 ans = 2 5 3 6 4 7

  16. Exercise >> B1=A1’ % the transpose of matrix A1is assigned to B1. B1 is a % three by two matrix B1 = 2 5 3 6 4 7 >> C1=A1*B1 % Matrix multiplication C1 = 29 56 56 110

  17. Exercise >> C2=B1*A1 C2 = 29 36 43 36 45 54 43 54 65 >> C1*C2 % Read the error matrix ??? Error using ==> * Inner matrix dimensions must agree.

  18. Exercise >> D1=[1 2]' % D1 is a column vector D1 = 1 2 >> C1,C3=[C1 D1] % C1 is augmented by an extra column C1 = 29 56 56 110 C3 = 29 56 1 56 110 2

  19. Exercise >> C2 C2 = 29 36 43 36 45 54 43 54 65 >> C3=[C3;C2(3,:)] % The column represents all the columns C3 = 29 56 1 56 110 2 43 54 65

  20. Exercise >> C4=C2*C3 C4 = 4706 7906 2896 5886 9882 3636 7066 11858 4376 >> C5=C2.*C3 % The .* represents the product of each element of % C2 with the corresponding element of C3 C5 = 841 2016 43 2016 4950 108 1849 2916 4225

  21. Exercise >> C6=inverse(C2) ??? Undefined function or variable 'inverse'. % Apparently, inverse is not a command in MATLAB, if command % name is known, it is easy to obtain help >>lookfor inverse % this command will find all files where it comes % across the word “inverse” in the initial comment lines. The % command we need appears to be INV which says inverse of a % matrix. The actual command is in lower case. To find out how to use % it: >> help inv inv(C2) % inverse of C2

  22. Exercise >> for i=1:20 f(i)=i^2; end % The for loop is terminated with “end” >> plot(sin(0.01*f)',cos(0.03*f)) >> xlabel('sin(0.01f)') >> ylabel('cos(0.03*f)') >> legend('Example') >> title('A Plot Example') >> grid >> exit % finished with MATLAB

  23. Graphical optimization Minimize f(x1,x2)= (x1-3)2 + (x2-2)2 subject to: h1 (x1,x2): 2x1 + x2 =8 h2(x1,x2): (x1-1)2 + (x2-4)2 =4 g1(x1,x2): x1 + x2 ≤ 7 g2(x1,x2): x1 – 0.25 x22≤ 0 0 ≤ x1≤ 10; 0 ≤ x2≤ 10;

  24. Example 1 % Example 1 (modified graphics)% % % graphical solution using matlab (two design variables) % the following script should allow the graphical solution % to example % % Minimize f(x1,x2) = (x1-3)**2 + (x2-2)**2 % % h1(x1,x2) = 2x1 + x2 = 8 % h2(x1,x2) = (x1-1)^2 + (x2-4)^2 = 4 % g1(x1,x2) : x1 + x2 <= 7 % g1(x1,x2) : x1 - 0.25x2^2 <= 0.0 % % 0 <= x1 <= 10 ; 0 <= x2 <= 10 % % %

  25. Example 1 % % % WARNING : The hash marks for the inequality constraints must % be determined and drawn outside of the plot % generated by matlab % %---------------------------------------------------------------- x1=0:0.1:10; % the semi-colon at the end prevents the echo x2=0:0.1:10; % these are also the side constraints % x1 and x2 are vectors filled with numbers starting % at 0 and ending at 10.0 with values at intervals of 0.1 [X1 X2] = meshgrid(x1,x2); % generates matrices X1 and X2 correspondin % vectors x1 and x2

  26. Example 1 cont’d f1 = obj_ex1(X1,X2);% the objecive function is evaluated over the entire mesh ineq1 = inecon1(X1,X2);% the inequality g1 is evaluated over the mesh ineq2 = inecon2(X1,X2);% the inequality g2 is evaluated over the mesh eq1 = eqcon1(X1,X2);% the equality 1 is evaluated over the mesh eq2 = eqcon2(X1,X2);% the equality 2 is evaluated over the mesh [C1,h1] = contour(x1,x2,ineq1,[7,7],'r-'); clabel(C1,h1); set(h1,'LineWidth',2) % ineq1 is plotted [at the contour value of 8] hold on % allows multiple plots k1 = gtext('g1'); set(k1,'FontName','Times','FontWeight','bold','FontSize',14,'Color','red') % will place the string 'g1' on the lot where mouse is clicked

  27. Example 1 cont’d [C2,h2] = contour(x1,x2,ineq2,[0,0],'r--'); clabel(C2,h2); set(h2,'LineWidth',2) k2 = gtext('g2'); set(k2,'FontName','Times','FontWeight','bold','FontSize',14,'Color','red') [C3,h3] = contour(x1,x2,eq1,[8,8],'b-'); clabel(C3,h3); set(h3,'LineWidth',2) k3 = gtext('h1'); set(k3,'FontName','Times','FontWeight','bold','FontSize',14,'Color','blue') % will place the string 'g1' on the lot where mouse is clicked [C4,h4] = contour(x1,x2,eq2,[4,4],'b--'); clabel(C4,h4); set(h4,'LineWidth',2) k4 = gtext('h2'); set(k4,'FontName','Times','FontWeight','bold','FontSize',14,'Color','blue')

  28. Example 1 cont’d [C,h] = contour(x1,x2,f1,'g'); clabel(C,h); set(h,'LineWidth',1) % the equality and inequality constraints are not written with 0 on the right hand side. If you do write % them that wayyou would have to include [0,0] in the contour commands xlabel(' x_1 values','FontName','times','FontSize',12,'FontWeight','bold'); % label for x-axes ylabel(' x_2 values','FontName','times','FontSize',12,'FontWeight','bold'); set(gca,'xtick',[0 2 4 6 8 10]) set(gca,'ytick',[0 2.5 5.0 7.5 10]) k5 = gtext({'Chapter 2: Example 1','pretty graphical display'}) set(k5,'FontName','Times','FontSize',12,'FontWeight','bold') clear C C1 C2 C3 C4 h h1 h2 h3 h4 k1 k2 k3 k4 k5 grid hold off

  29. Example 1 cont’d Objective function function retval = obj_ex1(X1,X2) retval = (X1 - 3).*(X1 - 3) +(X2 - 2).*(X2 - 2); The first inequality function retval = inecon1(X1, X2) retval = X1 + X2; The second inequality function retval = inecon2(X1,X2) retval = X1 - 0.25*X2.^2;

  30. Example 1 cont’d The first equality function retval = eqcon1(X1,X2) retval = 2.0*X1 + X2; The second equality function retval = eqcon2(X1,X2) retval = (X1 - 1).*(X1 - 1) + (X2 - 4).*(X2 - 4);

  31. Example 2- Graphical solution

More Related