call function in another function

8 次查看(过去 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));

类别

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