User request within a function

2 views (last 30 days)
MaJoBo
MaJoBo on 6 Jun 2017
Commented: MaJoBo on 6 Jun 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

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!