taylor series method expansion

7 visualizzazioni (ultimi 30 giorni)
PJS KUMAR
PJS KUMAR il 22 Set 2018
Commentato: Torsten il 27 Set 2018
I wrote the following code
function yb=taylor(f,a,b,ya,n)
syms x y;
h=(b-a)/n;
y(1)=ya;
y(2)=f;
ht=h.^(0:5)./factorial(0:5)
for i=2:3
y(i+1)=diff(y(i),x);
end
y
I got the output as follows
>> f=@(x)x^2+y^2;
>> taylor(f,0,0.8,1.5,4)
ht =
1.0000 0.2000 0.0200 0.0013 0.0001 0.0000
y =
[ 3/2, x^2 + y(x)^2, 2*x + 2*y(x)*diff(y(x), x), 2*diff(y(x), x)^2 + 2*y(x)*diff(y(x), x, x) + 2]
Suggest me how to perform the following actions
1) I want to evaluate y at different values of x
2) Multiply ht and y elements and sum
  4 Commenti
PJS KUMAR
PJS KUMAR il 25 Set 2018
Modificato: PJS KUMAR il 25 Set 2018
I wrote the following code for Taylor series expansion
function yb=taylor(f,a,b,ya,n)
syms x y;
h=(b-a)/n;
y(1)=ya;
y(2)=f;
ht=h.^(0:3)./factorial(0:3)
for i=2:3
y(i+1)=diff(y(i),x);
end
s=sum(ht.*y)
I run code and got the following output
>> syms x y(x);
>> f=@(x)x^2+y^2;
>> taylor(f,1,3,0.8,4)
s =
x/4 + (y(x)*diff(y(x), x))/4 + diff(y(x), x)^2/24 + y(x)^2/2 + (y(x)*diff(y(x), x, x))/24 + x^2/2 + 101/120
Now i want to evaluate the output 's' at different values of x and y, where x is from a to b and y(a)=0.8
Walter Roberson
Walter Roberson il 25 Set 2018
y' = x^2+y^2, for the values of x = 1 (0.5) 3 with y(1)=0.8
I am having difficulty understanding the initial conditions. Could you confirm that you are referring to
syms y(x)
>> simplify(dsolve(diff(y,x) == x^2+y^2, y(1)==0.8))
ans =
piecewise(C5 ~= 0, (x*(4*besselj(-1/4, 1/2)*besselj(-3/4, x^2/2) + 4*besselj(1/4, 1/2)*besselj(3/4, x^2/2) + 5*besselj(-3/4, 1/2)*besselj(3/4, x^2/2) - 5*besselj(3/4, 1/2)*besselj(-3/4, x^2/2)))/(4*besselj(1/4, 1/2)*besselj(-1/4, x^2/2) - 4*besselj(-1/4, 1/2)*besselj(1/4, x^2/2) + 5*besselj(-3/4, 1/2)*besselj(-1/4, x^2/2) + 5*besselj(3/4, 1/2)*besselj(1/4, x^2/2)))
and you want taylor expansion of that without having solved the differential equation ?

Accedi per commentare.

Risposta accettata

PJS KUMAR
PJS KUMAR il 25 Set 2018
function D = dy(x,y)
f = x^2+y^2;
df = 2*x+2*y*f;
d2f = 2+2*(f^2+y*df);
d3f = 2*(2*f*df+f*df+y*d2f);
d4f = 2*(2*(df^2+f*d2f)+df^2+f*d2f+f*d2f+y*d3f);
D = [f df d2f d3f d4f];
end
suggest me to change the above code as given below, so that the differentiation is calculated by using diff function, without giving the differentiation directly in the program.
function D = dy(x,y)
f = x^2+y^2;
df = diff(f);
d2f = diff(df);
---
---
  13 Commenti
Walter Roberson
Walter Roberson il 26 Set 2018
As I tried to get you to understand before:
If you are using a symbolic variable with a particular name, then you should strongly avoid using the same variable name for a different purpose, such as storing a list of particular locations to execute at, or such as storing the results of calculating the taylor series at particular locations.
Torsten
Torsten il 27 Set 2018
1. n is undefined.
2. You use i as a loop variable in both nested loops.
3. Walter's remark.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 22 Set 2018
As I already explained to you, because you have
y = [ 3/2, x^2 + y(x)^2, 2*x + 2*y(x)*diff(y(x), x), 2*diff(y(x), x)^2 + 2*y(x)*diff(y(x), x, x) + 2]
then the y on the left side refers to the same thing as the y on the right side, and so y(x) on the right side signifies array indexing. Your y vector is 5 elements long, so the only valid values of x for this would be 1, 2, 3, 4, or 5. Each y(x) would resolve to a numeric scalar value, and diff() of a numeric scalar value is empty. Any operation involving an empty array returns an empty array, so all of the entries except the first two are going to disappear, leaving you only [3/2, x^2 + y(x)^2] to work with for preliminary consistency.
Now, with that subset, can x = 1 be made self-consistent? That would require that y(1) be 3/2, which seems plausible. With the subset, can x = 2 be made self-consistent? That would require that y(2) = x^2 + y(x)^2 = 2^2 + y(2)^2 . Rewriting as z = 4 + z^2 we can see that has only complex roots 1/2-1i*sqrt(15)*(1/2), 1/2+1i*sqrt(15)*(1/2) . Is that acceptable, to force y(2) to be complex valued?

Community Treasure Hunt

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

Start Hunting!

Translated by