Using trapz doesn't work

Hey Guys,
I already read some answers about 'errors using trapz' but i didn't get what's wrong because the lengths of my vektors are similiar. All vectors have the size 1x24000 double, thus dX_dTeta is also 1x24000 double. I need to integrate the function F. Are there any mistakes in it?
Thanks!!!
Here is my code:
%% sektion
for i = 1:(ceil((T_H2O_ein-T_H2O_aus)/h))
dX_dTeta(i) = cp_H2O * Massenstromverhaeltnis(i) * ((X_sat(i) - X_vektor(i))/(h_Luft_sat(i)...
- h_Luft_vektor(i) + ((0.865^(2/3)*(((0.622+X_sat(i))/(0.622+X_vektor(i))-1)/...
(log((0.622+X_sat(i))/(0.622+X_vektor(i))))))-1)*(h_Luft_sat(i) - h_Luft_vektor(i) -...
(X_sat(i) - X_vektor(i))*(h_verdampfung + cp_Dampf*T_H2O_vektor(i))) - ...
(X_sat(i) - X_vektor(i))*cp_H2O * T_H2O_vektor(i)));
end
temp = linspace(19,43,24000);
F = dX_dTeta(i)/(X_sat(i)-X_vektor(i));
for i = 1:integrationsende
NTU = trapz(temp,F,1);
end

1 Comment

First, what does your 2nd for loop do? You dont have i anywhere in the loop.
Second - without knowing your various variable values it is hard for us to run the code and see the error you get - so can you either post the error, or include your values in the code strip you posted?

Sign in to comment.

Answers (1)

J. Alex Lee
J. Alex Lee on 31 Jan 2020

0 votes

F is of size 1x1, not 1x24000

10 Comments

Oh yeap good spot. F assignment is outside the for loop - needs to be inside :)
agree with Jakob about the 2nd loop and not being able to run the code, but I imagine what you want is
temp = linspace(19,43,24000);
F = dX_dTeta./(X_sat-X_vektor);
NTU = trapz(temp,F,1);
Thank you Jacob, thank you Alex!
You're right, the second loop is dumb - sorry for that. I think the F = ... was inside the loop before.
And I think the Alex second answer is what i wanna have. But I get the same error message, like this:
Error using trapz (line 66)
Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.
Error in NTU_ohneVariation_alphaBetaC (line 159)
NTU = trapz(temp,F,1);
Here is a photo of the equation i want to solve. To solve the integal, i already solved the upper equation using runge-kutta-4th order and saved all results in an vector - for each variable like temperature, X_sat, X and so on. My idea to solve the integral is to use trapz - or cumtrapz. This Integral at the bottom of the photo:Bildschirmfoto 2020-01-31 um 13.34.11.png
Can you try omitting the 3rd argument of trapz?
Without the 3rd argument it doesn't work as well.
What do you get when you issue
size(F)
size(temp)
1 24000 & 1 24000 ... should be fine
Is it the same if I do it manually like this?
FF = dX_dTeta(i)/(X_sat(i)-X(i));
NTU=0;
for i=1:integrationsende-1
s(i) = FF(i+1)*(T_H2O_vektor(i+1))- FF(i)*(T_H2O_vektor(i));
NTU = NTU+s(i);
end
Hmm, you might want to re-think your overall strategy. What do you mean you "solved" the upper equation? Eq. 20 and 21 look like coupled ODE...what do the double primes mean and ? Second derivatives? What are your boundary conditions? Is it possible to combine Eqs. 20, 21, and 22 into a single ODE or integral, or express Eq. 22 differently based on Eqs. 20 and 21?
But for the problem at hand of trapz not appearing to work when it seems like it should, can you provide the variables temp and F in a .mat file?
In general, trapz should be replaceable by manually doing a trapezoid rule, but your suggestion doesn't make a whole lot of sense to me, based on your original inputs being temp and FF...
Hey guys, thank you for your answers!
I re-thought it and it was actually pretty easy. I used an file from file-exchange and not it works. I don't know why it works but it does.
However, cheers for your help!
Michael

Sign in to comment.

Asked:

on 31 Jan 2020

Commented:

on 4 Feb 2020

Community Treasure Hunt

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

Start Hunting!