How to customize a function
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 29 Okt. 2018
Numbered parameter names is a sign that you are doing something wrong. You should use indexing to write simpler, more efficient code.
Antworten (2)
KSSV
am 29 Okt. 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 Kommentar
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!