Anonymous ODE function syntax for multipoint BVP

Is there a way to define an anonymous ODE function (in a separate m file) while using bvp4c with multi-point boundary conditions? I tried using the syntax dydx = ODEfun(x,y,k,p) where p is a vector of known parameters and k is the region. The bvp4c syntax I used was: sol = bvp4c(@(x,y,px,k) ODEfun,@(yl,yr) @BCfun, solinit). I get an error saying "Too many input parameters for ODEfun". I can get it to work if the ODEfun is nested as in the threebvp example.

2 个评论

Sorry, the syntax for bvp4c was: sol = bvp4c(@(x,y,k) ODEfun(x,y,k,p),@(yl,yr) BCfun, solinit)
Hey Binz,
Have you been abal to solve your problem? I have a similar problem (a multipoint bvp with several unknown parameters) and I got stock in the same line.

请先登录,再进行评论。

回答(1 个)

In your exercise, a relatively simple solution is to employ the function handles. The suggested approach is shown via Sturm-Liouville BVP example.
% Sturm_Liouville_bvp. Given: -u"+w^2*u=sinh(t)
% periodic boundary conditions: u(0)=u(1)=0
% ----------------------------------------------------------
% Define residues of BCs via a function handle
Res=@(yl,yr)([yl(1) yr(1)]);
% 1st guess solution function defined with function handle:
Gsol1=@(t)([sin(-0.540302305868140*t), cos(t)-0.540302305868140]);
close all; clc
figure
t = linspace(0, 1, 50);
omega = [0, 3, 5, 7, 13];
LABEL = {};
for ii=1:numel(omega)
% A guess structure consisting of time mesh within [0, 1]
% in the range of BCs and a guess function (Gsol):
SOLin1 = bvpinit(linspace(0,1, 10),Gsol1);
% Another numeric value based initial guess y1=0 and y2=0
SOLin2 = bvpinit(linspace(0,1, 10),[0,0]);
% Given problem formulation:
dy=@(t,y)([y(2), omega(ii)^2*y(1)-sinh(t)]);
% Obtain the solutions of the problem for two initial guesses:
SOLs1GUESS = bvp4c(dy,Res,SOLin1);
SOLs2GUESS = bvp4c(dy,Res,SOLin2);
% Compute numeric solutions of the problem within time-space:
y1 = deval(SOLs1GUESS,t);
y2 = deval(SOLs2GUESS,t);
subplot(211)
plot(t, y1(1,1:end), 'o'),
ylabel( 'y(t) solution'), hold all; grid on
LABEL{ii} =(['\leftarrow\omega = ' num2str(omega(ii))]);
legend(LABEL{:})
title(['Simulation of Sturm Liouville BVP: ', '-u"+\omega^2*u=sinh(t), BC: u(0)=u(1)=0'])
subplot(212)
plot(t, y2(1, :), 'p'), grid on
LABEL{ii} =(['\leftarrow\omega = ' num2str(omega(ii))]);
legend(LABEL{:}); hold all
end
gtext('Guess 1: sin and cos fcns', 'background', 'w')
gtext('Guess 2: numerical values', 'background', 'y')
xlabel 't', ylabel( 'y(t) solution')
hold off

类别

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by