Solve the following differential equation using symbolic and numeric methods: y'' + 2y' + y = 2sin(x), y(0) = 0, y'(0) = 1
时间: 2024-05-07 08:19:56 浏览: 109
Symbolic method:
We can solve the differential equation using the characteristic equation:
r^2 + 2r + 1 = 0
Solving for r, we get:
r = -1 (repeated root)
So the general solution is:
y(x) = (c1 + c2*x)*e^(-x)
To find the particular solution, we can use the method of undetermined coefficients. Since the right-hand side of the equation is 2sin(x), we can guess a particular solution of the form:
y_p(x) = A*sin(x) + B*cos(x)
Taking the first and second derivatives of y_p(x), we get:
y'_p(x) = A*cos(x) - B*sin(x)
y''_p(x) = -A*sin(x) - B*cos(x)
Substituting these into the differential equation, we get:
-A*sin(x) - B*cos(x) + 2(A*cos(x) - B*sin(x)) + (A*sin(x) + B*cos(x)) = 2sin(x)
Simplifying, we get:
2A = 2
-2B + 2A = 0
Solving for A and B, we get:
A = 1
B = 1
So the particular solution is:
y_p(x) = sin(x) + cos(x)
The general solution is:
y(x) = (c1 + c2*x)*e^(-x) + sin(x) + cos(x)
Using the initial conditions, we get:
y(0) = c1 + 1 = 0
y'(0) = -c1 + c2 + 1 = 1
Solving for c1 and c2, we get:
c1 = -1
c2 = 2
So the solution to the differential equation is:
y(x) = (-1 + 2x)*e^(-x) + sin(x) + cos(x)
Numeric method:
We can also solve the differential equation numerically using a computer program. One way to do this is to use the fourth-order Runge-Kutta method. Here is an example Python code that solves the differential equation:
```python
import numpy as np
import matplotlib.pyplot as plt
def f(x, y):
return [y[1], 2*np.sin(x) - 2*y[1] - y[0]]
x0 = 0
y0 = [0, 1]
h = 0.01
x = np.arange(x0, 10+h, h)
y = np.zeros((len(x), len(y0)))
y[0] = y0
for i in range(len(x)-1):
k1 = h*np.array(f(x[i], y[i]))
k2 = h*np.array(f(x[i]+h/2, y[i]+k1/2))
k3 = h*np.array(f(x[i]+h/2, y[i]+k2/2))
k4 = h*np.array(f(x[i]+h, y[i]+k3))
y[i+1] = y[i] + (k1 + 2*k2 + 2*k3 + k4)/6
plt.plot(x, y[:,0])
plt.xlabel('x')
plt.ylabel('y')
plt.show()
```
The code defines the function f(x, y) that returns the right-hand side of the differential equation as a list. It then initializes the initial conditions and the step size. The program then uses the fourth-order Runge-Kutta method to solve the differential equation and stores the solution in the array y. Finally, the program plots the solution. The resulting plot should match the symbolic solution.
阅读全文