Some questions about anonymous functions

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 Kommentare

Steven Lord
Steven Lord am 6 Jun. 2020
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.
Can you show us the actual differential equations you're trying to solve, and indicate which of those equations you're trying to solve at each step?
Ivan Khomich
Ivan Khomich am 6 Jun. 2020
Bearbeitet: Ivan Khomich am 6 Jun. 2020
Ok, I have an example of system:
F1=[u];%First step(here u - is the amplitude of control)
F2=[x(2);...
-x(2)+u];%Second step
F=[x(2);...
-x(2)+x(3);...
-x(3)+u];%Third step
The main point of why I want to do this because of for each step I have a different package of .m files. But they are pretty similar one to another and only difference between their work consists in different equations.
So if I could compose this three systems in something like array and transfer to another .m-files I would decrease number of similar .m-files in three times and so on decrease number of operations I need to change the system.
Can you write a custom function (not anonymous)?
function res = F(t,x,case1)
switch
case 1
res=[u];%First step(here u - is the amplitude of control)
case 2
res=[x(2);...
-x(2)+u];%Second step
case 3
res=[x(2);...
-x(2)+x(3);...
-x(3)+u];%Third step
end
end
Ivan Khomich
Ivan Khomich am 13 Jun. 2020
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.
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?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 6 Jun. 2020

Kommentiert:

am 13 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by