How to define a function with multiple handle in a loop?

2 visualizaciones (últimos 30 días)
Md Nurul Anwar
Md Nurul Anwar el 11 de Sept. de 2022
Comentada: Md Nurul Anwar el 15 de Sept. de 2022
I have a situation where I need to use a function that can take different number of input (depnding upon the number of treatment considered).
The function has a general expression (Attached as Pic for readability)
Currently I am defining the function (for different number of treatment) seperately (as bellow)
%% Parameters
par.a=80/365;% %; %bitting frequiency (per day)
par.g=1/10; %mosquito death rate (per day)
par.n=12; %duration of sporogony in mopsquito
par.b=.5; %transmission probability: mosquito to human
par.c=.23; %transmission probability: human to mosquito
par.r=1/60; %rate of blood stage infection clearance
par.omega=1/425 ; %hypnozoites death rate
par.nu=8.5; %Number of hypnozoites per infection
par.alpha=1/332;%1/332;
p_blood=.9; %treatment effect
p_rad=.9; %treatment effect
% tau is the current time
% s1,s2,s3 are treatment time.
pA=@(tau) par.alpha*(exp(-par.r*tau)-exp(-(par.alpha+par.omega)*tau))/(par.alpha+par.omega-par.r); %Base function without treatment
pA_rad=@(tau,s1) (1-p_blood)*exp(-par.r*(tau-s1)).*pA(s1)+(1-p_rad)*(pA(tau)-exp(-par.r*(tau-s1)).*pA(s1)); %with one treatment
pA_rad2=@(tau,s1,s2) (1-p_blood)*exp(-par.r*(tau-s2)).*pA_rad(s2,s1)+(1-p_rad)^2*(pA(tau)-exp(-par.r*(tau-s2)).*pA(s2)); %with two treatment
pA_rad3=@(tau,s1,s2,s3) (1-p_blood)*exp(-par.r*(tau-s3)).*pA_rad2(s3,s1,s2)+(1-p_rad)^3*(pA(tau)-exp(-par.r*(tau-s3)).*pA(s3)); %with three
as I couldn't figure out how to define these in a single function. The main issue is that as the number of treatment increases, the function handle increases as well and the function with larger handles utilise the function with lower handles for different inputs (as is attached pic). This is making me crazy. Is there any way that I can define these in a loop or nested function? Any help is much appreciated.

Respuesta aceptada

Jan
Jan el 11 de Sept. de 2022
Is there a need to use an anonymous function? Why not writing a standard function?
function y = pA(tau, s1, s2, s3)
a=80/365;% %; %bitting frequiency (per day)
g=1/10; %mosquito death rate (per day)
n=12; %duration of sporogony in mopsquito
b=.5; %transmission probability: mosquito to human
c=.23; %transmission probability: human to mosquito
r=1/60; %rate of blood stage infection clearance
omega=1/425 ; %hypnozoites death rate
nu=8.5; %Number of hypnozoites per infection
alpha=1/332;%1/332;
p_blood=.9; %treatment effect
p_rad=.9; %treatment effect
% tau is the current time
% s1,s2,s3 are treatment time.
y0 = alpha*(exp(-r*tau)-exp(-(alpha+omega)*tau))/(alpha+omega-par.r);
switch nargin
case 1
y = y0; %Base function without treatment
case 2
y = (1-p_blood)*exp(-r*(tau-s1)).*pA(s1)+(1-p_rad)*(y0-exp(-r*(tau-s1)).*pA(s1)); %with one treatment
case 3
y = (1-p_blood)*exp(-r*(tau-s2)).*pA(s2,s1)+(1-p_rad)^2*(y0-exp(-r*(tau-s2)).*pA(s2)); %with two treatment
case 4
y = (1-p_blood)*exp(-r*(tau-s3)).*pA(s3,s1,s2)+(1-p_rad)^3*(y0-exp(-r*(tau-s3)).*pA(s3)); %with three
otherwise
error('Too many inputs');
end
end
  8 comentarios
Jan
Jan el 14 de Sept. de 2022
@Md Nurul Anwar: Please care for posting the correct formula directly in the question the next time.
function y = pA(t, p, varargin)
N = nargin - 2;
if N == 0 % No s as inputs:
y = p.alpha / (p.alpha + p.mu - p.gamma) * ...
(exp(-p.gamma * t) - exp(-(p.alpha + p.mu) * t)) ;
elseif N > 0 % s1..sN are provided as inputs - recursive call:
sN = varargin{N};
y = (1 - p.p_blood) * exp(-p.gamma * (t - sN)) * ...
pA(sN, p, varargin{1:N-1}) + ...
(1 - p.p_rad)^N * (pA(t, p) - exp(-p.gamma * (t - sN)) * pA(sN, p));
else
error('need parameters')
end
end
Please test this, if it produces the correct answers. It works with an arbitrary number of inputs s1, ...sN limited by the recursion depth of 500. If this is a problem or the function is time-critical, convert it to a loop.
An option would be to provide s as vector instead of a list of scalars.
Md Nurul Anwar
Md Nurul Anwar el 15 de Sept. de 2022
It works! Thanks a lot Jan.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by