How to change anonymous function to take array parameter and subject to ranges

3 views (last 30 days)
Hi, I have a working anonymous function that takes a vector of size 1x3 and I want to expand this to 3x3. The function is named mu_x and looks like this
mu_x=@(t) f_lx(t,param);
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
The 1x3 param is now [a b c] and the 3x3 parameter I want to use is subject to x such as:
x=65-59 param(:,1)
x=70-76 param(:,2)
x=77-106 param(:,3)
just to clarify the new param looks something like this
a1 b1 c1
a2 b2 c2
a3 b3 c3
How can the anonymous function be changed to consider the ranges of x and new format of param?

Accepted Answer

James Tursa
James Tursa on 31 Oct 2018
Edited: James Tursa on 31 Oct 2018
You need to be careful with how you build your anonymous function:
mu_x = @(t) f_lx(t,param);
In the above line, t is an argument that can change with every call, but param is a fixed snapshot of whatever it happens to be at the time you create mu_x. Changing param downstream in your code will have no effect on mu_x. The param in mu_x will still be that fixed snapshot of the param that existed at the time mu_x was created. If you want to use a new param with mu_x, you will need to recreate mu_x from scratch (i.e., execute the above line again).
Bottom line: If you originally created mu_x with a 1x3 param, and then downstream in your code change param in any way (to a new 1x3 or to a 3x3 etc), then you will have to recreate mu_x from scratch in order for it to use that new param.
Simpler: Just call f_lx directly ... what do you need that mu_x for?

More Answers (1)

Orongo
Orongo on 31 Oct 2018
I tried following change in the function but get the error
Operands to the || and && operators must be convertible to logical scalar values.
Error in f_lx (line 19)
ind1 = x>=65 && x<=69;
I know my solution won't win the price for code prettiness, but it hopefully shows you the logic I'm after.
function res=f_lx(x,param)
res = zeros(size(x));
a1=param(1,1); a2=param(2,1); a3=param(3,1);
b1=param(1,2); b2=param(2,2); b3=param(3,2);
c1=param(1,3); c2=param(2,3); c3=param(3,3);
ind1 = x>=65 && x<=69;
ind2 = x>=70 && x<=76;
ind3 = x>=77 && x<=100;
ind4 = x>100;
res(ind1) =a1(ind1)+b1(ind1).*exp(c1(ind1).*x(ind1));
res(ind2) =a2(ind2)+b2(ind2).*exp(c2(ind2).*x(ind2));
res(ind3) =a3(ind3)+b3(ind3).*exp(c3(ind3).*x(ind3));
res(ind4) = a3(ind4)+b3(ind4).*exp(c3(ind4)*100)+(x(ind4)-100)*0.001;
end
  4 Comments
Orongo
Orongo on 31 Oct 2018
Changing to & helped but the function is not working. I'm calling f_lx by following
exp(-integral(mu_x,0,66))
and get the error
Index exceeds matrix dimensions.
Error in f_lx (line 24)
res(ind1) =a1(ind1)+b1(ind1).*exp(c1(ind1).*x(ind1));
Orongo
Orongo on 31 Oct 2018
Torsten, I might understand this differently. For example a1(ind1) will be a1 if ind1 is true, is this not right?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!