简体   繁体   中英

How can I plot a differential equation in python?

I want to plot the solution of my differential equation but I got this:

' ValueError: x and y must have same first dimension, but have shapes (360,) and (1,) '

When I write _plt.plot(t,final[:1])_ I got

' Equality object is not subscriptable '

statement.

Here is my code:

import numpy as np
import matplotlib.pyplot as plt

from sympy.abc import * 
import sympy as sy

L= float(input('L:'))
R= float(input('R:'))

v=220*sy.sqrt(2)
i=sy.Function('i')

q=sy.dsolve(sy.Eq(sy.Derivative(i(t)*L,t)+i(t)*R,v*sy.sin(t)),i(t)).evalf()
constant=sy.solve(q.subs(i(t),0),dict=True)

t=np.linspace(0,360,360)
final=q.subs(constant[0]).evalf()

plt.plot(t,final)
plt.show()

What should I do?

It's obvious from the code that t has 360 elements

t=np.linspace(0,360,360)

The error complains that final has an initial dimension of 1, where as it should be 360 like t . While it is possible the final has (1,) shape (1 element, containing another array or list), more likely it is (1, n).

When you get shape errors, you need to look at the shape of relevant arrays,

print(final.shape, final.dtype)

and decide from that the correct way of adjusting the shapes.

plot can handle a second argument that is (360,m), where m is the number of lines that it should plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM