User request within a function

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

采纳的回答

Steven Lord
Steven Lord 2017-6-6
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 个评论
MaJoBo
MaJoBo 2017-6-6
Thank you for your reply! That was a huge help!
Have a nice day!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by