This is my Matlab code. I am trying to simply substitute the M=[0 0 1] into n like this n1=0 n2=0 n3=1 but the answer comes without the substitution happening. What should I do to fix this?
M=zeros(1,3)
M(1,3) = 1;
syms n [1 5]
f=@(n) n1+6*n2+2*n3
a=f(M)
---------
n =
[ n1, n2, n3]
a =
n1 + 6n2 + 2n3
How can I get
a= 0+0+2
if the substitution happens

 Accepted Answer

M=zeros(1,3)
M = 1×3
0 0 0
M(1,3) = 1;
syms n [1 5]
f = matlabFunction(n1+6*n2+2*n3, 'vars', {n})
f = function_handle with value:
@(in1)in1(:,1)+in1(:,2).*6.0+in1(:,3).*2.0
a=f(M)
a = 2

2 Comments

Note: the 'vars', {n} is important there.
M=zeros(1,3)
M = 1×3
0 0 0
M(1,3) = 1;
syms n [1 5]
f = matlabFunction(n1+6*n2+2*n3)
f = function_handle with value:
@(n1,n2,n3)n1+n2.*6.0+n3.*2.0
Notice it created a function that expects three separate input variables instead of a vector of values
a=f(M)
Not enough input arguments.

Error in symengine>@(n1,n2,n3)n1+n2.*6.0+n3.*2.0
https://www.mathworks.com/matlabcentral/answers/750054-matlab-function-with-multiple-sym-variables?s_tid=srchtitle

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!