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 Center および File ExchangeBoundary Value Problems についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by