Pass Additional Arguments into Guess Function for BVP

3 次查看(过去 30 天)
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!

采纳的回答

Torsten
Torsten 2025-5-9
编辑:Torsten 2025-5-9
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 个评论
Paul Safier
Paul Safier 2025-5-9
This is it:
t2 = interp1(xx(2:end),diff(tstArray)/(dx),x,"linear","extrap");
Thanks for the help, @Torsten
Torsten
Torsten 2025-5-9
编辑:Torsten 2025-5-9
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by