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

2 visualizaciones (últimos 30 días)
Mehul kumar el 22 de Nov. de 2021
Respondida: Navya Singam el 2 de Dic. de 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'.
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);
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Navya Singam el 2 de Dic. de 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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by