Store function in an array
12 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2019 年 8 月 17 日
編集済み: Stephen23
2019 年 8 月 17 日
"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.
採用された回答
Walter Roberson
2019 年 8 月 17 日
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 件のコメント
Walter Roberson
2019 年 8 月 19 日
編集済み: Walter Roberson
2019 年 8 月 19 日
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!