Create functions in loop for increasing input

조회 수: 3(최근 30일)
Md. Nurul Anwar
Md. Nurul Anwar 2021년 10월 5일
댓글: Md Nurul Anwar 2021년 10월 28일
I need to define a function in a loop that increses the number of input. The function is attaches as screenshots
That is
pA_r(1)=@(t,s1) (function for n=1)
pA_r(2)=@(t,s1,s2) (function for n=2)
pA_r(3)=@(t,s1,s2,s3) (function for n=3)
where t, s1,...,sn are variables. I can't figure out where to start. I just need to define them so I can use then whenever I want. I very much appreciate the help.
  댓글 수: 1
Stephen
Stephen 2021년 10월 5일
You could use VARARGIN, but it would most likely be much easier to stick all of those S variables into one vector: MATLAB was designed to work very efficiently with vectors and matrices, whereas trying to work with lots of numbered variable names will make this task complex, slow, and very inefficient.
Judging by that formula, a simple loop would suffice (at most a recursive function).

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

답변(2개)

Mathieu NOE
Mathieu NOE 2021년 10월 5일
hello
my suggestion : work with one structure S that can grow in size inside your for loop
but you have only one argument (S) to pass to whatever function
t = (0:100)/100;
S.t = t;
for ci = 1:5
S(ci).x = sin(ci.*t);
out = myfunction(S);
end
function out = myfunction(S)
out = S; % dummy code
end
  댓글 수: 4
Mathieu NOE
Mathieu NOE 2021년 10월 26일
hello
I had some difficulties to understand how the code is supposed to work
I ended up doing quite a lot of modifications , so it "works" numerically speaking, but I am not sure it does what it's supposed to do
have a look and tell me where I did wrong
I understand there is a kind of recursion on pA_rad and that's why it's indexes for N and the current time index
but in your original code , there are some areas where i don't understand what your are doing like
pA(S.x(ci)) ?? in line :
pA_rad= (1-p_blood)*exp(-par.r*(tau-S.x(ci))).*pA_rad(need value from previous loop at current time)+(1-p_rad)^ci*(pA(tau)-exp(-par.r*(tau-S.x(ci))).*pA(S.x(ci)));
also S.x(ci) does not exist, but S(ci).x do
so far my code :
clc
clearvars
tau_max=100;
step=10;
h=tau_max/step;
s1=100;
p_blood=.2;
p_rad=.2;
par.r=1/60; %rate of blood stage infection clearance
par.omega=1/425 ; %hypnozoites death rate
par.alpha=1/332;% hypnozoites activation rate
for N =1:3
S(N).x =s1+(N-1)*(3*30);
for k=1:step
% t(j+1) = k*h;
% prob = myfunction(t(j+1),S,N);
tau(k) = k*h;
pA(k)=par.alpha*(exp(-par.r*tau(k))-exp(-(par.alpha+par.omega)*tau(k)))/(par.alpha+par.omega-par.r);
if N == 1
pA_rad(N,k) = (1-p_blood)*exp(-par.r*(tau(k)-S(N).x)).*0 +(1-p_rad)^N.*(pA(k)-exp(-par.r*(tau(k)-S(N).x)).*pA(k));
else
pA_rad(N,k) = (1-p_blood)*exp(-par.r*(tau(k)-S(N).x)).*pA_rad(N-1,k)+(1-p_rad)^N.*(pA(k)-exp(-par.r*(tau(k)-S(N).x)).*pA(k));
end
prob(k) = pA(k);
end
end
% SS=[S];
% function out = myfunction(tau,S,ci)
%
% pA=par.alpha*(exp(-par.r*tau)-exp(-(par.alpha+par.omega)*tau))/(par.alpha+par.omega-par.r);
% pA_rad= (1-p_blood)*exp(-par.r*(tau-S.x(ci))).*pA_rad(need value from previous loop at current time)+(1-p_rad)^ci*(pA(tau)-exp(-par.r*(tau-S.x(ci))).*pA(S.x(ci)));
% % this is where the problem is
% out = pA;
% end

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


Md Nurul Anwar
Md Nurul Anwar 2021년 10월 27일
Hi Mathieu, sorry for the messy code and thanks for your code. it's giving me the direction that I need.
In order to evaluate the pA_rad function, I need to evaluate pA at current time and fixed time S(N).x (drug time). That's why I wanted to defined
pA=@(time) par.alpha*(exp(-par.r*time)-exp(-(par.alpha+par.omega)*time))/(par.alpha+par.omega-par.r);
so I can evaluate it for both time.
the pA_rad(N) function uses N+1 inputs (current time and N fixed drug time). pA_rad(N) also use the value of pA_rad(N-1) which needs to be evaluated at drug times in sequence S(N).x, S(1).x,..., S(N-1).x i.e., the last drug time S(N).x would replace the current time in the previous loop. (I have marked in the attached picture)
So I need
if N == 1
pA_rad(N,k) = (1-p_blood)*exp(-par.r*(tau(k)-S(N).x)).*pA(S(N).x)+(1-p_rad)^N.*(pA(k)-exp(-par.r*(tau(k)-S(N).x)).*S(N).x));
else
pA_rad(N,k) = (1-p_blood)*exp(-par.r*(tau(k)-S(N).x)).*pA_rad(N-1,evaluated at S(N).x, S(1).x upto S(N-1).x)+(1-p_rad)^N.*(pA(k)-exp(-par.r*(tau(k)-S(N).x)).*pA(S(N).x));
end
I am not sure if these explain my situation
  댓글 수: 2
Md Nurul Anwar
Md Nurul Anwar 2021년 10월 28일
Thanks for your help Mathieu

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

Community Treasure Hunt

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

Start Hunting!

Translated by