1
votes

I'm using the trapz function on two sets of data and there's something wrong.

Here is a view of the data: pressure drag coefficient and lift coefficient

So you can understand my problem; y-axis is pressure coefficient Cp, x-axis is angle (49 values in total, 0:7.5:360), data with the green square matrix is the data obtained from the experiment, the red curve is basically Cpsin(angle) while the pink one is Cpcos(angle)

Using trapz on the red one works perfectly, it gives 7.5, and if I reverse the input arguments for trapz I get the negative of this number! The problem is with the pink graph, using trapz gives a huge number (it's wrong, I shouldn't be getting this) and when switching the input arguments I get another huge number, not the negative of the first number, that is weird, I don't know what is wrong, so I used quad (needs a function) to test the results of trapz

for i = 1:48
    y = @(x) (Cp(i+1) - Cp(i)) / ( theta_rad(i+1) - theta_rad(i) ) * x .* sin(x);
    clll(i) = -0.5* quad(y,theta_rad(i), theta_rad(i+1));

end
clll = sum(clll);

for j = 1:48
    f = @(t) (Cp(j+1) - Cp(j)) / ( theta_rad(j+1) - theta_rad(j) ) * t .* cos(t);
    cdddp(i) = 0.5* quad(f,theta_rad(j), theta_rad(j+1));

end

cdddp = sum(cdddp);

For the first loop (quad of the red curve) I got a very close answer, the error was probably due to the linear interpolation I used between points and for the second loop I got a sensible answer, a very small number which is in the range of the answer I'm looking for. I also tried trapezoidal rule in Excel and I got this huge number again, so it's something I'm doing wrong.


Edit:

I just found a mistake, I was using the trapezoidal using angles in degrees rather than radians! Now I'm getting much smaller numbers but I think there's another mistake because the integral using quad gives a more sensible answer.

Here's the code I'm using for trapz

Cdp = 0.5*trapz( theta*pi/180, Cp.*cosd(theta) ); %pressure drag coefficient

Cl = -0.5*trapz(theta*pi/180, Cp.*sind(theta) ); %lift coefficient
1

1 Answers

0
votes

I got it working now, after using radians in the trapz function I got the correct answer. The interpolation I was trying to do is nonsense! I managed to create a fit using Curve Fitting Tool, it's very efficient

This

Cdp = 0.5*trapz( theta*pi/180, Cp.*cosd(theta) ); %pressure drag coefficient

can be achieved using the tool by:

fit1 = createFit( theta*pi/180, Cp.*cosd(theta) ); 
##createFit is generated by MatLab and I only need parts of it, all the plots commands were removed.
Cdp = 0.5*integrate(fit1,0,2*pi) ##"integrate" < this is why the tool is great, No need to export the coefficients to the workspace, create a function handle then use quad