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

4 views (last 30 days)
Mehul kumar
Mehul kumar on 22 Nov 2021
Answered: Navya Singam on 2 Dec 2021
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);

Answers (1)

Navya Singam
Navya Singam on 2 Dec 2021
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.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by