Taylor's series method to solve first order first degree ODE

Can someone help with how to solve first order first degree ODE using Taylor's series method? I am able to find derivatives but unable to substitute the values at given initial condition.
I have typed the code as below,
clear
clc
syms x y(x)
x0 = 0;
y0 = 1;
y1 = x^2*y-1;
y2 = diff(y1);
y3 = diff(y2);
y4 = diff(y3);
y10 = subs(y1,x,0);
y20 = subs(y2,x,0);
y30 = subs(y3,x,0);
y40 = subs(y4,x,0);
%Taylor's Method
y = y0 + (x-x0)*y10 +(((x-x0)^2)/2)*y20 + (((x-x0)^3)/6)*y30 + (((x-x0)^4)/24)*y40
And the output is as below.
substitution at x=0 is not working. Kindly help.

답변 (2개)

VBBV
VBBV 2023년 5월 27일
편집: VBBV 2023년 5월 27일
Use taylor function
clear
clc
syms x y(x)
x0 = 0;
y0 = 1;
y1 = x^2*y-1;
y2 = diff(y1);
y3 = diff(y2);
y4 = diff(y3);
y10 = subs(y1,x,0);
y20 = subs(y2,x,0);
y30 = subs(y3,x,0);
y40 = subs(y4,x,0);
%Taylor's Method
% y = y0 + (x-x0)*y10 +(((x-x0)^2)/2)*y20 + (((x-x0)^3)/6)*y30 + (((x-x0)^4)/24)*y40
% use taylor
y = y0+taylor(x^2*y-1,x,'ExpansionPoint',0)
y(x) = 

댓글 수: 4

y(0), y'(0),y''(0) and y'''(0) all have to be substituted by the program itself. Which is not happening here.
Also the coefficients are wrongly being considered.
clear
clc
syms x y(x)
x0 = 0;
y0 = 1;
y1 = x^2*y-1;
y2 = diff(y1);
y3 = diff(y2);
y4 = diff(y3);
y10 = subs(y1,x,0)
y10(x) = 
y20 = subs(y2,x,0)
y20(x) = 
0
y30 = subs(y3,x,0)
y30(x) = 
y40 = subs(y4,x,0)
y40(x) = 
%Taylor's Method
% y = y0 + (x-x0)*y10 +(((x-x0)^2)/2)*y20 + (((x-x0)^3)/6)*y30 + (((x-x0)^4)/24)*y40
% use taylor
y = y0+taylor(x^2*y-1,x,'ExpansionPoint',0)
y(x) = 
y = subs(y,x,0)
y(x) = 
0
Can you tell why coefficients are not considered correctly ?
If you consider taylor function after first term (y0), the coefficient of y'(0) will be taken as 1, which is wrong. The coefficient of y'(0) should be x.
And moreover taylor function can be used on known function in MATLAB to expand. Not to find the unknown function.
@Shrivatsa Joshi ok. agreed but why do you think subs is not working. In your program, x has been defined as symbolic variable initially and used again in the expansion equation after substituting it with 0. So, whenever variable x is called symbolic toolbox treats it as new unknown (independent) variable.
clear
clc
syms x y(x)
x0 = 0;
y0 = 1;
y1 = x^2*y-1;
y2 = diff(y1);
y3 = diff(y2);
y4 = diff(y3);
y10 = subs(y1,x,0)
y20 = subs(y2,x,0)
y30 = subs(y3,x,0)
y40 = subs(y4,x,0)
%Taylor's Method
y = y0 + (x-x0)*y10 +(((x-x0)^2)/2)*y20 + (((x-x0)^3)/6)*y30 + (((x-x0)^4)/24)*y40
% use subs after completing the equation of taylor expansion terms.
y = subs(y,x,0)

댓글을 달려면 로그인하십시오.

Oper
Oper 2025년 10월 15일

0 개 추천

Consider the initial value problem given by the ODE dy dt = y − t 2 + 1, y(0) = 0.5. a. Use the Taylor Series Method (up to the second order) to derive the Taylor series expansion for the solution of the ODE around t = 0. Calculate the first few terms of the series. b. Implement the Taylor Series Method in MATLAB to approximate the solution over the interval t = [0, 2] and plot the result. c. Use the Second and Fourth-Order Runge-Kutta method to numerically solve the same ODE over the interval t = [0, 2] and plot solutions on the same graph as the Taylor Series approx- imation. d. Compare the three numerical methods based on their accuracy and computational efficiency, and discuss scenarios where one method may be preferred over the other.

카테고리

질문:

2023년 5월 27일

답변:

2025년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by