Store function in an array

Hi every one, i have a problem in my MATLAB code that i want to store user defined functions in an array(6x6) and use this array for calculation such as take its determinant and solve the det = 0 equation. Example as code below
A = @(c) 3.*c +1;
B= @(c) [A A A;...
A A A;...
A A A];
C= @(c) det(B);
D= fsolve(C,1)
But it got this error:
Undefined function 'det' for input arguments of type 'function_handle'.
Error in Untitled>@(c)det(B)
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Error in Untitled (line 4)
D= fsolve(C,1)
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Can any one help me to solve this problem..thanks you so much!

3 Comments

How is the 6 by 6 matrix defined? An example would help.
Hi @madhan ravi, in the example i use (3x3) array, (6x6) is similar.
Stephen23
Stephen23 on 17 Aug 2019
Edited: Stephen23 on 17 Aug 2019
"i want to store user defined functions in an array'"
Your example shows a 3x3 matrix with identical elements, which means that its determinant is always zero.
You write "functions" (plural) but then attempt to construct a 3x3 matrix using just one function: do all matrix elements always contain exactly the same function, or can the different matrix elements contain different functions? Please show another example matrix.

Sign in to comment.

 Accepted Answer

A = @(c) 3.*c +1;
B= @(c) [A(c) A(c) A(c);...
A(c) A(c) A(c);...
A(c) A(c) A(c)];
C= @(c) det(B(c));
D= fsolve(C,1)

6 Comments

Ductho Le
Ductho Le on 18 Aug 2019
Edited: Ductho Le on 18 Aug 2019
It works @Walter Roberson, thanks you so much!
I have an additional question that how can i cite the element (1,2) in array B for caculation?
example:
fun = @(c) B{1,2}(c);
Sol = fsolve(fun,2)
i have tried this but it did not work. Do you have any idea?
You cannot extract anything from the definition of B, you can only execute B.
The exception is:
B_info = functions(B);
B_formula = B_info.function;
This would give you the character vector
'@(c)[A(c),A(c),A(c);A(c),A(c),A(c);A(c),A(c),A(c)]'
which you could then do string processing on to figure out what the text for the position of interest is. However, because of the way that anonymous functions handle previously defined variables, getting an accurate current definition of what the text means is a little messy.
If you have the Symbolic Toolbox, then you could
syms C
Bsym = B(C);
fun = matlabFunction(Bsym(1,2), 'vars', C);
Ductho Le
Ductho Le on 19 Aug 2019
Edited: Ductho Le on 19 Aug 2019
Thanks you Walter Roberson, your answers are helped me clarify many issues.
Walter Roberson
Walter Roberson on 19 Aug 2019
Edited: Walter Roberson on 19 Aug 2019
Note: the symbolic approach I showed will fail if the function contains any if statements that test the input. If the function contains any relational tests that are being used for numeric calculations, such as (x>3)*5+(x<-2)*9 then the symbolic approach will not produce an error but will create an expression that might produce results you do not want. The symbolic approach will typically produce wrong results if the function uses mod() on the input -- mod is defined a bit strangely on symbolic expressions.
i see, i think that i have to try another approach for my problem. Thanks again!

Sign in to comment.

More Answers (0)

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!