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

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Introduction to Matlab
Introduction to Matlab. I use Matlab for: Data analysis Data plotting Image Analysis Also – Simulations (solving odes/pdes/finite element methods) – Minimisations,
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Visual Basic.NET Windows Programming
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB
Chapter 6 JavaScript: Introduction to Scripting
Prof. Mark Glauser Created by: David Marr
Signals in Matlab Matlab as a name stands for Matrix Laboratory.
MATLAB: Script and Function Files
Introduction to Scripting
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Introduction To MATLAB
Introduction To Matlab Class 1
EEE 244 Numerical Methods In Electrical Engineering
MATLAB: Script and Function Files
StatLab Matlab Workshop
Part 1 Chapter 2 MATLAB Fundamentals
WEB PROGRAMMING JavaScript.
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Introduction to MATLAB
INTRODUCTION TO MATLAB
MATLAB: Script and Function Files
Part 1 Chapter 2 MATLAB Fundamentals
Simulation And Modelling
244-2: MATLAB/Simulink FUNDAMENTALS
Experiment No. (1) - an introduction to MATLAB
Announcements P3 due today
How to Use MATLAB A Brief Introduction.
Matlab Training Session 2: Matrix Operations and Relational Operators
Introduction to Matlab:
Matlab Basics.
Presentation transcript:

MATLAB Exercises Assoc. Prof. Dr. Pelin GÜNDEŞ 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. ; 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.

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.

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.

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

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

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

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?

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

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

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

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 =

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

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

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

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

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

Exercise >> C4=C2*C3 C4 = 4706 7906 2896 5886 9882 3636 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

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

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

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;

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

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

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

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')

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 way you 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

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;

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);

Example 2- Graphical solution