Finding the stability of a system using Python code

In summary, this code makes a plot that is given below. I don't understand what the line:dT/dt=( a11+Temperature_Cycles(t, 0.35, 1)+Temperature_Cycles(t, 0.5, 1.5) )*T+forcing means. I guess in a and b I set forcing=0 since they don't ask about adding forcing. But when making an annual cycle, should I just get rid of one of the Temperature_Cycles () in the line above? And when plotting the systems with two cycles, I must keep both Temperature_Cycles expressions?The code for the first task is:def Temperature_Cycles(t, amplitude, period):#
  • #1
appletree23
12
2
I'm given this code to plot the system for task 1 where my teacher have used some random numbers just to show us how the code can be used:

1612477667285.png


The code makes a plot that is given below:
1612477711327.png

So my professor just gave us this code and plot without saying anything more. All the students in the class have also been given a set of unique coefficients in a table for a11, a12, a21,a22, forcing and amplitude and period for the second cycle it says. We are supposed to use this code above to solve a-c. What I don't understand is the line:

dT/dt=( a11+Temperature_Cycles(t, 0.35, 1)+Temperature_Cycles(t, 0.5, 1.5) )*T+forcing

I guess in a and b I set forcing=0 since they don't ask about adding forcing. But when making an annual cycle, should I just get rid of one of the Temperature_Cycles () in the line above? And when plotting the systems with two cycles, I must keep both Temperature_Cycles expressions?

I also struggle with understanding problem number 2:
1612478038426.png

I don’t understand if I should use the values a11=a12=a21=1 or my own unique value given from the table in both task 2a and 2b because it says table in the text above in brackets after the task defines the values. How do you guys interpret this?

This is the code (down below) we are supposed to use to solve task 2 by the way if someone wonder. And my teacher says we are only supposed to change the values in matrix except from a22.

1612478328463.png
 
Technology news on Phys.org
  • #2
Welcome to PF. Fun problem :smile:

It would help a lot if you could post each piece of code as text enclosed in "code tags" instead of as images. That would help us a lot in reviewing your code, and quoting any issues that we see.

To enclose your text code in code tags, just use these tags without the spaces:

[ code ]
<< your text here >>
[ /code ]

So it looks like this:

Code:
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}
 
  • #3
berkeman said:
Welcome to PF. Fun problem :smile:

It would help a lot if you could post each piece of code as text enclosed in "code tags" instead of as images. That would help us a lot in reviewing your code, and quoting any issues that we see.

To enclose your text code in code tags, just use these tags without the spaces:

[ code ]
<< your text here >>
[ /code ]

So it looks like this:

Code:
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}
Ok, I will do that and post the individual codes right away. I'm not able to edit my orginal post, so I have to post the code under here
 
  • Like
Likes berkeman
  • #4
The code for the first task:

Python:
# make a model with annual cycle
def Temperature_Cycles(t, amplitude, period):
# amplitude - is the amplitude of a cycle
# period - is the period of a cycle (in years)
    tc = amplitude * np.sin(t*period*2*np.pi/12)
    return tc

# the model function
def model(T,t,a11,forcing):
    dTdt = (a11 + Temperature_Cycles(t, 0.35, 1.0) + Temperature_Cycles(t, 0.5, 1.5)) * T + forcing
    return dTdt

# time (in months)
t = np.linspace(0,120)
a11 = 0.01
forcing = 0.01
# solve ODE
Tac = odeint(model,T0,t,args=(a11,forcing))
a11 = -0.01
forcing = 0.01
# solve ODE
Tac1 = odeint(model,T0,t,args=(a11,forcing))

# plot results 
plt.plot(t,Tac, color='red')
plt.plot(t,Tac1, color='blue')
plt.xlabel('time [months]')
plt.ylabel('T(t)')
plt.legend(('$a_{11} > 0$', r'$a_{11} < 0$'))
plt.show()

And the plot for the second task is these three cells:

Python:
# load modules
from sympy import *
from sympy.plotting import plot
# define variables
a22 = Symbol('a22')
lambda1 = Symbol('lambda1')
lambda2 = Symbol('lambda2')

Python:
# solving the Task
M = Matrix([[-1, 1], [-1, a22]])
M_eigenvals = M.eigenvals().keys()
lambda1 = list(M_eigenvals)[0]
lambda2 = list(M_eigenvals)[1]
print(list(M_eigenvals))

In this cell above one shall change the values in the matrix, except for a22. And the last cells makes the plot to solve the task:

Python:
# plot the results: 1st eigenvalue red, 2nd - blue
p = plot(re(lambda1), re(lambda2), line_color='red', title='Eigenvalue (real part)', xlabel=r'$a_{22}$', ylabel=r'$\lambda_{1,2}$', show=False)
p[1].line_color = 'blue'
p.show()
p = plot(im(lambda1), im(lambda2), line_color='red', title='Eigenvalue (imagery part)', xlabel=r'$a_{22}$', ylabel=r'$\lambda_{1,2}$', show=False)
p[1].line_color = 'blue'
p.show()
 
  • Like
Likes berkeman
  • #5
@berkeman . Do you have any suggestions regarding the first task?
 

1. How can I use Python code to determine the stability of a system?

To determine the stability of a system using Python, you can use the concept of eigenvalues. This involves converting the system into a matrix and calculating the eigenvalues of that matrix. If all the eigenvalues have negative real parts, then the system is stable.

2. What are the advantages of using Python code for stability analysis?

Python is a versatile and powerful programming language that offers a wide range of tools and libraries for scientific computing. It also has a simple and intuitive syntax, making it easier to write and debug code for stability analysis compared to other languages.

3. Can I use Python code to analyze the stability of any type of system?

Yes, you can use Python code to analyze the stability of various types of systems, such as mechanical, electrical, and chemical systems. As long as the system can be represented as a set of differential equations, Python can be used for stability analysis.

4. Are there any reliable packages or modules in Python for stability analysis?

Yes, there are several packages and modules available in Python that can be used for stability analysis. Some popular options include SciPy, Control Systems Library, and PyDSTool. These packages offer various functions and methods for analyzing the stability of different types of systems.

5. How accurate are the results obtained from stability analysis using Python code?

The accuracy of the results obtained from stability analysis using Python code depends on the accuracy of the input data and the complexity of the system. If the input data is accurate and the system is not too complex, then the results can be highly accurate. However, it is always recommended to validate the results with other methods to ensure accuracy.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
765
  • Programming and Computer Science
Replies
2
Views
896
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top