evaluate function handle (determine the value of function handle using my code

1 vue (au cours des 30 derniers jours)
Please tell me how to determine the value of a function handle in a quick way.
hf12 = @(age) exp(-0.0625.*age-0.0134) % exp(age effect+time effect)
hf13 = 0
hf14 = 0
hf15 = @(age) exp(-9.65573+0.01844+0.08218*age+0.02246) % exp(intercept+ age effect+time effect)
hf11 = 0
hf21 = 0
hf23 = @(age) exp(-1.6660-0.1116.*age-0.0025) % exp(intercept+ age effect+time effect)
hf24 = @(age)exp(-8.96236+0.07691.*age + 0.00978) % assuming the death rate of male of same age(Hubener et al.)
hf25 = @(age)exp(-9.65573+0.08218.*age+0.02246) % self-mortality
hf22 = 0
hf31 = 0
hf32 = @(age) exp(-0.0625.*age-0.0134+0.0676) %exp(intercept+ age effect+time effect+marriage once before)
hf34 = 0
hf35 = @(age) exp(-9.65573+0.08218.*age+0.02246-0.11853)
hf33 = 0
hf41 = 0
hf42 = @(age) exp(-0.4176-0.0625-0.0134.*age)
hf43 = 0
hf45 = @(age) exp(-9.65573+0.08218.*age+0.02246-0.00415) %exp(intercept+ age effect+time effect+widowhood effect)
hf44 = 0
hf51 = 0
hf52 = 0
hf53 = 0
hf55 = 0
hf54 = 0
age =30
% Evaluate matrix Q at age=30
Q30 = [-(hf15(30)+hf12(30)), hf12(30), hf13, hf14, hf15(30); ...
hf21, -(hf23(30)+hf25(30)), hf23(30), hf24(30), hf25(30); ...
hf31, hf32(30), -(hf32(30)+hf35(30)), hf34, hf35(30); ...
hf41, hf42(30), hf43, -(hf42(30)+hf45(30)), hf45(30); ...
hf51, hf52, hf53, hf54, hf55]
It takes so long if I have to determine Q matrix for each age from 20 to 100 years. I want to figure out, how can I do this in some loop or some other way like:
% put all function handles in a matrix like
Q = [h11, hf12, hf13, hf14, hf15; ...
hf21, h22, hf23, hf24, hf25; ...
hf31, hf32, h33, hf34, hf35; ...
hf41, hf42, hf43, h44, hf45; ...
hf51, hf52, hf53, hf54, hf55]
% and then determine Q for each age
% Determine the value of "Q" for ages 20 to 100.

Réponse acceptée

Stephen23
Stephen23 le 9 Fév 2021
Defining lots and lots of function handles is not an efficient approach. One function is much simpler:
myfun(30)
ans = 5×5
0 0.1513 0 0 0.0008 0 0 0.0066 0.0013 0.0008 0 0.1619 0 0 0.0007 0 0.4139 0 0 0.0008 0 0 0 0 0
function hf = myfun(age)
hf = zeros(5,5);
hf(1,2) = exp(-0.0625.*age-0.0134); % exp(age effect+time effect)
hf(1,5) = exp(-9.65573+0.01844+0.08218*age+0.02246); % exp(intercept+ age effect+time effect)
hf(2,3) = exp(-1.6660-0.1116.*age-0.0025); % exp(intercept+ age effect+time effect)
hf(2,4) = exp(-8.96236+0.07691.*age + 0.00978); % assuming the death rate of male of same age(Hubener et al.)
hf(2,5) = exp(-9.65573+0.08218.*age+0.02246); % self-mortality
hf(3,2) = exp(-0.0625.*age-0.0134+0.0676); %exp(intercept+ age effect+time effect+marriage once before)
hf(3,5) = exp(-9.65573+0.08218.*age+0.02246-0.11853);
hf(4,2) = exp(-0.4176-0.0625-0.0134.*age);
hf(4,5) = exp(-9.65573+0.08218.*age+0.02246-0.00415);
end

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by