Anonymous ODE function syntax for multipoint BVP
Show older comments
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.
Answers (1)
Sulaymon Eshkabilov
on 20 May 2021
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
Categories
Find more on Boundary Value Problems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!