function SystTaylorSolve
syms t h
Q=input('Do you want to enter a new problem y/n: ','s');
if Q=='y'||Q=='Y'
k=input('No. of Equations k=');
for i=1:k
u(i)=sym(strcat('u',num2str(i)),'real');
end
for z=1:k
up(z,1)=input( strcat('ODE',num2str(z),'u',num2str(z),'prime(t)=') );
end
for e=1:k
uN(e,1)=input(strcat('initial condition u',num2str(e),'(a)= '));
end
for i=1:k
uExact(i,1)=input(strcat('exact solution of ODE u',num2str(i),'(t)='));
end
a=input('start of interval a= ');
b=input('end of interval b= ');
save data_mat up a b u k uN uExact
else
load data_mat up a b u k uN uExact
end
n=input('Taylor order n=');
hN=input('step size h =');
for i=1:k
T(i)=up(i,1);
end
fac=1;
for i=2:n
fac=fac*i;
for j=1:k
pt(j)=diff(up(j,i-1),t);
up(j,i)=pt(j);
for p=1:k
pu(j,p)=diff(up(j,i-1),u(p));
up(j,i)= up(j,i)+pu(j,p)*up(p,1);
end
T(j)=T(j)+h^(i-1)/fac*simplify(up(j,i));
end
end
D=cell(1,k);
for i=1:k
D{i} = char(T(i));
for j=k:-1:1
D{i} =strrep(D{i},strcat('u',num2str(j)),strcat('v(',num2str(j),')'));
end
end
N=(b-a)/hN;
tN(1)=a;
w(1:k,1)=uN(1:k,1);
h=hN;
t=tN(1);
for i=1:N;
v(1:k)=w(1:k,i);
for s=1:k
w(s,i+1)=w(s,i)+h*eval(D{s});
end
tN(i+1)=tN(i)+h;
t=tN(i+1);
for s=1:k
uN(s,i+1)=eval(uExact(s));
error(s,i+1)=abs(uN(s,i+1)-w(s,i+1));
end
end
out=[tN]';
for i=1:k
out=[out uN(i,:)' w(i,:)' error(i,:)'];
end
format long e
single(out)
plot(tN,uN(1,:),'ko-',tN,w(1,:),'k+-',tN,uN(2,:),'ks-',tN,w(2,:),'kx-')
legend('u1','w1','u2','w2')
6 Comments
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719384
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719384
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719387
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719387
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719624
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719624
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719626
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_719626
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_720796
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_720796
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_720797
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/469364-can-someone-please-help-me-to-find-out-mistake#comment_720797
Sign in to comment.