Changing parameter in a function when called by bvp4c

8 次查看(过去 30 天)
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).
.

更多回答(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