dsolve using a string
Mostrar comentarios más antiguos
Team,
I am trying to figure out why the code below won't work. This is taken out of a larger program where it asks at the beginning using input "What equation would you like solved?" That is why dfeq = '2*x*y' at this particular point.
>> initcondx=1
initcondx =
1
>> initcondy=1
initcondy =
1
>> dfeq='2*x*y'
dfeq =
'2*x*y'
>> syms y x y(x)
>> ode=diff(y,x)==str2sym(dfeq)
ode(x) =
diff(y(x), x) == 2*x*y
>> cond=y(initcondx)==initcondy
cond =
y(1) == 1
>> dsolve(ode,cond)
Error using mupadengine/feval (line 187)
Expecting an ODE in the specified variable.
Error in dsolve>mupadDsolve (line 340)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 194)
sol = mupadDsolve(args, options);
Respuestas (1)
Amal Raj
el 22 de Feb. de 2024
When using str2sym to convert a string to a symbolic expression, it treats the variables as independent symbols and does not associate them with any specific function. That's why when you use str2sym(dfeq) in the diff function, it does not include the (x) notation for the function y(x).
To keep the y(x) notation, you can explicitly define y as a function of x using the symfun function. Here's an example:
dfeq = '2*x*y';
syms y(x) x; % Define symbolic variables
y = symfun(y(x), x); % Define y as a function of x
ode = diff(y, x) == str2sym(dfeq); % Define the ODE
Now, when you use diff(y, x), it will include the (x) notation for the function y(x).
Categorías
Más información sobre Numeric Solvers en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!