Cannot understand the error in my code to solve differential equation. Please someone tell me the corrections to be made.
1 次查看(过去 30 天)
显示 更早的评论
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'.
Error in sym>tomupad (line 1214)
S = convertChar(x);
Error in sym (line 211)
S.s = tomupad(x);
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);
0 个评论
回答(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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!