Some questions about anonymous functions
Ältere Kommentare anzeigen
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
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
am 6 Jun. 2020
Bearbeitet: Ivan Khomich
am 6 Jun. 2020
darova
am 13 Jun. 2020
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
am 13 Jun. 2020
J. Alex Lee
am 13 Jun. 2020
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?
Antworten (0)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!