Pass Additional Arguments into Guess Function for BVP

5 views (last 30 days)
In the example given here:
What would be the syntax (if it's even possible) to pass an additional argument(s) into the guess function?
This is how it is currently written:
function yinit = mat4init(x) % initial guess function
yinit = [cos(4*x)
-4*sin(4*x)];
end
It fails if I do this:
solinit = bvpinit(linspace(0,pi,10),@mat4init,lambda,param);
function yinit = mat4init(x,param) % initial guess function
yinit = [cos(param*x)
-4*sin(param*x)];
end
I want to pass arrays/parameters into the guess function because the call to the function to solve my BVP will occur within another code and the proper initial guess will evolve/change for each call.
Thanks!

Accepted Answer

Torsten
Torsten on 9 May 2025
Edited: Torsten on 9 May 2025
lambda = 15;
additional_parameters = 22;
solinit = bvpinit(linspace(0,pi,10),@(x)mat4init(x,additional_parameters),lambda);
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
additional_parameters = 22
sol = bvp4c(@mat4ode, @mat4bc, solinit);
fprintf('Fourth eigenvalue is approximately %7.3f.\n',...
sol.parameters)
Fourth eigenvalue is approximately 17.097.
xint = linspace(0,pi);
Sxint = deval(sol,xint);
plot(xint,Sxint)
axis([0 pi -4 4])
title('Eigenfunction of Mathieu''s Equation.')
xlabel('x')
ylabel('y')
legend('y','y''')
function dydx = mat4ode(x,y,lambda) % equation being solved
q = 5;
dydx = [y(2)
-(lambda - 2*q*cos(2*x))*y(1)];
end
function res = mat4bc(ya,yb,lambda) % boundary conditions
res = [ya(2)
yb(2)
ya(1)-1];
end
function yinit = mat4init(x,additional_parameters) % initial guess function
additional_parameters
yinit = [cos(4*x)
-4*sin(4*x)];
end
  4 Comments
Paul Safier
Paul Safier on 9 May 2025
This is it:
t2 = interp1(xx(2:end),diff(tstArray)/(dx),x,"linear","extrap");
Thanks for the help, @Torsten
Torsten
Torsten on 9 May 2025
Edited: Torsten on 9 May 2025
The problem is that you cannot interpolate the derivative at x if x is in the interval from xx(1) to xx(2) with the command
t2 = interp1(xx(2:end),diff(tstArray)/(dx),x);
Better compute an approximation to the derivative function using "gradient" instead of "diff" and before calling bvp4c:
lambda = 15;
xx = linspace(0,pi,20);
tstArray = cos(4*xx);
der_tstArray = gradient(tstArray,xx(2)-xx(1));
solinit = bvpinit(linspace(0,pi,10),@(x)mat4init(x,xx,tstArray,der_tstArray),lambda);
sol = bvp4c(@mat4ode, @mat4bc, solinit);
fprintf('Fourth eigenvalue is approximately %7.3f.\n',...
sol.parameters)
Fourth eigenvalue is approximately 17.097.
xint = linspace(0,pi);
Sxint = deval(sol,xint);
plot(xint,Sxint)
axis([0 pi -4 4])
title('Eigenfunction of Mathieu''s Equation.')
xlabel('x')
ylabel('y')
legend('y','y''')
function dydx = mat4ode(x,y,lambda) % equation being solved
q = 5;
dydx = [y(2)
-(lambda - 2*q*cos(2*x))*y(1)];
end
function res = mat4bc(ya,yb,lambda) % boundary conditions
res = [ya(2)
yb(2)
ya(1)-1];
end
function yinit = mat4init(x,xx,tstArray,der_tstArray) % initial guess function
t1 = interp1(xx,tstArray,x);
t2 = interp1(xx,der_tstArray,x);
yinit = [t1
t2];
%yinit = [cos(4*x)
% -4*sin(4*x)];
end

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!