How to solve the error undefined the function or variables in matlab - matlab

This is the code I write in Matlab. As you can see, I have defined the symbolic variable tao. However, it still shows that undefined variable or functions. I didn't know where is wrong in the code.
The function is like this:
Ch = 0.8; H=0.6088;
syms t u s k tao
fsb= limit( symsum( (int( Ch*s^(1/2)*int((u-s)^(H-3/2)*u^(H-1/2),u,floor(t/tao)*tao,floor((t+tao)/tao)*tao),s,(k-1)*tao,k*tao ))^2,k,1,floor(t/tao)),tao,0) + ...
+limit( (int( Ch*s^(1/2)*int((u-s)^(H-3/2)*u^(H-1/2),u,s,floor((t+tao)/tao)*tao ),s,floor(t/tao)*tao,floor((t+tao)/tao)*tao) )^2,tao , 0);
fsb = matlabFunction(fsb);
sss=fsb(1);
I have tried change the path into the Matlab installed path, but it didn't work.
The fsb is a function defined by my self. It's a complicated function including integral and limit.
This is the information about the error:
undefined functions or variables 'tao'
symengine>#(t)limit(integral(#(s)sqrt(s).*integral(#(u)u.^(6.8e1./6.25e2).*1.0./(-s+u).^(5.57e2./6.25e2),s,tao.*floor((t+tao)./tao)).*(4.0./5.0),tao.*floor(t./tao),tao.*floor((t+tao)./tao)).^2,tao==0.0)+limit(symsum(integral(#(s)sqrt(s).*integral(#(u)u.^(6.8e1./6.25e2).*1.0./(-s+u).^(5.57e2./6.25e2),tao.*floor(t./tao),tao.*floor((t+tao)./tao)).*(4.0./5.0),tao.*(k-1.0),k.*tao).^2,k,1.0,floor(t./tao)),tao==0.0,Real)

Related

Not enough input arguments when calling GA toolbox

I called Matlab's genetic algorithm solver, ga, in the following way:
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
where theta is a 26-by-1 column vector that needs to be optimized.
So in the main function, it goes like this:
clc
clear
global var1 var2...
load ('abcd.mat')
theta0=[1 2 3....];
LB=[26-by-1 row vector];
UB=[26-by-1 row vector];
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
The fitness function, smmobj, is defined as:
function [obj]=smmobj(theta)
global var1 var2...
But when I run it, it always says:
Error using smmobj (line 4)
Not enough input arguments.
Error in SMMga (line 32)
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
But I run the fitness function by itself, it works.
[theta,fval,exitflag] = ga(smmobj,26,[],[],[],[],LB,UB,[],[]);
will throw that error because you are essentially calling the function smmobj (with no parameters) instead of passing your ga function a function handle to your objective function. You do this using the # symbol so
[theta,fval,exitflag] = ga(#smmobj,26,[],[],[],[],LB,UB,[],[]);
Incidentally, I would recommend that you do not use global in order to pass the extra parameters of var1 and var2 to your objective function but rather use anonymous functions:
[theta,fval,exitflag] = ga(#(x)(smmobj(x,var1,var2)),26,[],[],[],[],LB,UB,[],[]);
and then change smmobj's definition to
function [obj]=smmobj(theta,var1,var2)

How do I retrieve the names of functions used in an .m file in MATLAB?

Say I have the following MATLAB function do_this.m
function result = do_this(input1, input2)
% input1 and input2 must have the same size.
A = input1 + diag(input2);
result = rand(size(input1)) + A;
end
I would like to know how to return an array of all the functions used in a given .m file. For example,
>> func_names = get_func_names('do_this.m')
So that func_names is ['diag', 'rand', 'size']
You can get this sort of information from depfun().
% See depfun helpfile
[TRACE_LIST, BUILTINS] = depfun('do_this.m');
BUILTINS is a cell array of all built-in functions needed by your function, and returns something like
BUILTINS =
'colon'
'datenummx'
'diag'
'evalin'
'horzcat'
'loadobj'
'numel'
'plus'
'rand'
'saveobj'
'size'
'subsindex'
'subsref'
'vertcat'
Note that this also returns functions used by diag(), rand(), etc.
In R2014a (and up?), MATLAB warns Warning: DEPFUN will be removed in a future release. Use matlab.codetools.requiredFilesAndProducts instead. However, the suggested function does not return built-in functions, only user-defined ones.

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.

Anonymous function inside a script error

I created an anonymous function inside a script and I can't get MATLAB to run the fminsearch? Here's an what I have so far:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(#q,x);
The variables D and L are defined, but MATLAB gives me the following error:
Error: File: Testing.m Line: 51 Column: 17
"q" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
q is not mentioned before this command. What am I doing wrong?
Another thing that could solve my problem is to get my script to write a function file, but how to do that?
Remove the second #:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(q,x);
q is a function handle. fminsearch expects a function handle. You can create a function handle out of a function using an # (e.g. #min), but you don't need to do that here.
You can also write the anonymous function inline with the search command:
V=x(1);
f=x(2);
fminsearch(#(x) (pi.*D.*L)./(1000.*V.*f),x);
UPDATE (credits to #wakjah)
For your code to do anything sensible, you should use the argument x of the anonymous function:
x0 = [initialV, initialF];
fminsearch(#(x) (pi.*D.*L)./(1000.*x(1).*x(2)), x0);
#function creates a function handle for an existing function.
q = #(x) whatever... creates a function handle called q.
But, you can't create a function handle for a function handle, only for a function.
See this:
>> fones = #ones
fones =
#ones
>> ffones = #fones
Error: "fones" was previously used as a
variable,
conflicting with its use here as the name
of a function or command.
See MATLAB Programming, "How MATLAB
Recognizes Function Calls That Use
Command Syntax" for details.
In Matlab, a function handle is a kind of a pointer to a function and is distinct from a function (unlike in some other languages where the a function identifier can be passed and stored as any other variable).
It's important to note that calling a function and a function handle results in the same behaviour. Except for the case where the identifier is used without any parentheses following it:
>> ones
ans =
1
>> fones
fones =
#ones
>> ones(2)
ans =
1 1
1 1
>> fones(2)
ans =
1 1
1 1

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).