Hello, I would like to use a function f in a differential equation for example f=x but get a crazy result.
syms x y f
f=x
y=dsolve('D2x=-f','x(0)=1','Dx(0)=0')
How can I put into dsolve a function f defined outside of dsolve?

 채택된 답변

Teja Muppirala
Teja Muppirala 2012년 6월 1일

1 개 추천

One way would be to convert f to a string:
syms x y f
f=x
y=dsolve(['D2x=-' char(f)],'x(0)=1','Dx(0)=0')

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 5월 31일

0 개 추천

You could try
syms x y f
f=x
eqn1 = subs(sym('D2x=-f'), 'f', f);
y = dsolve(eqn1, 'x(0)=1','Dx(0)=0')

댓글 수: 3

bym
bym 2012년 5월 31일
this doesn't work:
??? Attempted to access e(1); index out of bounds because numel(e)=0.
Error in ==> sym.sym>char2sym at 415
sk = x(s(k):e(k));
Error in ==> sym.sym at 134
S = char2sym(x);
Error in ==> sym.horzcat at 17
args{k} = sym(args{k});
Error in ==> dsolve at 121
[sys_parsed, err] = feval(symengine,'mlconvertode',sym(['[' sys_str(1:end-1) ']']),x);
Walter Roberson
Walter Roberson 2012년 5월 31일
Ah, I guess it doesn't analyze syms for occurrences of D. Ummm, I will have to make up a variable for x to be related to:
If I read the documentation right,
syms x(t) f(t) t
f = x(t); %or maybe f = x;
D2x = diff(x,2);
Dx = diff(x);
dsolve( D2x == -f, x(0) == 1, Dx(0) == 0 )
Anyhow, the root of the problem is that, as usual, when you have a quoted string like 'D2x=-f', MuPAD does not have access to the value of any variable such as f that was declared at the MATLAB level. subs() is the mechanism to "import" MATLAB-level values to the MuPAD level. It looks like dsolve() interprets symbolic variables differently than it interprets strings so the more obvious subs() does not work. char() around the subs() might perhaps have worked.
Daniel
Daniel 2012년 6월 1일
Thanks for trying.
Teja's code works:
syms x y f
f=x
y=dsolve(['D2x=-' char(f)],'x(0)=1','Dx(0)=0')

댓글을 달려면 로그인하십시오.

카테고리

태그

Community Treasure Hunt

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

Start Hunting!

Translated by