dsolve using a string
이전 댓글 표시
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);
답변 (1개)
Amal Raj
2024년 2월 22일
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).
카테고리
도움말 센터 및 File Exchange에서 Numeric Solvers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!