Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Array OperationsSlide 20Slide 21Slide 22Matrix Operation (conti.)Colon OperatorSlide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Initializing VariablesSlide 35Slide 36M-Files: Scripts and FunctionsSlide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55MATLABMatrix LaboratoryInternet Resources:Dr. H-C Chen’s CVEN 302 Web Pagehttp://ceprofs.tamu.edu/hchen/cven302.htmlFor MATLAB, select chap01b.ppt, or clickhttp://ceprofs.tamu.edu/hchen/cven302/chap01b.pptProgrammingThe ideal style of programming is structuredstructured• Break down a larger goal into tasks• Develop a modulemodule for each task• A module has a single entrance and exit• Modules are reusableUse MATLAB to solve a problem•State the problem clearly•Describe the Input/Output (I/O)•Algorithm - Numerical Method•Develop a MATLAB program•Debugging and Testing•Documentation If needed, work the problem by handCompiler and Program ExecutionComputer ProgramCompilerMachine language programLink/LoadExecuteinputoutput    executionadinglinking/loncompilatio MATLAB ExecutionInteractive environment - does not require formal compilation, linking/loading, and execution Scripts - develop and execute m-files that contain MATLAB commandsImportant!You must include commentscomments in your programs you turn in - otherwise we will have great difficulty knowing what you are thinking and doing. You will have difficulty knowing what you were doing if you need to reuse the programs.Command Window - enter commands and data - print resultsGraphics Window - display plots and graphsEdit Window - create and modify m-filesMATLAB’s 3 WindowsTwo simple MATLAB programs (also called scripts)Filename: lec2a.m% Find sum of 1 to b% Comments begin with “%”% cd y:\cven302\clear allsum = 0;b = input('what is the value of b? ')for i = 1 : b sum = sum + iendNeed extension .m called m-fileFilename: lec2b.m% plot 1/x v.s. tanh xclca = linspace(0,5);f = 1./a;g = tanh(a);plot(a,f,'-', a, g,'--')axis([0 3 0 2])xlabel ('x')ylabel ('y')title ('Plot 1/x and tanh x')text(0.7, 1.6, '1/x')text(2, 1.1, 'tanh x')Now we have seen Command Window, Graphics Window, and Edit WindowThere is another window which is very useful:the Help WindowYou can also type help in Commend Windowe.g.,help input» help input INPUT Prompt for user input. R = INPUT('How many apples') gives the user the prompt in the text string and then waits for input from the keyboard. The input can be any MATLAB expression, which is evaluated, using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix. R = INPUT('What is your name','s') gives the prompt in the text string and waits for character string input. The typed input is not evaluated; the characters are simply returned as a MATLAB string. See also KEYBOARD.MATLAB’s basic component is a matrixi.e., all variables are treated as matricesAll operations are vectorized (optimized for vector use)Loops run much slower in MATLAB than in Fortran (not a vector operation)Scalar, vector, and matrixScalar: k = 511 matrixMatrix: 864297534321am  n matrixm rows (m = 3)n columns (n = 4)231c 4321b14 matrix, or a row vectorVector:31 matrix, or a column vectorVariable• may consist up to 31 characters• starting with a letter and followed by any combination of letters, digits, and underscores (Variable names must start with a letter)• punctuation marks and spaces should not be includede.g., CVEN_302, TIME, Time, time, velocity, force_x, force_y•All numbers are double precision•Text is stored as arrays of characters•You don’t have to declare the type of data (defined when running)• Variables are case sensitiveData Type e.g., CVEN_302, TIME, Time, time, velocity, force_x, force_yArithmetic operation of scalarsAddition: a+b subtraction: a-bmultiplication: a*b division: a/bexponentiation: a^b= ababs(x) : absolute value sqrt(x) : square root = x^(1/2)sin(x) : sine asin(x) : inverse sinesinh(x) : hyperbolic sine asinh(x) : inverse hyperbolic sinelog(x) : natural logarithm log10(x) : common logarithmexp(x) : exponental: ex (e = 2.7183….)Elementary math functions» a = 5;» b = 3;» a*bans = 15» d = ans * 3d = 45» e = ans * 3;» ee = 45» a^2ans = 25» a/bans = 1.6667» format long» a/bans = 1.66666666666667» format short» a/bans = 1.6667» exp(2)ans = 7.3891» 10^2ans = 100» log(100)ans = 4.6052» log10(100)ans = 2» pians = 3.1416; - result not displayedformat long - 15 digitsformat short - 5 digitsans - result from previous calculationpi - Ctrl-c : terminate a running program2e100ln100logArray Operations•An array operation is performed element-by-An array operation is performed element-by-element - Need “element - Need “..” in front of the operator” in front of the operatorB(5);A(5) C(5)B(4);A(4) C(4)B(3);A(3) C(3)B(2);A(2) C(2)B(1);A(1) C(1)MATLAB: C = AMATLAB: C = A..B;B;Arithmetic operation of ArraysAddition: a+b subtraction: a-bmultiplication: a.*b division: a./bexponentiation: a.^b = abMATLAB variables and matrix operation231c 654b» b = [4 5 6]b = 4 5 6» c = [1 3 2]c = 1 3 2» c = [1; 3; 2]c = 1 3 2» d = c'd = 1 3 2 - transpose; - a new line» b * cans = 31» b.*dans = 4 15 12» b./dans = 4.0000 1.6667 3.0000» f = [b; d; [3 6 8]]f = 4 5 6 1 3 2 3 6 8231c 231d 654bb.*d = [4*1 5*3 6*2]b./d = [4/1 5/3 6/2]bd[3 6 8]» f(2,3)ans = 2» size(f)size - size of a matrixMatrix Operation (conti.)    497 ; 321  yx    497321  yxz   497 321 ;  yxus = [1 2 3 4; 5 6 7 8]t = [1 2 3 4.... 5 6 7 8]continueColon OperatorCreating new matrices from an existing matrixC = [1,2,5; -1,0,1;


View Full Document

TAMU CVEN 689 - Chap01 MATLAB

Download Chap01 MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chap01 MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chap01 MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?