Error using function handles in for loop

I have a function, pi_t, which is defined using a function handle but also is a function i in the for loop. When I try to incorporate my function handle inside the for loop it gives an error for pi_t saying "Nonscalar arrays of function handles are not allowed; use cell arrays instead." What I want to do is to have Np amount of pi_t values with the function handle but with the dpi(i) value correctly substituted in the equation for pi_t. Any ideas how to effectively use a function handle in a for loop?
a_pi20 = 1.50;
b_pi20 = 2.25;
c_pi20 = 3;
dist_pi20 = makedist('Triangular', a_pi20, b_pi20, c_pi20);
pi08 = 8.50;
Np = 1000;
for i = 1:Np
pi20 = random(dist_pi20);
dpi(i) = (pi20 - pi08)/(12*pi08);
pi_t(i) = @(x)abs(-dpi(i)*x(1)*pi08 - pi08);
end

回答(2 个)

pi_t{i} = @(x)abs(-dpi(i)*x(1)*pi08 - pi08);
and to call it, you would need something like
pi_t{17}(37.8)

2 个评论

Yes but I need the -dpi(i) value to substitute into pi_t(i) every time. I cannot call pi_t as you did because I am solving for x later on using optimization. What I need is that every iteration dpi has a new value based on its statistical distribution and I need that to go into the pi_t term. When I type in pi_t(1) after using your suggestion it gives me @(x)(-dpi(i)*x(1)*pi08-pi08). What I want to do is have it give me for example dpi(1) = 0.06, so pi_t(1) = @(x)(-0.06*x(1)*pi08-pi08). Any ideas how to do this?
fzero( pi_t{17}, x0 )
"When I type in pi_t(1) after using your suggestion it gives me @(x)(-dpi(i)*x(1)*pi08-pi08)"
p{1} would give that, and it is fine. It has "captured" the value that dpi(i) had at the time the function handle was created. It will not display as @(x)(-0.06*x(1)*pi08-pi08) but it will execute that way. You can use
temp = functions(pi_t{1});
temp2 = temp.workspace{1};
temp2.dpi
temp2.i
to see that it has copied dpi and i; those are what it will use at execution time.

请先登录,再进行评论。

You can follow with out suing function as below. (Note that x is not defined, I have taken some random value).
a_pi20 = 1.50;
b_pi20 = 2.25;
c_pi20 = 3;
dist_pi20 = makedist('Triangular', a_pi20, b_pi20, c_pi20);
pi08 = 8.50;
Np = 1000;
pi_t = zeros(1,Np) ;
dpi = zeros(1,Np) ;
x = rand ;
for i = 1:Np
pi20 = random(dist_pi20);
dpi(i) = (pi20 - pi08)/(12*pi08);
pi_t(i) = abs(-dpi(i)*x*pi08 - pi08);
end
If you want to go by function. You have to define the function first and then call the function in the loop. An example code given below would help you to understand.
f = @(x) x+2 ;
y = zeros(1,10) ;
for i = 1:10
y(i) = f(i) ;
end
Also note that, your entire code can be vectorised with out using loop. As a beginner it is always good to know loops and then vectorize it.

5 个评论

I like the second option because i need the function handles for later on. However, in that second option pi_t is a function of dpi, which needs to be in the for loop. Won't i get an error defining pi_t out of the loop before dpi?
Follow like this:
f = @(x,y) x+y+2 ;
A = zeros(1,10) ;
for i = 1:10
y = i-1 ;
A(i) = f(i,y) ;
end
functions needs x and y...y I am calculating sending to function.
a_pi20 = 1.50;
b_pi20 = 2.25;
c_pi20 = 3;
dist_pi20 = makedist('Triangular', a_pi20, b_pi20, c_pi20);
pi08 = 8.50;
Np = 1000;
x = rand ;
pi_tfun = @(x,dpi)abs(-dpi*x*pi08 - pi08);
dpivec = zeros(1,Np) ;
pi_t = zeros(1,Np);
for i = 1:Np
pi20 = random(dist_pi20);
dpi = (pi20 - pi08)/(12*pi08);
dpivec(i) = dpi ;
pi_t(i) = pi_tfun(x,dpi) ;
end
I see why you had to make x = rand but the issue is that I solve for x later on in an optimization so I can't define it earlier like you did. Is there a way to do this without defining what x is initially?
You have not mentioned x so I have taken it random. If you define what is x it can be included in the code.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

评论:

2017-4-27

Community Treasure Hunt

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

Start Hunting!

Translated by