how to use syms variables in function?

조회 수: 3 (최근 30일)
ming Lu
ming Lu 2019년 6월 7일
답변: Star Strider 2019년 6월 7일
i wrote a function for matrix:
%=============================
function [A] = Test(a,b)
A(1,1)=a+1;
A(1,2)=b;
end
%=======================
the function is nothing special. when i input in Command Window area of Matlab:
>> syms a b
>> Test(0,b)
but Matlab shows:
The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array. Error in Test (line 3) A(1,2)=b;
Matlab tell me there is a mistake(my Expect to return: A=[1,b])
how to fix this problem,thank you very much!

채택된 답변

Star Strider
Star Strider 2019년 6월 7일
In order to do that, you will have to convert ‘A’ to a symbolic object as well:
function [A] = Test(a,b)
A = sym('A',[1 2])
A(1,1)=a+1;
A(1,2)=b;
end
then:
Q = Test(0,b)
Q =
[ 1, b]

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by