How to do numerical integration using trapezoidal rule?

I want a graph for velocity profile. In my equation have one of the term is integro_differential. for that purpose i have to solve the integral part in my equation using newtons cotes closed integration. so, I'm using one of the rule is trapezoidal form. I want to use trapezoidal rule for integration. i.e . Here, we have to notice the 'T' is another equation output value which I already solved and i got the matrix form. i.e
%===========================================%
ymax=20; m=80; dy=ymax/m; %y=dy:dy:ymax;
xmax=1; n=20; dx=xmax/n;
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
tol=1e-2;
max_difference(1)=1; max_Iteration=1;
pr=6.2;
UOLD=zeros(m,nt);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;
while max_Iteration>tol
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); %This 'T' is a function of integral w.r.to y/ how to solve this case usign this 'T'.
dt=0.2+dt;
TOLD=T;
max_difference(j) = max(abs(T(:,j)-T(:,j-1))./max(1,abs(T(:,j-1))));
if max_difference(j)<tol
break
end
max_Iteration=max_difference;
end
end
I want to integrate this 'T' w.r.to Y
T(:,j)=TriDiag(A,B,C,D);
kindly give advice. thank you.

6 个评论

Do you know about the trapz()-function in MATLAB? This does "trapezoidal numerical integration" and might help you
@Nathan Hardenberg Yes. I tried that rule and try to include this term in my main equation. After integrate w.r.to.Y, I have to differentiate that integral term w.r.to. X.
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));
I integrated this 'I' afterthat i couldn't differentiate this integral term.
R=diff(I)/h; %dono this is the proper way for partial derivative
This R not satisfying with my equation. or If possible, we can use it in numerical differentiation method. Give me some idea to proceed this. Thnak you.
I can't really follow your steps. One reason might be that I can not execute your original code, since I do not have the TriDiag()-Function. Also your other code is not executable as well. I assumed:
syms F(y) % defined as symbolic function
%F=@(y) T
a=0; b=20; m=80; % added m=80;
h=(b-a)/m;
i=1:1:m-1;
S=F(a+i*h);
I=(h./2)*(F(a)+2*sum(S)+F(b))
I = 
R=diff(I)/h
R = 
0
Which gives a vaild result, but probably not what you want!?
I still do not know what T is! You write: "'T' is a function [...]". But in your Code it looks like T is a 2D-matrix, not a function. Maybe I'm missing something.
If possible please provide T (whatever it is)
@Nathan Hardenberg Really thanks for your response.
I will explained it clearly in the following link. please find this link and give your valuable solution. https://in.mathworks.com/matlabcentral/answers/1991053-how-to-include-partial-derivative-term-of-matrix-in-finite-difference-equation
Sorry I'll not answer the question, since I still do not understand it and your code in the new question was still not executable. Maybe try to reduce your problem. Then other users might be able to help

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

提问:

2023-6-30

评论:

2023-7-5

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by