External function problem

Hello, I’m trying to use external_function option. My m file is as follows:
function a = exter(b)
if b<=0.2
a=0.02b;
elseif b>0.2
a=0.004+0.1
(b-0.2);
end
end

In mod file:
external_function(name=exter);

model(linear);
death=exter(infected);
end;

When I am running it works but the results of death are not right(I mean that death=0.02*infected always). What’s wrong with my m file?
How can be fixed this problem?

Thanks in advance.

Are you using stochastic simulations? In that case, the model will be approximated and you will lose the non-differentiability.

Thanks for your reply.
No, I’m using deterministic simulation

Then I would need to see the full files.

Many thanks.
baseline.mod (10.4 KB) exter.m (147 Bytes)

Your model is not linearly. So don’t specify

model(linear)
1 Like

One more question. When I’m using ramsey policy under perfect foresight setup and including death in planner’s objective function (for example 100death^2 , 50000death^2, 1000000*death^2 ) it works in cases of 100 ,1000000(small and very big weight) but in cases of middle weights I get the following:

Simulation of the perfect foresight model failed!
Switching to a homotopy method…

and after

Failed to solve perfect foresight model.
What’s the reason of the failure?

Here are my files:
baseline.mod (10.7 KB) exter.m (139 Bytes)

It’s hard to tell, but considerable size differences are often numerically problematic.

1 Like