用 bvp4c 求解二阶微分方程(没有匹配的维度)

Solving second order differential equation with bvp4c (not having matching dimensions)

假设我有 BVP

ny^(n-1)-2*phi*y''=0,其中给出了n和phi。 y(0)=y(5)=4

为了使用BVP4c,我首先将其转换为一阶微分方程组

说 y_1=y 和 y_2=y'

然后我们得到系统

y'_1 = y2, y'_2 = (nx^(n-1))/(2*phi)

我现在写了一个matlab函数来做到这一点,即

function sol=bvpsolve(n,phi)

function dydx = odefun(x,y)
dydx = [y(2);(n*y(1)^(n-1))/(2*phi)]
end

function res = bcfun(ya,yb)
res = [ ya(1) -1.0; yb(1) -4.0]
end


solinit = bvpinit(linspace(0,5,4),[1 0]);
sol = bvp4c(@odefun,@bcfun,solinit);
end

然后我在终端中调用函数,即

sol=bvpsolve(2,1);

然后我得到这个错误

In an assignment  A(I) = B, the number of elements in B and I must be the same.

Error in bvp4c>colloc_RHS (line 600)
Phi(1:nBCs) = Gbc(y(:,Lidx),y(:,Ridx),ExtraArgs{1:nExtraArgs});

Error in bvp4c (line 189)
  [RHS,yp,Fmid,NF] = colloc_RHS(n,x,Y,ode,bc,npar,xyVectorized,mbcidx,nExtraArgs,ExtraArgs);

Error in bvpsolve (line 13)
sol = bvp4c(@odefun,@bcfun,solinit);

Error in hw4 (line 3)
sol=bvpsolve(2,1);

这似乎是一个匹配错误,即由于某种原因我没有匹配尺寸。我匹配了此处找到的文档 https://www.mathworks.com/help/matlab/ref/bvp4c.html

基本上只是重做第一个示例所做的事情。有人能轻易看出我哪里出错了吗?

你的线路

res = [ ya(1) -1.0; yb(1) -4.0]

看起来像构造一个 2×2 矩阵,实际上你想要 return 边界条件(你在问题陈述中说 y(0)=4?)

本质上是CAS在解释white-spaces的意思时想太多了。只需删除空格

res = [ ya(1)-1.0; yb(1)-4.0]

或使格式更明确

res = [ [ ya(1) - 1.0 ]; [ yb(1) - 4.0 ] ]

在示例中,他们制作了加号 stand-alone,这似乎也阻止了 mis-interpretations、

res = [ ya(1) - 1.0; yb(1) - 4.0]