How to create a local function during the code run

조회 수: 1 (최근 30일)
Ivan Khomich
Ivan Khomich 2020년 6월 5일
댓글: Steven Lord 2020년 6월 5일
Hello, everyone!
I have a question about opportunity of creating local functions during the code run.
For example I have this code:
function diffprobe
x0=[1 1];
for i=1:2
a=strcat('@odeEvent', num2str(i));
str = str2func(a);
options(i)=odeset('Events', str, 'RelTol',1e-9,'AbsTol',1e-9);
[~,~,TE,XE,IE]=ode45(@System, [0 inf],x0, options(i));
end
function [value, isterminal, direction]=odeEvent1(~,x)
value=[x(1)];
isterminal=[1];
direction=0;
function [value, isterminal, direction]=odeEvent2(~,x)
value=[x(2)];
isterminal=[1];
direction=0;
function RPF=System(~,x)
RPF=[x(2);...
-x(2)-1];
It is work right, but i have an interest to somehow create local functions odeEvent1 and odeEvent2 during the cycle, because of I need to generalize the method on high order systems so the cycle might go not from 1 to 2, but from 1 to n, and obviously I don't want to rewrite the code every time, when I try the new system.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 5일
Dynamically creating a function name is not a recommended coding practice. Following shows how to do such a thing more efficiently
function diffprobe
x0=[1 1];
for i=1:2
options(i)=odeset('Events', @(t,x) odeEvent(t,x,i), 'RelTol',1e-9,'AbsTol',1e-9);
[~,~,TE,XE,IE]=ode45(@System, [0 inf],x0, options(i));
end
function [value, isterminal, direction]=odeEvent(~,x,i)
value=[x(i)];
isterminal=[1];
direction=0;
function RPF=System(~,x)
RPF=[x(2);...
-x(2)-1];
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 6월 5일
Yes, @(t,x) is the requirement of the solver(), whereas odeEvent(t,x,i) shows how you pass the solver variables to your odeEvent functions. You can read the documentation related to anonymous functions to see in detail what is happening here.
Steven Lord
Steven Lord 2020년 6월 5일
What Ivan Khomich wrote:
options(i)=odeset('Events', @(t,x,i) odeEvent, 'RelTol',1e-9,'AbsTol',1e-9);
ode45 will call the anonymous function stored in the Events option with two inputs (that is what it is documented to do) and so the variable named i will be undefined. The anonymous function will call odeEvent with zero inputs.
What Ameer Hamza wrote:
options(i)=odeset('Events', @(t,x) odeEvent(t,x,i), 'RelTol',1e-9,'AbsTol',1e-9);
ode45 will call the anonymous function stored in the Events option with two inputs. The anonymous function will call odeEvent with three inputs, the two passed into it by ode45 and one the anonymous function "remembered" because that variable existed when the anonymous function was created.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by