dsolve problem
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Teja Muppirala
2012-6-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
2012-5-31
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 个评论
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!