taylor series method expansion
Show older comments
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 Comments
PJS KUMAR
on 25 Sep 2018
function main
x = linspace(1,3,5);
y = zeros(numel(x))
deltax = x(2)-x(1);
y(1) = 0.8;
for i = 1:numel(x)-1
D = dy(x(i),y(i));
y(i+1) = y(i)+deltax*D(1)+deltax^2/2*D(2)+deltax^3/6*D(3)+deltax^4/24*D(4)+deltax^5/120*D(5);
end
plot(x,y)
end
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
Walter Roberson
on 25 Sep 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 ?
Accepted Answer
More Answers (1)
Walter Roberson
on 22 Sep 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?
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!