Issue: Confusing: Index must be a positive integer or logical - matlab

I've got such a problem using MATLAB:
I wrote this function:
function E = f(x, lamda)
E = 1 - exp(-lamda * x);
end
When I write: Prob = f(1000, lamda); where lamda = 3.4274e-004
I get this error:
??? Attempted to access f(1000,0.000341565); index must be a positive integer or logical.
I understand that it requires a positive integer, but why ? I need lamda to be real. What's the problem here ? Can you, please, tell me where I'm wrong?

You have a function f and a variable f declared at the same time. Do clear f; then try your code again. What's happening here is that the variable declaration takes precedence over your function and so doing f would try and access the variable f first.
If you're using f as a variable somewhere and can't change this, then rename your function to be something other than f... perhaps... comp or something. Once you do this, make sure you change your file name so that it's called comp.m, then do:
Prob = comp(1000, lamda);

Your error message indicates that there is a variable called f in your workspace and matlab thinks you are trying to access its elements. Remove the variable f with clear('f') or rename the function to something else and you should be fine.

Related

Operations with function handle in matlab

Could you please help me with the following issue: I have the following function handle:
r1 = #(lambda) b + lambda*(r - b); % r and b are vectors of return data
I want to find the optimal lambdas that set me a mean function to zero, for a given set of powers within that function. What I tried to do and didn't work, as it returns me an error for undefined operators for input arguments of type 'function_handle' is:
lambda0 = 0.3;
for a = 2:10 %power coefficient
S1(a) = fzero(mean((r - b)*r1.^(1/(a - 1))),lambda0);
end
Any suggestion as to how to go about this problem is highly appreciated! Thank you in advance.
fzero accepts a function handle as the first input. As you currently have it, you're trying to pass a statement as the first input. This statement can't even be properly evaluated because you are trying to perform numerical operations on a function handle (more on this in a bit).
You need to instead do something like this where we create a new function handle that evaluates the original function handle and performs the other operations you need.
S1(a) = fzero(#(lambda)mean((r - b)*r1(lambda).^(1/(a - 1))),lambda0);
Further Explanation
Performing operations on a function handle is not the same as performing them on the result.
So for example, if we had a function handle:
func = #(x)2*x;
If we evaluation this, by calling it with an input value for x
func(2)
4
This works as we would expect. If now we really want the value (2*x)^2, we could try to write it the way that you wrote your statement in your question
func2 = func^2;
We will get an error!
Undefined operator '^' for input arguments of type 'function_handle'.
This does not work because MATLAB attempts to apply the ^ operation to the function handle itself and not the value of the evaluated function handle.
Instead, we would need to create a new function handle that essentially wraps the other one and performs any additional options:
func2 = #(x)func(x)^2;
func2(2)
16
Bringing it Full-Circle
So if we go back to your question, you defined your anonymous function r1 like this.
r1 = #(lambda) b + lambda*(r - b); % r and b are vectors of return data
This all looks great. You have one input argument and you reference r and b from the parent workspace.
Now when you call fzero you try to perform operations on this function handle in hopes of creating a new function handle.
mean((r - b)*r1.^(1/(a - 1)))
Like we just showed this will result in a very similar error
Undefined operator .^ for input arguments of type 'function_handle'
So we need to wrap this into a new function.
newfunc = #(lambda)mean((r - b)*r1(lambda).^(1 / (a - 1)));
Now we can safely pass this to fzero.
result = fzero(newfunc, lambda0);

What is "index should be positive integer(not complex format integer)" in MATLAB?

// I made function like this:
function y = ZL(L,f)
if isvector(f)
y=1j*2*pi.*f*L;
else
y=1j*2*pi.*f*L;
end
// and command :
L = 10,
f= -10000:100:10000,
ZL=ZL(L,f);
// then error :
index should be positive integer(not complex format integer) or boolean
-> this error is translated by me who is Korean; sorry
what's wrong with it?
First time you call ZL = ZL(L,f), you don't get any error.
If you try to call your command a second time, you'll get this error: Index exceeds matrix dimensions. It's because you affect the variable ZL, so the second time it's not the function you're calling but you"re trying to get elements from the array ZL.
So please don't use same name for a variable and an existing function, otherwise the function name is masked by the variable.

minFunc package usage

I have been using MATLAB fminunc function to solve my optimization problem. I want to try the minFunc package :
http://www.di.ens.fr/~mschmidt/Software/minFunc.html
When using fminunc, I defined a function funObj.m which gives me the objective value and the gradient at any point 'x'. It also takes in several external inputs say, {a,b,c} which are matrices. So the function prototype looks like :
function [objVal,G] = funObj(x,a,b,c)
I want to use the same setup in the minFunc package. From the examples, I figured this should work :
options.Method='lbfgs';
f = #(x)funObj(x,a,b,c);
x = minFunc(f,x_init,options);
But when I call this way, I get an error as:
Error using funObj
Too many output arguments.
What is the correct way to call minFunc for my case?
**EDIT : Alright, here is a sample function that I want to use with minFunc. Lets say I want to find the minimum of a*(b-x)^2, where a,b are scalar parameters and x being a scalar too. The MATLAB objective function will then look like :
function obj = testFunc(x,a,b)
obj = a*(b-x)^2;
The function call to minimize this using fminunc (in MATLAB ) is simply:
f = #(x)testFunc(x,a,b);
x = fminunc(f,x_init);
This gives me the minimum of x = 10. Now, How do I do the same using minFunc ?
"Note that by default minFunc assumes that the gradient is supplied, unless the 'numDiff' option is set to 1 (for forward-differencing) or 2 (for central-differencing)."
The error is because only one argument is returned by the function. You can either return the gradient as a second argument or turn on numerical differencing.
Agree with Mark. I think the simplest way to solve it is
minFunc(#testFunc, x_init, a, b, c)
In MATLAB temporary function can only have one return value. So f = #(x)testFunc(x,a,b); let your method drop gradient part every time. Because minFunc can accept extra paramters, you can pass a, b and c after x_init. I think this would work.

Passing a variable out of local functions

I'm having some trouble with local functions within my code so I've pasted a simple example below:
function [avg,testvar] = test(x) %Warning
n = length(x);
avg = mymean(x,n);
end
function [a,testvar] = mymean(v,n)
a = sum(v)/n;
testvar=123;
end
One can probably see what I'm attempting; to pass testvar out of the local functions. However Matlab returns the warning:
"The function return value 'testvar' might be unset"
with respect to the line I've commented "%Warning".
What's the best way of getting around this?
You need to specify the value of the second output of test(). Otherwise how can MATLAB know what its value is supposed to be? It doesn't know the second output of mymean() should be routed to the second output of test(). Perhaps this will solve your problem.
function [avg,testvar] = test(x) %Warning
n = length(x);
[avg, testvar] = mymean(x,n);
end
function [a,testvar] = mymean(v,n)
a = sum(v)/n;
testvar=123;
end
The variables between brackets after function are the output variables.
In your first function, you did not assign any value to testvar hence the warning. If you add testvar = 123; in the first function, the warning goes away. Or you can remove testvar from the output variables, leaving:
function avg = test(x)

Matlab: how to call a function

This is my function
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
and I called
[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1])
??? Undefined function or method 'stat' for input arguments of type 'double'.
I also tried
mean,stdev = stat([12.7 45.4 98.9 26.6 53/1])
??? Input argument "x" is undefined.
Error in ==> mean at 30
y = sum(x,dim)/size(x,dim);
Both of them are wrong and I cannot figure out why.
Could you please help me =] Thanks a lot
Your function looks fine to me, therefore I assume that your Matlab "Current Directory" is not the same directory where your function lives.
Another reason might be that the file in which this function exists does not have the same as this function. In order for Matlab to know that this function exists, it has to live in a separate file called stat.m (note how file name is the same as the function name).