function handle how to apply to dsolve???

syms t y
yp=@(t,y) y-t.^2+1;
exact=dsolve( 'Dy=yp(t,y)', 'y(0)=0.5');
abso=abs(diff(exact,2));
Explicit solution could not be found.
> In dsolve (line 201)
how to change code????

 Accepted Answer

syms y(t)
yp = y-t^2+1;
eqn = diff(y) == yp;
ic = y(0) == 1/2;
exact = dsolve([eqn, ic]);
abso = abs(diff(exact,2));
You are causing problems for yourself here and in the previous question by using y as both a function and a variable.

7 Comments

I want to remain function handle.. In your code there is no function handle, isnt it??
syms y t
yp = @(t,y) y-t.^2+1;
yt = sym('y(t)')
exact = dsolve( [diff(yt,t) == subs(yp(t,y), y, yt), 'y(0)=0.5'] );
abso = abs(diff(exact,2));
You are causing problems for yourself here and in the previous question by using y as both a function and a variable.
umm... this code has an error in 2017a. Support of character vectors that are not valid variable names or define a number will be removed in a future release. To create symbolic expressions, first create symbolic variables and then use operations on them.
That is not an error, that is a warning. Other than disabling the warning, there is no solution as long as you insist on using y as both a function and a variable name.
Seong Ik Kim
Seong Ik Kim on 23 Nov 2017
Edited: Seong Ik Kim on 23 Nov 2017
aha.. Thank you! and then, I have a quenstion. In symbolic , function handle is not recommend than symfun??
You have to invoke the function handle on appropriate symbolic variables in order to use it in dsolve() .
Your problem was a conflict between using y as a variable and y as a function.
You had
syms t y
which makes y a variable rather than a function.
You have
yp=@(t,y) y-t.^2+1;
when called with yp(t, y) this gives a result back in terms of the variable y. But in dsolve, you need the function y(t)
Ummm... I must be tired...
syms y(t)
yp = @(t,y) y-t.^2+1;
exact = dsolve([diff(y) == yp(t,y), y(0)==0.5]);
abso = abs(diff(exact,2));
Sorry, and Thank you. I understand my problems and your teaching. Thank you so much!!

Sign in to comment.

More Answers (1)

Torsten
Torsten on 23 Nov 2017
Use function handles if you want to integrate numerically, use symbolic expressions if you want to integrate symbolically.
Best wishes
Torsten.

Community Treasure Hunt

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

Start Hunting!