Python Finding the stability of a system using Python code

AI Thread Summary
The discussion revolves around using provided code to analyze a system of equations for a class assignment. The code includes functions for plotting temperature cycles and solving ordinary differential equations (ODEs). Key points include confusion about the line of code involving temperature cycles and how to apply unique coefficients provided in a table for specific tasks. There is uncertainty about whether to set the forcing parameter to zero for certain tasks and how to handle the temperature cycle functions when creating an annual cycle. Additionally, there is a question about whether to use default values or the unique values from the table for matrix coefficients in subsequent tasks. The participants are encouraged to share code snippets in a more accessible format for better collaboration and troubleshooting.
appletree23
Messages
12
Reaction score
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
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;
}
 
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
 
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()
 
@berkeman . Do you have any suggestions regarding the first task?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top