Unrecognized function error in bvp4c (in boundary conditions)

I have a diiferential equation:
where f(r) is known and interpolated with same mesh size.
Boundary conditions:
I have implemeted bvp4c using matlab documentation:
A = 2;
B = 1.5;
load('gr_in.mat');
rc = gr(:,2);
N = 100;
r = linspace(rc(1),rc(end),N);
fr = interp1(gr(:,2),gr(:,1),r,'spline');
fr=fr';
opts = bvpset('FJacobian',@jac,'RelTol',0.01,'AbsTol',0.01,'Stats','on');
rmesh = linspace(rc(1), rc(end), N);
solinit = bvpinit(rmesh, [1; 1]);
sol = bvp5c(@(r,y) bvpfcn(r,y,A,B,fr), bcfcn(ya,yb), solinit, opts);
function dydr = bvpfcn(r,y,A,B,fr) % equation to solve
dydr = [y(2)
-2*y(2)/r+A*y(1)-B*fr];
end
%---------------------------------
function res = bcfcn(ya,yb) % boundary conditions
res = [ya(1)-1
yb(1)];
end
%---------------------------------
function dfdy = jac(r,y,A) % analytical jacobian for f
dfdy = [0 1
A -2/r];
end
I get following error:
Unrecognized function or variable 'ya'.
Error in Untitled (line 11)
sol = bvp5c(@(r,y) bvpfcn(r,y,A,B,fr), bcfcn(ya,yb), solinit, opts);

 Réponse acceptée

Torsten
Torsten le 26 Oct 2023
Modifié(e) : Torsten le 26 Oct 2023
A = 2;
B = 1.5;
g = load('gr_in.mat');
gr = g.gr;
rc = gr(:,2);
figure(1)
plot(gr(:,2),gr(:,1))
N = 100;
r = linspace(rc(1),rc(end),N);
fr = @(r)interp1(gr(:,2),gr(:,1),r,'spline');
opts = bvpset('FJacobian',@(r,y)jac(r,y,A),'RelTol',0.01,'AbsTol',0.01,'Stats','on');
rmesh = linspace(rc(1), rc(end), N);
solinit = bvpinit(rmesh, [1; 1]);
sol = bvp5c(@(r,y) bvpfcn(r,y,A,B,fr), @bcfcn, solinit, opts);
The solution was obtained on a mesh of 50 points. The maximum error is 5.407e-03. There were 1861 calls to the ODE function. There were 29 calls to the BC function.
figure(2)
plot(sol.x,sol.y(1,:))
function dydr = bvpfcn(r,y,A,B,fr) % equation to solve
dydr = [y(2)
-2*y(2)/r+A*y(1)-B*fr(r)];
end
%---------------------------------
function res = bcfcn(ya,yb) % boundary conditions
res = [ya(1)-1
yb(1)];
end
%---------------------------------
function dfdy = jac(r,y,A) % analytical jacobian for f
dfdy = [0 1
A -2/r];
end

2 commentaires

rc(1) is non zero.
I have attached the data file.
Now i get this error:
Error using bvparguments (line 108)
Error in calling BVP5C(ODEFUN,BCFUN,SOLINIT,OPTIONS):
The derivative function ODEFUN should return a column vector of length 2.
Error in bvp5c (line 135)
bvparguments(solver_name,ode,bc,solinit,options);
Error in Untitled (line 13)
sol = bvp5c(@(r,y) bvpfcn(r,y,A,B,fr), @bcfcn, solinit, opts);
Torsten
Torsten le 26 Oct 2023
Modifié(e) : Torsten le 26 Oct 2023
It gives results, but I doubt it makes sense to use such a function as fr as input to a boundary value problem (see above).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by