Skip to content

Investigate and fix errors when using nested functions in a RHS #9

@Schlevidon

Description

@Schlevidon

Summary

It seems like IFDIFF is currently not able to correctly handle nested functions in a RHS. Investigate the following example and implement a fix.

Example

ODE system

An example of a RHS with nested functions that will produce errors during preprocessing.

%%% nested functions are bad !!

function dx = switchedSystem1(t,x,p)
    function out = q1(t)
        out = 50/27*t.^2 - 100/3*t + 400/3;
    end
    function out = q2(t, x, p)
        out = (1000 - x(2))*(p(1)*(t-19) + p(2));
    end

dx = zeros(2,1);

if x(1) <= 750
    dx(1) = q1(t);
else
    dx(1) = q1(t) - q2(t,x,p);
end

if x(1) <= 750
    dx(2) = 0;
else
    if x(1) <= 800
        dx(2) = -300;
    else
        dx(2) = 0;
    end
end

Ideas for a potential fix

  • Nested functions can probably be treated in the same way as the rest of the RHS, i.e. simply keep them and replace statements that induce control flow (i.e. if, abs, sign, etc.) with ctrlif.
    • But consider if there could be any caveats. Do we need a function_index like in external helper functions?
  • The source of the issue may be the fact that the first statement in the body of the RHS function is a function definition, which then causes something to go wrong when processing the mtree. However, this is still pure speculation and further investigation is required.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions