Changing parameter in a function when called by bvp4c
8 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Star Strider
on 17 Aug 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on Boundary Value Problems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!