Zeros from Trapz integration
Show older comments
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 Comments
KSSV
on 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)
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.
Answers (1)
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)
Categories
Find more on Numerical Integration and Differentiation 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!