User request within a function

2 visualizaciones (últimos 30 días)
MaJoBo
MaJoBo el 6 de Jun. de 2017
Comentada: MaJoBo el 6 de Jun. de 2017
Hello,
I am using a function to solve a coupled differential equation.
function [vec] = ode45func(x,z)
k = input('Value for k: ');
vec=[
z(1);
-4*z(1)-0.1*z(2)+k*(z(3)-z(1));
z(3);
-4*z(3)-0.1*z(4)+k*(z(1)-z(3))
];
end
As you can see, I try to get an user input when I call the function, to give k a value.
But I only get a loop of the input request. It just asks again and again for user input...
Any Ideas? Thank you

Respuesta aceptada

Steven Lord
Steven Lord el 6 de Jun. de 2017
The ODE solvers call the function you pass into them repeatedly. I suspect you want to ask for a value for k once at the start of the process of solving your ODE and to solve an ODE defined using that parameter value. If that's the case, don't include that in your ODE function. Parameterize your ODE function instead, asking for the value of k before you call ode45 and passing that into your ODE function as an additional parameter. The examples on that documentation page all use fzero, but the same techniques are also applicable for ode45.
  1 comentario
MaJoBo
MaJoBo el 6 de Jun. de 2017
Thank you for your reply! That was a huge help!
Have a nice day!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by