call function in another function

조회 수: 7 (최근 30일)
StefSter
StefSter 2023년 1월 2일
이동: Rik 2023년 1월 3일
Hi all!
I have the following problem:
I use the Runge-Kutta algorithm to solve a differential equation - the (self written) function uses as input the differential equation (function handle or anonymus function) and some initial conditions, which are not relevant for my problem here.
Example with anonymus function:
phi = rungekutta(@(y) ([y(2); -sin(y(1))]));
Example with function handle:
function dydt = myfun(y)
dydt = [y(2); -sin(y(1))];
end
and then:
phi = rungekutta(myfun);
This works without any troubles. The problem appears if my differential equation has more than one input parameter.
function dydt = myfun(y,omega)
dydt = [y(2); -omega^2*y(1)];
end
Now I want to use different values of omega, but I am not sure how to say which variable is the input and which is the one to solve for. Something like:
omega = 1;
phi = rungekutta(myfun(y, omega));
but then Matlab asks for y, altough it is only the variable to solve for.
I hope i could formulate my problem in an understandable way.
Thanks,
Stefan

답변 (1개)

Torsten
Torsten 2023년 1월 2일
이동: Rik 2023년 1월 3일
phi = rungekutta(@(y)myfun(y, omega));
  댓글 수: 1
StefSter
StefSter 2023년 1월 2일
이동: Rik 2023년 1월 3일
Thank you, that was exactly what i was looking for

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by