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

  • Context: Python 
  • Thread starter Thread starter Natchanon
  • Start date Start date
  • Tags Tags
    Graph Plotting Python
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to plotting the velocity of a relativistic electron using Python's scipy library, specifically the solve_ivp function. Participants explore how to extract and store time and velocity data for plotting after solving the equations of motion.

Discussion Character

  • Technical explanation, Homework-related, Debate/contested

Main Points Raised

  • One participant describes their attempt to solve the equation of motion for a relativistic electron and expresses difficulty in plotting velocity versus time due to issues with data extraction from the solve_ivp function.
  • Another participant questions whether the graph should depict acceleration versus time instead of velocity, indicating a misunderstanding of the original request.
  • Several participants suggest that the returned value from the function should contain the necessary time and velocity data, pointing to the documentation for clarification.
  • A later reply indicates that the original poster has resolved their issue but does not specify the solution.
  • There is a suggestion to close the thread, with some participants discussing the process of doing so.

Areas of Agreement / Disagreement

Participants generally agree that the issue relates to data extraction from the solve_ivp function, but there is no consensus on the exact nature of the problem or the solution, as the original poster has not detailed their resolution.

Contextual Notes

Some participants reference the documentation for the solve_ivp function, but there is no detailed discussion of the specific error encountered or the assumptions made in the original code.

Who May Find This Useful

Individuals interested in numerical methods for solving differential equations in Python, particularly those using scipy for physics-related applications, may find this discussion relevant.

Natchanon
Messages
31
Reaction score
3
TL;DR
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
Is the graph should be something like this ?
 

Attachments

  • Figure_1.png
    Figure_1.png
    4.4 KB · Views: 280
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.
 
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.
 
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?
 
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.
 
Why not close the thread by telling us what the error was?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
990