Taylor series method for 2 coupled ODEs and I'm confused!!!
조회 수: 6 (최근 30일)
이전 댓글 표시
Greetings all,
I'm trying to solve the following initial value problem using the Taylor series method:
x1prime = sin(x1)+cos(t*x2)
x2prime = sin(t*x1)/t
with x1(-1) = 2.37 and x2(-1) = -3.48. I've included the derivates to the third order (see code below), and I'm trying to get it to run until t = 1, and where my step size h = 1.
I didn't include a print statement just yet as I am seeing if I am on the right track. All of the examples I see are for one equation ODE with initial condition, not two, so this is very new to me and I'm quite confused.
Any suggestions would be appreciated.
Code:
function taylor
h = 0.01;
x1 = 2.37;
x2 = -3.48;
t = -1;
for i=1:100
x1prime = sin(x1) + cos (t*x2);
x2prime = sin(t*x1)/t;
x1_2 = cos(x1)-t*sin(t*x2);
x1_3 = -t^2*cos(t*x2)-sin(x1);
x2_2 = cos(t*x1);
x2_3 = -t*sin(t*x1);
x1out = x1+h*(x1prime+0.5*h*(x1_2+(1/3)*h*(x1_3)));
x2out = x2+h*(x2prime+0.5*h*(x2_2+(1/3)*h*(x2_3)));
t = t+h;
end
댓글 수: 0
채택된 답변
Youssef Khmou
2013년 12월 3일
Jesse, i think you should add the index to the two solutions, try :
h = 0.01;
x1 = 2.37;
x2 = -3.48;
t = -1;
for i=1:200
x1prime = sin(x1) + cos (t*x2);
x2prime = sin(t*x1)/t;
x1_2 = cos(x1)-t*sin(t*x2);
x1_3 = -t^2*cos(t*x2)-sin(x1);
x2_2 = cos(t*x1);
x2_3 = -t*sin(t*x1);
x1out(i) = x1+h*(x1prime+0.5*h*(x1_2+(1/3)*h*(x1_3)));
x2out(i) = x2+h*(x2prime+0.5*h*(x2_2+(1/3)*h*(x2_3)));
t = t+h;
end
N=length(x1out);
time=linspace(-1,t,N);
plot(times,x1out)
figure,plot(time,x2out,'r'),
댓글 수: 3
Youssef Khmou
2013년 12월 3일
hi Jesse, yes, however we need see the history of that chain, in the first code we lost old events X(t-n....), because after each iteration t changes hence all Xs change. what do you think?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!