Changing parameter in a function when called by bvp4c

조회 수: 8 (최근 30일)
Lewis
Lewis 2021년 8월 17일
댓글: Star Strider 2021년 8월 17일
I have a system of ode's I want to solve using bvp4c, which depend on a (known) parameter, e.g
function dydx = bvpeq(x,y)
k = 2;
dydx(1) = y(2) + k*x
dydx(2) = y(1) + k*x
end
Plus a guess bvpguess and boundry conditions bvpbc which don't depend on k. Then I want to vary k and have an array which contains the first few points of the solution for each k.
xmesh= linspace(1,5);
solinit = bvpinit(xmesh, @bvpguess);
sol = bvp4c(@bvpeq, @bvpbc, solinit);
y1vals(1,1:10) = sol.y(1,1:10);
y2vals(1,1:10) = sol.y(2,1:10);
xvals(1,1:10) = sol.x(1:10);
I then vary k by manually chaning the value of k and the rows of y1vals, y2vals, and xvals that I'm inputting in, but I want to do this with a for loop
for j = 1:10
xmesh= linspace(1,5);
solinit = bvpinit(xmesh, @bvpguess);
sol = bvp4c(@bvpeq, @bvpbc, solinit);
y1vals(j,1:10) = sol.y(1,1:10);
y2vals(j,1:10) = sol.y(2,1:10);
xvals(j,1:10) = sol.x(1:10);
end
function dydx = bvpeq(x,y)
k = j;
dydx(1) = y(2) + k*x
dydx(2) = y(1) + k*x
end
But j is an unrecognised variable in the function, is there a way I can change the value of k in bvpeq, or generate 10 functions (bvpeq1, bvpeq2, etc) with different values of k?

채택된 답변

Star Strider
Star Strider 2021년 8월 17일
If you want to pass ‘k’ as an extra parameter, see the documentation section on Passing Extra Parameters for relevant examples.
So:
function dydx = bvpeq(x,y,k)
dydx(1) = y(2) + k*x
dydx(2) = y(1) + k*x
end
and:
sol = bvp4c(@(x,y)bvpeq(x,y,k), bvpbc, solinit);
I have not tested that here, however it should work (with ‘k’ defined before the bvp4c call).
.
  댓글 수: 2
Lewis
Lewis 2021년 8월 17일
Thank you, it did work with k definied before the bvp4c call.
Star Strider
Star Strider 2021년 8월 17일
As always, my pleasure!
.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by