BVP4c解决未知边界 [英] BVP4c solve for unknown boundary

查看:278
本文介绍了BVP4c解决未知边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bvp4c解决4颂歌的系统.问题在于边界之一是未知的.

I am trying to use bvp4c to solve a system of 4 odes. The issue is that one of the boundaries is unknown.

bvp4c可以处理吗?在我的代码中L是我要解决的未知数.

Can bvp4c handle this? In my code L is the unknown I am solving for.

我在下面收到一条错误消息.

I get an error message printed below.

function mat4bvp
L = 8;
solinit = bvpinit(linspace(0,L,100),@mat4init);
sol = bvp4c(@mat4ode,@mat4bc,solinit);
sint = linspace(0,L);
Sxint = deval(sol,sint);
end

% ------------------------------------------------------------
function dtdpdxdy = mat4ode(s,y,L)
Lambda = 0.3536;
dtdpdxdy = [y(2)
            -sin(y(1)) + Lambda*(L-s)*cos(y(1))
            cos(y(1))
            sin(y(1))];
end
% ------------------------------------------------------------
function res = mat4bc(ya,yb,L)
res = [  ya(1) 
         ya(2)
         ya(3)
         ya(4)
        yb(1)];
end
% ------------------------------------------------------------
function yinit = mat4init(s)
yinit = [  cos(s)
    0
    0
    0
      ];
end

不幸的是,我收到以下错误消息;

Unfortunately I get the following error message ;

>> mat4bvp
Not enough input arguments.

Error in mat4bvp>mat4ode (line 13)
            -sin(y(1)) + Lambda*(L-s)*cos(y(1))

Error in bvparguments (line 105)
    testODE = ode(x1,y1,odeExtras{:});

Error in bvp4c (line 130)
    bvparguments(solver_name,ode,bc,solinit,options,varargin);

Error in mat4bvp (line 4)
sol = bvp4c(@mat4ode,@mat4bc,solinit);

推荐答案

将可变端点转换为固定端点的一个技巧是更改时间刻度.如果x'(t)=f(t,x(t))是微分方程,则将t=L*ss0设置为1,然后为y(s)=x(L*s)

One trick to transform a variable end point into a fixed one is to change the time scale. If x'(t)=f(t,x(t)) is the differential equation, set t=L*s, s from 0 to 1, and compute the associated differential equation for y(s)=x(L*s)

y'(s)=L*x'(L*s)=L*f(L*s,y(s))

下一个使用的技巧是将全局变量计算为常数函数,从而将其转换为微分方程的一部分.所以新系统是

The next trick to employ is to transform the global variable into a part of the differential equation by computing it as constant function. So the new system is

[ y'(s), L'(s) ] = [ L(s)*f(L(s)*s,y(s)), 0 ]

L的值作为附加的自由左或右边界值出现,从而将变量的数量=状态向量的维数增加到边界条件的数量.

and the value of L occurs as additional free left or right boundary value, increasing the number of variables = dimension of the state vector to the number of boundary conditions.

我还没有现成的Matlab,在python中,使用scipy工具可以实现为

I do not have Matlab readily available, in Python with the tools in scipy this can be implemented as

from math import sin, cos
import numpy as np
from scipy.integrate import solve_bvp, odeint
import matplotlib.pyplot as plt

 # The original function with the interval length as parameter

def fun0(t, y, L):
    Lambda = 0.3536;
    #print t,y,L
    return np.array([ y[1], -np.sin(y[0]) + Lambda*(L-t)*np.cos(y[0]), np.cos(y[0]), np.sin(y[0]) ]);

# Wrapper function to apply both tricks to transform variable interval length to a fixed interval.

def fun1(s,y):
    L = y[-1];
    dydt = np.zeros_like(y);
    dydt[:-1] = L*fun0(L*s, y[:-1], L);
    return dydt;

# Implement evaluation of the boundary condition residuals:

def bc(ya, yb):
    return [  ya[0],ya[1], ya[2], ya[3], yb[0] ];

# Define the initial mesh with 5 nodes:

x = np.linspace(0, 1, 3)

# This problem has multiple solutions. Try two initial guesses.

L_a=8
L_b=9

y_a = odeint(lambda y,t: fun1(t,y), [0,0,0,0,L_a], x)
y_b = odeint(lambda y,t: fun1(t,y), [0,0,0,0,L_b], x)

# Now we are ready to run the solver.

res_a = solve_bvp(fun1, bc, x, y_a.T)
res_b = solve_bvp(fun1, bc, x, y_b.T)

L_a = res_a.sol(0)[-1]
L_b = res_b.sol(0)[-1]
print "L_a=%.8f, L_b=%.8f" % ( L_a,L_b )

# Plot the two found solutions. The solution are in a spline form, use this to produce a smooth plot.

x_plot = np.linspace(0, 1, 100)
y_plot_a = res_a.sol(x_plot)[0]
y_plot_b = res_b.sol(x_plot)[0]

plt.plot(L_a*x_plot, y_plot_a, label='L=%.8f'%L_a)
plt.plot(L_b*x_plot, y_plot_b, label='L=%.8f'%L_b)
plt.legend()
plt.xlabel("t")
plt.ylabel("y")
plt.grid(); plt.show()

产生

L尝试不同的初始值会发现其他解决方案,它们的规模差异很大.

Trying different initial values for L finds other solutions on quite different scales, among them

L=0.03195111
L=0.05256775
L=0.05846539
L=0.06888907
L=0.08231966
L=4.50411522
L=6.84868060
L=20.01725616
L=22.53189063

这篇关于BVP4c解决未知边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆