How to include partial derivative term of matrix in finite difference equation?

2 views (last 30 days)
I'm solving finite difference equation by tridiagonal system along time loop. I want to include integrodifferential term in my equation. For that purpose, I solved integration in a numerical way using trapezoidal rule and I got output values like matrix form. Now I want to solve the partial derivative of matrix(integral output) w.r.to x and include it in my equation. To solve partial derivative, I'm using GRADIENT function. but it makes an error: "Unable to perform assignment because the left and right sides have a different number of elements".
This is my code:
ymax=20; m=80; dy=ymax/m; %y=dy:dy:ymax; %'i'th row
xmax=1; n=20; dx=xmax/n; %'j'th column
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
phi=45; gr=10^6;
UWALL=zeros(m,nt); UOLD=zeros(m,nt); UNEW=0;
VOLD=zeros(m,nt); TNEW=0; TOLD=TNEW*ones(m,nt); TWALL=ones(1,length(t));
A=zeros([1,m]);
B=A;
C=A;
D=A;
T=TOLD; U=UOLD;
for j=1:nt
for i=1:m
if j>i
C(i)=dt*VOLD(i,j)/4*dy-dt/(2*dy^2);
elseif i>j
A(i)=-dt*VOLD(i,j)/4*dy-dt/(2*dy^2);
elseif i==j
B(i)=1+dt*UOLD(i,j)/2*dx+dt/dy^2;
end
end
end
for j=2:nt
for i=2:m-1
if i==2
D(i)=-(dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1)/2*dx))+(dt*(TWALL(j)-2*TOLD(i,j)+TOLD(i-1,j)/(2*dy^2)))-(dt*VOLD(i,j)*(TOLD(i-1,j)-TWALL(j)+TOLD(i,j)/4*dy))-((-dt)*(VOLD(i,j)/4*dy))-(dt*(TWALL(j)/2*dy^2));
elseif i==m-1
D(i)=-(dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1)/2*dx))+(dt*(TOLD(i+1,j)-2*TOLD(i,j)+TNEW/(2*dy^2)))-(dt*VOLD(i,j)*(TNEW-TOLD(i+1,j)+TOLD(i,j)/4*dy))-(dt*(VOLD(i,j)/4*dy))-(dt*(TNEW/2*dy^2));
else
D(i)=-(dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1)/2*dx))+(dt*(TOLD(i+1,j)-2*TOLD(i,j)+TOLD(i-1,j)/2*dy^2))-(dt*VOLD(i,j)*(TOLD(i-1,j)-TOLD(i+1,j)+TOLD(i,j)/4*dy));
end
end
T(:,j)=TriDiag(A,B,C,D); %call tridiagonal function
dt=0.2+dt;
TOLD=T;
end
%============CALCULATE INTEGRAL==========================%
F=@(y) T;
a=0; b=20;
h=(b-a)/m;
i=1:1:m-1;
S=F(a+i*h);
I=(h./2)*(F(a)+2*sum(S)+F(b));
%================CALCULATE PARTIALDERIVATIVE=============%
%[IX,IY] = gradient(I); %how to solve this derivative
for j=1:nt
for i=1:m
if j>i
C(i)=dt*VOLD(i,j)/4*dy-dt/(2*dy^2);
elseif i>j
A(i)=-dt*VOLD(i,j)/4*dy-dt/(2*dy^2);
elseif i==j
B(i)=1+dt*UOLD(i,j)/2*dx+dt/dy^2;
end
end
end
for j=2:nt
for i=2:m-1
if i==2 %here.......................................................................................want to include that derivative here.
D(i)=-(dt*UOLD(i,j)*(-U(i,j-1)+UOLD(i,j)-UOLD(i,j-1)/2*dx))+(dt*(UWALL(j)-2*UOLD(i,j)+UOLD(i-1,j)/2*dy^2))+(dt*(gr^(-1/4)*cos(phi)*[IX,IY]+sin(phi)*(1/2)*(TWALL(j)+TOLD(i,j))))-(dt*VOLD(i,j)*(UOLD(i-1,j)-UWALL(j)+UOLD(i,j)/4*dy))-((-dt)*(VOLD(i,j)/4*dy))-(dt*(UWALL(j)/2*dy^2));
elseif i==m-1
D(i)=-(dt*UOLD(i,j)*(-U(i,j-1)+UOLD(i,j)-UOLD(i,j-1)/2*dx))+(dt*(UOLD(i+1,j)-2*UOLD(i,j)+UNEW/2*dy^2))+(dt*gr^(-1/4)*cos(phi)*[IX,IY]+sin(phi)*(1/2)*(TNEW+TOLD(i,j)))-(dt*VOLD(i,j)*(UNEW-UOLD(i+1,j)+UOLD(i,j)/4*dy))-(dt*(VOLD(i,j)/4*dy))-(dt*(UNEW/2*dy^2));
else
D(i)=-(dt*UOLD(i,j)*(-U(i,j-1)+UOLD(i,j)-UOLD(i,j-1)/2*dx))+(dt*(UOLD(i+1,j)-2*UOLD(i,j)+UOLD(i-1,j)/2*dy^2))-(dt*VOLD(i,j)*(UOLD(i-1,j)-UOLD(i+1,j)+UOLD(i,j)/4*dy))+(dt*(gr^(-1/4)*cos(phi)*[IX,IY]+sin(phi)*(1/2)*(T(i,j)+TOLD(i,j))));
end
end
U(:,j)=TriDiag(A,B,C,D);
dt=0.2+dt;
UOLD=U;
end
%tridiagonal function
function x = TriDiag(e,f,g,r)
%input
% e = subdiagonal vector
% f = diagonal vector
% g = superdiagonal vector
% r = right hand side vector
% output:
% x = solution vector
n=length(f);
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
x(n) = r(n)/f(n);
for k = n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end
for i = 1:length(x)
fprintf('\n%d = %f\n', i, x(i));
end
Please give some suggestion for solving derivative of matrix and how to include it in my equation?. Thank you for advance.
  10 Comments
Torsten
Torsten on 6 Jul 2023
Edited: Torsten on 6 Jul 2023
It's not clear from your notation, but my guess is that for each value of j, you have to evaluate
integral_{y(j)}^{Inf} T(x(i),y) dy
which can be approximated as
I(i,j) = trapz(y(j:end),T(i,j:end))
For j fixed, this gives you an x-profile I(1,j), I(2,j),...,I(end,j).
Now apply gradient on this vector:
gradient(I(:,j))/gradient(x(:))
Maybe one advice (although I know you most probably will not follow it): First make the pure flow equations work, then you can try to include extra terms from the energy equation.
Nathi
Nathi on 7 Jul 2023
Yes! I made it. This is the baseform only. For the system, I need to solve that integral term in momentum equation. That's why I try to solve this case.

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations 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!