Python, scipy.integrate.solve_ive, a problem with plotting a graph

In summary, the conversation discusses a code for solving the equation of motion of a relativistic electron and the issue of plotting velocity vs time. The solution involves using the function's returned value, which contains members called t and y. The conversation ends with the poster stating that they have found the answer to their problem.
  • #1
Natchanon
31
3
TL;DR Summary
I want to plot v vs t, but the code doesn't return enough points for plotting
Hi, I have this code that solve the equation of motion of a relativistic electron.
Python:
from math import sqrt
from scipy.integrate import odeint, solve_ivp
import numpy as np
import matplotlib.pyplot as plt

e = 1.602 * 10 ** (-19)
E = 10 ** 6
m = 9.106 * 10 ** (-31)

def d2vdt2(t,r):
    t_arr = []
    v_arr = []
    c = 300000000
    e = 1.602*10**(-19)
    E = 10**6
    m = 9.106*10**(-31)
    tau = 6*10**(-24)
    v,a = r
    gamma = 1/sqrt(1-(v**2)/(c**2))
    d2vdt2 = (1/(tau*(gamma)**6))*((e*E/m) - a*((6*tau*v*a*gamma**8)/(c**2) + gamma + (v**2)*(gamma**3)/(c**2)))
    print(t,v/c)
    return a,d2vdt2

a = solve_ivp(d2vdt2,(0, 20*10**(-9)), (0,e*E/m),"Radau")

The problem is I want to plot v vs t, but I don't know how get lists of v and t from the function. If I have the function return v and t, then the code won't loop enough times to get enough data points for plotting. The results of v and t printed out in the function are correct, but I don't know how to make lists of them as the function resets every time it is called by solve_ivp, and the values of v and t from the previous iteration are gone. Thanks!
 
Technology news on Phys.org
  • #2
Is the graph should be something like this ?
 

Attachments

  • Figure_1.png
    Figure_1.png
    4.4 KB · Views: 187
  • #3
Arman777 said:
Is the graph should be something like this ?
That is the acceleration vs time. I need velocity vs time. I already got the solution now. Thanks for replying.
 
  • #4
Have you read the documentation for the function you are using? A quick search suggests that your returned value a should contain what you are looking for as members called t and y.
 
  • #5
Ibix said:
Have you read the documentation for the function you are using? A quick search suggests that your returned value a should contain what you are looking for as members called t and y.
I already got the answer. How can I close the thread?
 
  • #6
You can report it and ask for the mentors to close it, but I wouldn't bother - I only replied because posts 2 and 3 didn't download until I'd hit post.
 
  • #7
Why not close the thread by telling us what the error was?
 

1. What is Python?

Python is a high-level, interpreted programming language that is used for various purposes such as web development, data analysis, and scientific computing.

2. What is scipy.integrate.solve_ive?

scipy.integrate.solve_ive is a function in the SciPy library that is used for solving initial value problems numerically. It is commonly used for solving differential equations.

3. How can I solve a problem with plotting a graph in Python?

To solve a problem with plotting a graph in Python, you can use the matplotlib library. This library provides various functions and tools for creating visualizations and plots.

4. What is the purpose of using scipy.integrate.solve_ive for plotting a graph?

scipy.integrate.solve_ive is used for solving initial value problems, which can be used to model various physical or mathematical systems. By using this function, you can obtain a numerical solution to the problem, which can then be plotted to visualize the behavior of the system.

5. Can scipy.integrate.solve_ive be used for solving any type of problem?

No, scipy.integrate.solve_ive is specifically designed for solving initial value problems. It may not be suitable for solving other types of problems. It is important to understand the problem at hand and choose the appropriate function or tool for solving it.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
929
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
766
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top