简体   繁体   中英

Matlab nested function handle

We are working on a assignment for datacommunications where we have to declare a nested function handle in matlab. We did a few tests on how matlab handles this but nothing works.

This is one of the tests:

clear;
f = @(x) x.^2;
d = @(x,u) f(x) + u;
disp(d(x,u));

With this test matlab gives a undefined function or variable x. What do we have to do to fix this?

Kind regards

Full code:

[~, distr] = make_probability_functions(Quantization.filename);
%distr is a matrix of certain values

x_0 = 127.5;
M = 8;
delta= 10:1:30;

q = @(i) x_0+(i-(M+1)/2);

r = @(i) x_0+((2*i-M)*delta)/2;

f = @(u,i) ((q(i)-u).^2)*distr(u);

%GRANULAR
int_gran=@(delta,i) int(f,u,q(i)-delta/2,q(i)+delta/2);
s_gran=@(delta) symsum(int_gran(delta,i),i,0,M);

%OVERLOAD
s_ol=@(delta) int(@(u)f(u,1),u,-inf,q(1)-delta/2)+int(@(u)f(u,M),q(m)+delta/2,inf);

%GKD
s_e=@(delta) s_gran(delta)+s_ol(delta);

%plot GKD
plot(delta,s_e(delta),delta,s_gran(delta),delta,s_ol(delta));

Error:

Undefined function or variable 'u'.

Error in Quantization>@(delta,i)int(f,u,q(i)-delta/2,q(i)+delta/2)

Error in Quantization>@(delta)symsum(int_gran(delta,i),i,0,M)

Error in Quantization>@(delta)s_gran(delta)+s_ol(delta) (line 59)
            s_e=@(delta) s_gran(delta)+s_ol(delta);

Error in Quantization.determine_optimal_uniform_quantizer (line 62)
            plot(delta,s_e(delta),delta,s_gran(delta),delta,s_ol(delta));

Error in script_run (line 1)
Quantization.determine_optimal_uniform_quantizer();

You have to pass actual values to d . The issue is that the x that you're passing to d is not defined. There is no issue with the anonymous functions themselves.

f = @(x) x.^2;
d = @(x,u) f(x) + u;

d(1, 2)
%   3

If you want to use x and u as inputs to d , you'll need to define them

x = 1; u = 2;
d(x, u)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM