User request within a function

조회 수: 2 (최근 30일)
MaJoBo
MaJoBo 2017년 6월 6일
댓글: MaJoBo 2017년 6월 6일
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