call function in another function

7 Ansichten (letzte 30 Tage)
StefSter
StefSter am 2 Jan. 2023
Verschoben: Rik am 3 Jan. 2023
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

Antworten (1)

Torsten
Torsten am 2 Jan. 2023
Verschoben: Rik am 3 Jan. 2023
phi = rungekutta(@(y)myfun(y, omega));
  1 Kommentar
StefSter
StefSter am 2 Jan. 2023
Verschoben: Rik am 3 Jan. 2023
Thank you, that was exactly what i was looking for

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by