EEE 244 Numerical Analysis

EEE 244 Numerical Analysis

244-2: MATLAB/PYTHON FUNDAMENTALS Scalars To assign a single value to a variable, simply type the variable name, the = sign, and the value: >> a = 4 a= 4 Note that variable names must start with a letter, though they can contain letters, numbers, and the underscore (_) symbol Array Creation % Matlab command to create a vector >>x = [ 1 2 3 4 5]

ans = 12345 # Python command to create a vector Import numpy as np x = np.array([1,2,3,4,5]) x Matrix creation % Matlab command x=[1 2 3; 4 5 6; 7 8 9]; ans = 123 456 789

# Python command import numpy as np x=np.array([[1,2,3], [4,5,6], [7,8,9]]) x Mathematical Operations Mathematical operations in MATLAB/Python can be performed on both scalars and arrays Matlab 4^2 = 16 2*8 = 16 32/4 = 8 Python

4**2 =16 2*8 = 16 32/4 = 8 Generate p pi Import numpy as np 2*np.pi = 6.2832 np.pi/4 = 0.7854 Addition and Subtraction

3+5 = 8 3-5 = -2 3+5 = 8 3-5 = -2 Exponent Multiplication and Division Complex Numbers in Matlab/Python All operations can be used with complex quantities

x*y x/y x+y x-y Values containing an imaginary part are entered using: x = 2+4j Matrix Calculations in Matlab/Python MATLAB/Python can also perform operations on vectors and matrices. For example, multiply the two matrices: A= 367 5 -3 0 B=1 1

2 1 3 -3 Matlab A = [ 3 6 7; 5 -3 0] B = [1 1; 2 1; 3 -3] C = A*B Python import numpy as np A = np.array([[3, 6, 7], [5, -3, 0]]) B = np.array([[1, 1], [2, 1], [3, -3]]) C = A.dot(B) print(C)

Element-by-Element Calculations At times, you will want to carry out calculations item by item in a matrix or vector. They are also often referred to as element-by-element operations. For array operations, both matrices must be the same size A= 367 B=1 11 5 -3 0 2 13 Matlab A = [ 3 6 7; 5 -3 0] B = [1 1 1; 2 1 3] C = A.*B

Python import numpy as np A = np.array([[3, 6, 7], [5, -3, 0]]) B = np.array([[1, 1, 1], [2, 1, 3 ]) C = A*B print(C) Graphics in Matlab/Python MATLAB/Python have a common plot command to graph vectors Matlab x = [ 3 6 7] y = [1 2 3]

plot(x,y) Python import numpy as np import matplotlib.pyplot as plt x = np.array([3,6,7]) y = np.array([1,2,3]) plt.plot(x,y) Other Matlab Plotting Commands hold on and hold off hold on will keep the current plot active Enables the user to superimpose plots on each other

hold off will release the current plot subplot(m, n, p) subplot command enables multiple plots on a single page Divides the page into m x n sections Conditional IF Loops For Loops % Matlab x=0 for i = 1:10; x=x+I end

# Python x=0 for i in range(1,10): sum = sum + x print(sum) Example of Input command Let us say you want to enter todays date clear month = input(enter month,1-12 ) day = input(enter day, 1-31 ) year = input(enter year,20xx ) today_date=[month day year]

Relational Operators Summary of relational operators in MATLAB/Python Example x == 0 unit ~= m a < 0 s > t 3.9 <= a/3 r >= 0 Operator == ~=

< > <= >= Relationship Equal Not equal Less than Greater than Less than or equal to Greater than or equal to

Recently Viewed Presentations

  • Work and Kinetic Energy Serway (7.1 - Physics &amp; Astronomy

    Work and Kinetic Energy Serway (7.1 - Physics & Astronomy

    Work and Energy Work Example Determine the work done by a force as the particle moves from x=0 to x=6m: Concept Quiz Physics 1D03 - Lecture 19 Work and Energy Work Example Determine the work done by a force as...
  • Ethics &amp; Values - MCCC

    Ethics & Values - MCCC

    Arial Arial Black Calibri Times NRS 101 Ethics and Valus 1_NRS 101 Ethics and Valus Ethics & Values About Ethics Values Values Professional Values Clarifying Client Values Slide 7 Principles of Ethical Decision Making Nursing Codes of Ethics Models of...
  • Section 4.19 - Lower Moreland Township School District

    Section 4.19 - Lower Moreland Township School District

    Britain: The Puritan Revolution Day 31-33 Chapter 16 (p.448-452) (4.19) Puritan Revolution
  • The Case For Passive Investing - New York University Stern ...

    The Case For Passive Investing - New York University Stern ...

    The Case For Passive Investing: Active investor track records ... which divides the excess return by the beta and the appraisal ratio which divides the alpha from the regression by the standard deviation can be considered close relatives of Jensen's...
  • School initiative

    School initiative

    The GOAL . To implement a model of residential group care that works with youth, their families and other community partners to prepare them for success after leaving placement.. It is designed for use
  • Marketing Strategy Formulation Process and Strategic Analysis

    Marketing Strategy Formulation Process and Strategic Analysis

    Characteristics of Effective Corporate Mission Statements Ansoff Strategies The BCG Growth Share Matrix Cash Flows Across Businesses in the BCG Portfolio Model The GE Nine-Cell Matrix Long-haul, coal carrying railroad Railroad business Physical Based on existing products or technology Long...
  • 投影片 1 - National Dong Hwa University

    投影片 1 - National Dong Hwa University

    Negotiation * * Concessions give-and-take being common in negotiation guidelines for making concessions reserve room for concessions first understand the other side's needs and objectives first to concede minor but not the first to concede major portray unimportant concessions as...
  • Supplier evaluation criteria - Ericsson.com

    Supplier evaluation criteria - Ericsson.com

    The on-site audit follows the steps described in the picture and are based on the supplier evaluation criteria with the finance criteria as an exception. The result from the on-site audit will be used as input for decision regarding future...