2 views (last 30 days)
Shahid Iqbal on 28 Jun 2019
Answered: David K. on 30 Jul 2019
function SystTaylorSolve
syms t h
%Entering system of IVP and saving it with different Taylor order and step size
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)=') );% up(i)is the i-th derivative of
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 =');
%Finding the derivatives of u' and constructing Taylor expansion
for i=1:k
T(i)=up(i,1);% Taylor series
end
fac=1; %factorial
for i=2:n
fac=fac*i;
for j=1:k
pt(j)=diff(up(j,i-1),t);% partial derivative of u prime(i-1)
up(j,i)=pt(j);
for p=1:k
pu(j,p)=diff(up(j,i-1),u(p));% partial derivative of
%u prime(i-1) with respect to u
up(j,i)= up(j,i)+pu(j,p)*up(p,1);% construct u prime(i)
end
T(j)=T(j)+h^(i-1)/fac*simplify(up(j,i));% construct Taylor series
end
end
D=cell(1,k);
for i=1:k
D(i) = (T(i));
for j=k:-1:1
D{i} =strrep(D{i},strcat('u',num2str(j)),strcat('v',num2str(j)));
end
end
%Taylor Method
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));
end
error=abs(uN(s,i+1)-w(s,i+1));
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')
I am trying to run this program from last one month, Error at line 52 to 55 . someone please help me to run this program , I shell be thankful.
command window input
>> SystTaylorSolve3_6
Do you want to enter a new problem y/n: y
No. of Equations k=3
ODE1u1prime(t)=u(2)-u(3)+t
ODE2u2prime(t)=3*t^2
ODE3u3prime(t)=u(2)+exp(-t)
initial condition u1(a)=1
initial condition u2(a)=1
initial condition u3(a)=-1
exact solution of ODE u1(t)=-0.05*t^5+0.25*t^4+t+2-exp(-t)
exact solution of ODE u2(t)=t^3+1
exact solution of ODE u3(t)=0.25*t^4+t-exp(-t)
start of interval a= 0
end of interval b= 1
Taylor order n=9
step size h =0.1
Error occurred
The following error occurred converting from sym to cell:
Conversion to cell from sym is not possible.
Error in SystTaylorSolve3_6 (line 52)
D(i) = (T(i));
I am trying to run this program from last one month, Error at line 52 to 55 . someone please help me to run this program , I shell be thankful.
Shahid Iqbal on 3 Jul 2019

David K. on 30 Jul 2019
Here you go. A few things needed to be changed. First the change that Torsten initially pointed out. Then we further change it to
D{i} = char(T(i));
the char is a needed conversion because strrep only works on chars and strings. Next, this cause an error saying that v2 and so on are not variables in the evaluate statement. I made the assumption that you want v1,v2,v3 to be the values in the v variable you create so I changed the strrep statement to replace u1 with v(1).
Lastly, when putting together the output the error variable was only 1x1 while it needs to be 3x11 like the other variables you are concatenating. So I put it into the loop that creates uN assuming that it should be calculated for every iteration.
And now it should run.
function SystTaylorSolve
% function test
syms t h
%Entering system of IVP and saving it with different Taylor order and step size
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)=') );% up(i)is the i-th derivative of
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 =');
%Finding the derivatives of u' and constructing Taylor expansion
for i=1:k
T(i)=up(i,1);% Taylor series
end
fac=1; %factorial
for i=2:n
fac=fac*i;
for j=1:k
pt(j)=diff(up(j,i-1),t);% partial derivative of u prime(i-1)
up(j,i)=pt(j);
for p=1:k
pu(j,p)=diff(up(j,i-1),u(p));% partial derivative of
%u prime(i-1) with respect to u
up(j,i)= up(j,i)+pu(j,p)*up(p,1);% construct u prime(i)
end
T(j)=T(j)+h^(i-1)/fac*simplify(up(j,i));% construct Taylor series
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
%Taylor Method
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')

### Categories

Find more on Calculus 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!