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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by