# 求解具有未知参数的 BVP

${{\mathit{y}}^{\prime }}^{\prime }+\left(\lambda -2\mathit{q}\text{}\mathrm{cos}\left(2\mathit{x}\right)\right)\mathit{y}=0$.

${\mathit{y}}^{\prime }\left(0\right)=0$,

${\mathit{y}}^{\prime }\left(\pi \right)=0$.

$\mathit{y}\left(0\right)=1$.

### 编写方程代码

• `x` 是自变量。

• `y` 是因变量。

• `lambda` 是表示特征值的未知参数。

${{\mathit{y}}_{1}}^{\prime }={\mathit{y}}_{2}$,

${{\mathit{y}}_{2}}^{\prime }=-\left(\lambda -2\mathit{q}\text{}\mathrm{cos}\left(2\mathit{x}\right)\right){\mathit{y}}_{1}$.

```function dydx = mat4ode(x,y,lambda) % equation being solved dydx = [y(2) -(lambda - 2*q*cos(2*x))*y(1)]; end ```

### 编写边界条件代码

• `ya` 是在区间 $\left[\mathit{a},\mathit{b}\right]$ 开始处的边界条件的值。

• `yb` 是在区间 $\left[\mathit{a},\mathit{b}\right]$ 结束处的边界条件的值。

• `lambda` 是表示特征值的未知参数。

${\mathit{y}}^{\prime }\left(0\right)=0$,

${\mathit{y}}^{\prime }\left(\pi \right)=0$,

$\mathit{y}\left(0\right)-1=0$.

```function res = mat4bc(ya,yb,lambda) % boundary conditions res = [ya(2) yb(2) ya(1)-1]; end ```

### 创建初始估计值

```function yinit = mat4init(x) % initial guess function yinit = [cos(4*x) -4*sin(4*x)]; end ```

```lambda = 15; solinit = bvpinit(linspace(0,pi,10),@mat4init,lambda);```

### 求解方程

`sol = bvp4c(@mat4ode, @mat4bc, solinit);`

### 参数值

```fprintf('Fourth eigenvalue is approximately %7.3f.\n',... sol.parameters)```
```Fourth eigenvalue is approximately 17.097. ```

### 对解进行绘图

```xint = linspace(0,pi); Sxint = deval(sol,xint);```

```plot(xint,Sxint) axis([0 pi -4 4]) title('Eigenfunction of Mathieu''s Equation.') xlabel('x') ylabel('y') legend('y','y''')```

### 局部函数

```function dydx = mat4ode(x,y,lambda) % equation being solved q = 5; dydx = [y(2) -(lambda - 2*q*cos(2*x))*y(1)]; end %------------------------------------------- function res = mat4bc(ya,yb,lambda) % boundary conditions res = [ya(2) yb(2) ya(1)-1]; end %------------------------------------------- function yinit = mat4init(x) % initial guess function yinit = [cos(4*x) -4*sin(4*x)]; end %-------------------------------------------```