Zeros from Trapz integration

조회 수: 10 (최근 30일)
Brittany Burns
Brittany Burns 2022년 4월 20일
편집: Torsten 2022년 5월 17일
I'm trying to solve for Tstar via integration of Dm; however, I keep getting all zeroes. How should I be using trapz? Is it better to use another command?
clear all;clc
%Given
TL=300; %surface T [K]
alpha=4/1000^2; %Thermal diffusivity [m^2/s]
L=2; %thickness [m]
x=0:0.01:L; %[m]
xstar=x/L;
t=[0 2 20 200]*3600; %time [s]
tstar=(alpha.*t)/L^2;
%Temperatures
T0x0=340;%T0(x=0)
T0x=-182.22.*x.^3 + 582.22.*x.^2 - 445.56.*x + 340; %To(x)
T0star = (T0x - TL)./(T0x0 - TL); %T0 star
for k = 1:length(t)
for i = 1 : length(x)
lambda(i) = ((2*i - 1)*pi) / 2;
fun(i,k)=T0star(i).*cos(lambda(i).*xstar(i));
Dm(i,k)=trapz(fun(i,k),0,1);
Tstar(i,k)=Dm(i,k)*cos(lambda(i)*xstar(i))*exp(-(lambda(i))^2*tstar(k));
end
end
  댓글 수: 2
KSSV
KSSV 2022년 4월 20일
Dm(i,k)=trapz(fun(i,k),0,1);
In the above line fun(i,k) is a single, number, how you expect to get area?
John D'Errico
John D'Errico 2022년 5월 17일
It looks like you think that trapz is a tool that can do intgration over an interval. For example you call trapz with THREE arguments. For example, when I do this:
trapz(5,0,1)
ans = 0
As you should see, trapz returns zero. Why? trapz is not integrating the constant function with value 5, over the interval [0,1]. Instead, what you did just misuses trapz. Read the help for trapz. Look at the examples of use.

댓글을 달려면 로그인하십시오.

답변 (1개)

Torsten
Torsten 2022년 5월 17일
편집: Torsten 2022년 5월 17일
Maybe you mean something like
L = 2; %thickness [m]
TL = 300; %surface T [K]
T0x0 = 340;%T0(x=0)
T0x = @(x) -182.22.*x.^3 + 582.22.*x.^2 - 445.56.*x + 340;
T0star = @(x) (T0x(x) - TL)./(T0x0 - TL); %T0 star
fun = @(x,i) T0star(x).*cos((2*i-1)/2 * pi * x/L);
x = 0:0.01:L; %[m]
I = 1:numel(x);
value = arrayfun(@(i)integral(@(x)fun(x,i),0,L),I)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by