dsolve - doesn't work inside a function.

3 views (last 30 days)
Kamil
Kamil on 13 Feb 2012
How can I solve: dx/dt = f(t,x) , x(t_0) = x_0 using dsolve? (or in any other symbolic way)
If I try typing for example: dsolve('Dx = t^2/x', 'x(0)=1') then everything is ok.
But if I try: f = @(t,x) t^2/x; dsolve('Dx = f', 'x(0)=1') - it doesn't work.
I need this to define some function[] = name(f,...) using dsolve.
Could someone help me?

Answers (2)

Walter Roberson
Walter Roberson on 13 Feb 2012
dsolve() only operates on symbolic functions or symbolic expressions, and does not operate on anonymous functions or inline functions.
The expression syntax is different for symbolic functions vs anonymous functions or inline functions, so you cannot use char() to convert the anonymous function to its MATLAB equivalent.
What you need is:
syms t x
ftx = f(t,x); %pass symbolic arguments to the anonymous function!
dfun = subs(sym('Dx = FTX'), 'FTX', ftx);
dsolve(dfun, sym('x(0)=1'))

Kamil
Kamil on 13 Feb 2012
function[ ] = fun(f)
syms t x
ftx = f(t,x); %pass symbolic arguments to the anonymous function!
dfun = subs(sym('Dx = FTX'), 'FTX', ftx);
dsolve(dfun, sym('x(t0)=x0'))
end
And I'm still getting:
> In dsolve at 92 In fun at 5
ans =
[ empty sym ]
  1 Comment
Walter Roberson
Walter Roberson on 13 Feb 2012
dfun_init = subs(sym('x(t0)=x0'), {'t0', 'x0'}, {t0, x0}));
dsovle(dfun, dfun_init);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!