Zeros from Trapz integration

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 Kommentare

KSSV
KSSV am 20 Apr. 2022
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?
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Torsten
Torsten am 17 Mai 2022
Bearbeitet: Torsten am 17 Mai 2022

0 Stimmen

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)

Produkte

Version

R2022a

Gefragt:

am 20 Apr. 2022

Bearbeitet:

am 17 Mai 2022

Community Treasure Hunt

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

Start Hunting!

Translated by