1
1.16 Write a user-defined MATLAB function that uses the finite difference method to solve a boundary
value problem of the form:
for with and
where and are constants. Discretize the ODE using second-order accurate central differences. For
the function name and arguments use [x,y]=BVP2ndVar(a,b,Ya,Yb,n,pOFx,qOFx,rOFx).
The input variables a and b define the domain of the solution, Ya and Yb are the boundary conditions, n is
the number of subintervals, and pOFx, qOFx, and rOFx are the names for the user-defined functions that
calculate , , and , respectively. They are dummy names for the anonymous or user-defined
functions that are imported into BVP2ndVar. Within the program, use MATLAB’s left division operation
to solve the system of linear equations.
Use BVP2ndVar with 50 subintervals to solve the boundary value problem in Problem 11.3. Plot
the solution.
Solution
Using central difference formulas for the derivatives gives:
d2y
dx2
——-–px()
dy
dx
—––qx()y++ rx()=
yi1– 2yi
–yi1+
+
h2
—————-——————-—-– px
i
()
yi1+ yi1–
–
2h
—————-———- qx
i
()yi
++rx
i
()=