Why Python? (Intro. to Python)¶
Why Python?¶
- free/open-source
- clean syntax
- powerful and all purpose
- easy to learn
- “batteries included”
- many, many libraries available
- python-control library will be very valuable for our class
- numpy, scipy, matplotlib, IPython, spyder, ....
- I like it and have a lot of experience using and teaching it
What do we need for dynamic systems and control?¶
- model dynamic systems and simulate their response to various inputs
- numeric integration
- transfer functions
- process experimental data (.csv or .xlsx)
- find frequency response of various systems
- design feedback control systems based on root locus and Bode
- Big Idea: Python has open-source libraries for doing all of these things and it is free and fairly easy to learn
Python vs. C syntax 1¶
- Python uses dynamic typing, so you do not have to pre-declare your variable types
- semi-colons are not necessary at the end of every line
Python vs C. Syntax 2: for loops¶
C
for (int i=0; i<10; i++){
cout << "i = " << i
}
Python
for i in range(10):
print('i = ' + str(i))
Python vs. C: functions¶
C
int my_C_func(int A, int B){
int C;
C = A + B;
return C;
}
Python
def my_Python_func(A, B):
C = A + B
return C
C functions: only 1 return value¶
- C functions can only return one thing
- to work around this, you have to pass in a pointer for your other “outputs”
// declare D outside of function
int D;
int my_C_func(int A, int B, int* D){
int C;
C = A + B;
D = A*B;
return C;
}
// call passing in pointer to D
my_C_func(2, 7, &D);
Python functions: multiple return values¶
Returning multiple values from a Python function is straightforward:
def my_Python_func(A, B):
C = A + B
D = A*B
return C, D
Learning a new language¶
- what things do you need to learn to use a new language for
engineering purposes?
- basic syntax
- declaring variables
for
loopsif/then
statements- defining functions
- plotting
- working with vectors and matrices
- reading data files
- simulating dynamic systems
The next section discusses scientific Python installation: python_installation.html