Some questions about anonymous functions

2 ビュー (過去 30 日間)
Ivan Khomich
Ivan Khomich 2020 年 6 月 6 日
コメント済み: J. Alex Lee 2020 年 6 月 13 日
Hello everyone!
I have some troubles with understanding of field of using anonymous functions.
The main idea contains something like this:
I have a system of differential equations, so every ode solver gets this system from the local function @odeFunc(t,x), but I need to solve this system...something like stepwise, i.e. at first I solve the first equation of the system, do something with results. Then I add second equation and solve the resulting system. And so on.
So the first question: can I somehow choose needed equations from beforehand given system?
My assumption that I can create an function handle array
f={@(x) f1;
@(x) f2;
...}
and then somehow extract needed functions from this array and form a matrix from them needed for the solver on each step.
The second question is about how to transer this array or it's parts from the main function to another? The simplest case is how to transfer different right parts of the ode system to function @odeFunc(t,x)?
f={@(x) f1;
@(x) f2;
...}
[T,X]=ode45(@odeFunc, tspan, x0);
function RPF=odeFunc(t,x)
%variable rigth part of ode system
  5 件のコメント
Ivan Khomich
Ivan Khomich 2020 年 6 月 13 日
Thank you for your answer. It is an ok variant to solve the problem of switching odes, but the question was: is there a possibility to transfer function from one .m-file to another. Or maybe some ground why it is pointless and only worsen the code.
J. Alex Lee
J. Alex Lee 2020 年 6 月 13 日
Do you think you need duplication of source code across m-files? I don't think this is productive...
But you can always refer to other functions from functions...if you have the core functions somewhere "above" in your hierarchy of m-file groups, something like
function res = localFn(t,x,step)
res = MasterResFn(t,x,step)
end
and have the MasterResFn be something similar to what darova suggests. Would this accomplish what you want?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by