for double for loop error

3 views (last 30 days)
yogeshwari patel
yogeshwari patel on 5 Jun 2024
Edited: Torsten on 5 Jun 2024
syms x t%a g d l1 m1
%%%%%%%%%%%%%%%% Fractional Order%
alpha=2;
beta=1;
%%%%%%%%%%%% initilization of variable%%%%%%
U=zeros(1,2,'sym');
V=zeros(1,2,'sym');
A=zeros(1,2,'sym');
B=zeros(1,2,'sym');
C=zeros(1,2,'sym');
D=zeros(1,2,'sym');
E=zeros(1,2,'sym');
F=zeros(1,2,'sym');
G=zeros(1,2,'sym');
series1(x,t)=sym(zeros(1,1));
series2(x,t)=sym(zeros(1,1));
a=1;
g=0.01;
d=0.01;
l1=-(4/9)*g^4*d+(16/27)*g^6-(5/9)*g^2*d^2-(5/54)*d^3;
m1=20*g^4*d-16*g^6+5*g^2*d^2;
U(1)=a-2*g*cot(g*x);
U(2)=-2*g^2*d*csc(g*x)*csc(g*x);
V(1)=l1-(1/9)*m1*cot(g*x)*cot(g*x);
for k=1:3
A(1)=0;
B(1)=0;
C(1)=0;
D(1)=0;
E(1)=0;
F(1)=0;
G(1)=0;
for r=1:k
A(1)=A(1)+diff(U(r),x,2)*diff(U(k-r+1),x,3);
B(1)=B(1)+diff(U(r),x,1)*diff(U(k-r+1),x,4);
D(1)=D(1)+diff(U(r),x,2)*(k-r+1)*U(k-r+1);
E(1)=E(1)+diff(U(r),x,1)*(k-r+1)*diff(U(k-r+1),x,1);
F(1)=F(1)+diff(V(r),x,1)*diff(U(k-r+1),x,1);
G(1)=G(1)+V(r)*diff(U(k-r+1),x,2);
for l=1:r
C(1)=C(1)+diff(U(l),x,1)*diff(U(r-l+1),x,1)*diff(U(k-r+1),x,2);% 4th term
end
end
U(k+2)=gamma(((k-1)*alpha)+1)/gamma((alpha*(k+1-1))+1)*((1/5)*diff(U(k),x,6)+3*A(1)+3*B(1)+9*C(1)-(k)*diff(U(k+1),x,2)-3*D(1)-3*E(1)+(18/5)*diff(V(k),x,1))
V(k+1)=gamma(((k-1)*beta)+1)/gamma((beta*(k+1-1))+1)*(diff(V(k),x,3)+3*F(1)+3*G(1));
end
for k=1:2
series1(x,t)=simplify(series1(x,t)+U(k)*power(t,k-1));
series2(x,t)=simplify(series2(x,t)+V(k)*power(t,k-1));
end
series1
series2
row=0
for r=1:10:110
row=row+1;
col=0;
e=r-1
for t=1
col=col+1;
f=t+19
C1(row,col)=series1(e,f);
C2(row,col)=series2(e,f);
end
end
vpa(C1,15)
vpa(C2,15)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=10:10:100
t=20
row=0
for i=1:length(x)
row=row+1
col=col+1
for j=1:length(t)
C1(row, col) =series1(x(i), t(i))
C2(row,col) =series2(x(i),t(i))
end
end
This two for loop shows thw different value of C1 and C2 . The first for loop gives the correct value for C1 but not C2. But the second loop give both wrong
.
I want know the value of x anf t are same then why for loop show two different value
  1 Comment
Torsten
Torsten on 5 Jun 2024
Edited: Torsten on 5 Jun 2024
In the code
x=10:10:100
t=20
row=0
for i=1:length(x)
row=row+1
col=col+1
for j=1:length(t)
C1(row, col) =series1(x(i), t(i))
C2(row,col) =series2(x(i),t(i))
end
end
you never reference the loop index j in the inner loop and you use col=col+1 in the outer instead of the inner loop. Both things are obviously wrong.
And x is not the same since the first construct starts with e = 0 while the second starts with x = 10.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!