Pass Additional Arguments into Guess Function for BVP

조회 수: 6 (최근 30일)
Paul Safier
Paul Safier 2025년 5월 9일
편집: Torsten 2025년 5월 9일
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