Pass Additional Arguments into Guess Function for BVP
    조회 수: 6 (최근 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!
댓글 수: 0
채택된 답변
  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);
sol = bvp4c(@mat4ode, @mat4bc, solinit);
fprintf('Fourth eigenvalue is approximately %7.3f.\n',...
            sol.parameters)
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
  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)
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



