Passing parameters in boundary value problem using BVP4C

조회 수: 30 (최근 30일)
Saeid
Saeid 2019년 12월 1일
댓글: Christian Chamberlayne 2020년 3월 20일
For a system of boundary value problems I need to pass a parameter as a coefficient. Let's assume that I have defined the problem as:
solinit = bvpinit(linspace(0,10,50),[0 10]);
sol = bvp4c(@twoode,@twobc,solinit,a);
x = linspace(0,4);
y = deval(sol,x);
function dydx = twoode(x,y,a)
dydx = [ y(2); -a*exp(-x)+x*exp(-x)];
end
function res = twobc(ya,yb,a)
res = [ya(1); yb(1)-10*exp(-10)];
end
Where a is the parameter, and my first try was to add a to all the functions involved, but I receive the "Not enough input arguments" error. Is there a way to include the parameter a in the problem?

채택된 답변

Star Strider
Star Strider 2019년 12월 1일
Define ‘a’ in your workspace, then use this bvp4c call:
sol = bvp4c(@(x,y)twoode(x,y,a),@(ya,yb)twobc(ya,yb,a),solinit);
You are passing the extra parameter correctly, however bvp4c does not need to know about it, so only show bvp4c the arguments it wants, thus:
@(x,y)twoode(x,y,a)
and:
@(ya,yb)twobc(ya,yb,a)
The anonymous function construction (that you wrote correctly) will pick up ‘a’ from your workspace and pass it to your functions.
With this change (and specifying ‘a’), your code ran without error with this bvp4c call when I tested it.
  댓글 수: 3
Star Strider
Star Strider 2019년 12월 2일
As always, my pleasure!
I very much appreciate your compliment!
Christian Chamberlayne
Christian Chamberlayne 2020년 3월 20일
This is super helpful. I simularly was stuck with the same issue. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephan
Stephan 2019년 12월 1일
See here: passing extra parameters. I suggest to either use the anonymous functions or the nested functions approach. Both is easy to apply.

카테고리

Help CenterFile Exchange에서 Boundary Value Problems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by