taylor series expansion with initial condition

syms x f(x) p
f1=taylor(f(x),x,'order',3)
(D(D(f))(0)=p; D(f)(0)=1; f(0)=0;
%%% I want to put initial conditios (D(D(f))(0)=p; D(f)(0)=1; f(0)=0; in f1 to
find f1=x+p*x^2/2 in symbolic form. Guide me please

 Réponse acceptée

John D'Errico
John D'Errico le 5 Jan 2020
Modifié(e) : John D'Errico le 5 Jan 2020
Why not try it! ??? Make an effort. For example, this seems the obvious thing to try. So what does this do?
syms x f(x) p
f1=taylor(f(x),x,'order',3)
f1 =
(D(D(f))(0)*x^2)/2 + D(f)(0)*x + f(0)
subs(f1,f(0),0)
ans =
(D(D(f))(0)*x^2)/2 + D(f)(0)*x
Can you finish the next two steps on your own?

5 commentaires

MINATI
MINATI le 5 Jan 2020
But when I run
f2=subs(f1,f(0),0,D(f)(0),1,D(D(f)(0),p)
ERROR came as
()-indexing must appear last in an index expression.
what next?
John D'Errico
John D'Errico le 5 Jan 2020
Modifié(e) : John D'Errico le 5 Jan 2020
Ah, but that is good. YOU TRIED IT! It looks like the symbolic toolbox is not able to recognize the format it returned for a differential. So we need to find a way to get the tools to use those initial conditions in another way. Sometimes, the trick is to get yout hands dirty and try things. This works, but it seems a bit of a kludge:
syms x f(x) p
f1 = taylor(f(x),x,'order',3);
fp = subs(diff(f1,x),0)
fp =
D(f)(0)
fpp = subs(diff(f1,x,2),0)
fpp =
D(D(f))(0)
subs(f1,[f(0),fp,fpp],[0 1 p])
ans =
(p*x^2)/2 + x
I can certainly find a better way to solve it, of course. There always is a better way. The problem at this point seems to be to get the symbolic toolbox to recognize the form that Taylor creates for those differentials. Stupid computers. ;-) At least we can extract that differential from f1 as I did, and then subs recognizes it.
MINATI
MINATI le 5 Jan 2020
Modifié(e) : MINATI le 5 Jan 2020
Thanks for your effort
Actually I want to generalise my work of three functions (f , g, h) of taylor expansion
of order (3, 3, 2), so I am trying to change the code given below:
If you want to follow:
syms x f(x) p g(x) a q h(x) r f1 g1 h1
TE = taylor([f(x) g(x) h(x)],x,'order',3); %%%TAYLOR EXP
f0=subs(TE,x,0);fp = subs(diff(TE,x),0);,fpp= subs(diff(TE,x,2),0);
IC=[f0 fp fpp]; %%% INITIAL CONDITION
[f(1) g(1) h(1)] =subs(TE,IC,[0,0,1, 1,a,r, p,q,0])%%%%
RESULT should come as [f(1) g(1) h(1)]=[ x+p*x^2/2 a*x+q*x^2/2 1+r*x] ]
Yes, but you cannot do what you want.
syms x f(x) p g(x) a q h(x) r
f1 = taylor([f(x) g(x) h(x)],x,'order',[3 3 2]);
Error using sym/taylor (line 99)
The value of 'Order' is invalid. It must satisfy the function: isPositiveInteger.
The taylor function appears not to be vectorized in the sense that you can expand each term ot the vector of functions to a different order.
So this next is valid:
f1 = taylor([f(x) g(x) h(x)],x,'order',3);
And then you should be able to proceed further.
MINATI
MINATI le 5 Jan 2020
Now everything is OK
but can it be possible to bring
[ f(1) g(1) h(1) ] = [ x+p*x^2/2 a*x+q*x^2/2 1+r*x]
as I have to incorporate this in another code

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by