0

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();
0

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

3 Comments

I understand, but when I want to pass the function d(x,u) in general to an integral for example, it gives the same error. This was just a test, i will edit my main question and add the 'main' code where we get the error.
@Joachim I think what you are trying to achieve is to use symbolic variables , you cannot just pass the value of undeclared variables to a function , more information about symbolic variables here
Exactly what the problem was. You made my day, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.