How to customize a function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I need to amend my function f_lx.m. Today the function takes 2 parameters; t=age and param_1955=a vector with 3 scalars. I want different parameters to be used depending on age. The function looks like this
function res=f_lx(x,param)
a=param(1);
b=param(2);
c=param(3);
res = zeros(size(x));
ind = x>100;
res(ind) = a+b*exp(c*100)+(x(ind)-100)*0.001;
res(~ind) =a+b*exp(c*x(~ind));
end
Now I have more sets of parameters; param_1938 and param_1945. Given the age x, different parameters will be used - here is a table demonstrating what I mean
x parameter
65 param_1955
66 param_1955
67 param_1955
68 param_1955
69 param_1955
70 param_1945
71 param_1945
72 param_1945
73 param_1945
74 param_1945
75 param_1945
76 param_1945
77 param_1938
78 param_1938
79 param_1938
... param_1938
106 param_1938
How can I change my f_lx.m to consider other parameters given age?
1 comentario
Stephen23
el 29 de Oct. de 2018
Numbered parameter names is a sign that you are doing something wrong. You should use indexing to write simpler, more efficient code.
Respuestas (2)
KSSV
el 29 de Oct. de 2018
function res=f_lx(x,param)
a=param(:,1);
b=param(:,2);
c=param(:,3);
res = zeros(size(x));
ind = x>100;
res(ind) = a(ind)+b(ind).*exp(c(ind)*100)+(x(ind)-100)*0.001;
res(~ind) =a(~ind)+b(~ind).*exp(c(~ind).*x(~ind));
end
Your x and param can me arrays now.
1 comentario
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!