# Cannot understand the error in my code to solve differential equation. Please someone tell me the corrections to be made.

조회 수: 5 (최근 30일)
Mehul kumar 2021년 11월 22일
답변: Navya Singam 2021년 12월 2일
clf
syms t m
ycf='exp(m*t)';clf
syms t m
ycf='exp(m*t)';
y1=diff(ycf,t);
y2=diff(y1,t);
y3=simplify(subs('D2y+y=0',{'y','Dy','D2y'},{ycf,y1,y2}));
ycf=solve(y3,'m');
y4='(A*cos(2*t))';
y5=diff(y4,t);
y6=diff(y5,t);
y7=simplify(subs('D2y+y=cos(2*t)',{'y','Dy','D2y'},{y4,y5,y6}));
ypi=solve('y7','A');
ERROR-
Error using sym>convertChar (line 1448)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate
character vectors and strings representing symbolic expressions, use 'str2sym'.
S = convertChar(x);
Error in sym (line 211)
Error in sym/privResolveArgs (line 966)
argout{k} = sym(arg);
Error in sym/diff (line 21)
args = privResolveArgs(S,varargin{:});
Error in qwedfgh (line 4)
y1=diff(ycf,t);

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Navya Singam 2021년 12월 2일
Hi Mehul,
The mentioned error in the code is because of the following lines
ycf = 'exp(m*t)'
y1 = diff(ycf,t);
ycf is of the type 'char', but "diff" expects the first argument to be of type symbolic expression or symbolic function. To convert the char into sym, "str2sym" function can be used.
ycf = str2sym('exp(m*t)')
y1 = diff(ycf,t)
Alternatively, you may also define the variable ycf as
ycf = exp(m*t)
since, exp(m*t) is a symbolic expression.
Refer to the str2sym and diff documentation to know more about the input arguments to the "diff" function.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by