Changing parameter in a function when called by bvp4c

8 visualizaciones (últimos 30 días)
Lewis
Lewis el 17 de Ag. de 2021
Comentada: Star Strider el 17 de Ag. de 2021
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?

Respuesta aceptada

Star Strider
Star Strider el 17 de Ag. de 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 comentarios
Lewis
Lewis el 17 de Ag. de 2021
Thank you, it did work with k definied before the bvp4c call.
Star Strider
Star Strider el 17 de Ag. de 2021
As always, my pleasure!
.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by