How do fix this code to give me the error?

%% dt=10
h=10;
t=[100:h:1500];
Cp=(.00000000747989.*t.^3)-(.000028903.*t.^2)+.0423484.*t+36.1064;
dH=trapz(t,Cp)
dH = 7.4939e+04
for i=1:5
h=h*10;
tt=100:h:1500;
CcPp=(.00000000747989.*tt.^3)-(.000028903.*tt.^2)+.0423484.*tt+36.1064;
aa=trapz(tt,CcPp);
dHH(i)=aa;
error(i)=abs((dH-dHH(i))/dH);
hh(i)=h;
end
Error using matlab.internal.math.getdimarg
Dimension argument must be a positive integer scalar within indexing range.

Error in trapz>getDimArg (line 90)
dim = matlab.internal.math.getdimarg(dim);

Error in trapz (line 44)
dim = min(ndims(y)+1, getDimArg(dim));
[10 dH 0;hh' dHH' error']
plot(log10(hh),log10(error),'o')
xlabel('Log_{10} (\Delta h)')
ylabel('Log_{10} \epsilon')
a=polyfit(log10(hh),log10(error),1);
f=@(x) a(1)*x+a(2);
hold on
fplot(f,[-5 -1])
hold off
a
%% dt=20
t=[100:20:1500];
Cp=(.00000000747989.*t.^3)-(.000028903.*t.^2)+.0423484.*t+36.1064;
enthalpy_change=trapz(t,Cp)
%% dt=50
t=[100:50:1500];
Cp=(.00000000747989.*t.^3)-(.000028903.*t.^2)+.0423484.*t+36.1064;
enthalpy_change=trapz(t,Cp)
%% dt=100
t=[100:100:1500];
Cp=(.00000000747989.*t.^3)-(.000028903.*t.^2)+.0423484.*t+36.1064;
enthalpy_change=trapz(t,Cp)
%% dt=200
t=[100:200:1500];
Cp=(.00000000747989.*t.^3)-(.000028903.*t.^2)+.0423484.*t+36.1064;
enthalpy_change=trapz(t,Cp)

回答(2 个)

Cp = @(T) .00000000747989.*T.^3-.000028903.*T.^2+.0423484.*T+36.1064;
H = @(T) 1/4*.00000000747989.*T.^4-1/3*.000028903.*T.^3+1/2*.0423484.*T.^2+36.1064*T;
deltaT = [10 20 50 100 200];
Tstart = 100;
Tend = 1500;
deltaH_true = H(Tend)-H(Tstart);
for i = 1:numel(deltaT)
dT = deltaT(i);
T = Tstart:dT:Tend;
deltaH(i) = trapz(T,Cp(T));
error(i) = abs((deltaH(i)-deltaH_true)/deltaH_true);
end
format long
error
error = 1×5
0.000003409811578 0.000013639246313 0.000085245289456 0.000340981157826 0.001363924631303
p = polyfit(log10(deltaT),log10(error),1)
p = 1×2
1.999999999992101 -7.467269618875773
h=h*10;
tt=100:h:1500;
CcPp=(.00000000747989.*tt.^3)-(.000028903.*tt.^2)+.0423484.*tt+36.1064;
aa=trapz(tt,CcPp);
What is happening is that your increment gets large enough that eventually tt becomes a scalar. But when you pass a scalar to the second parameter of trapz then it is interpreted as a dimension number rather than as a coordinate. (Note that trapz() of a scalar is 0)

类别

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

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by