Intergrating using For Loop! HELP!

So, I have this assignment when I am supposed to find the distribution of certain values along a surface. The data was provided to calculate all the values needed.
this is a picture of the assignment
and this is my code..
clc;
clear;
%Reading data
fid=fopen('airfoil.dat','r');
m=fscanf(fid,'%d',1);
for i=1:m+1
x(i)=fscanf(fid,'%f',1);
y(i)=fscanf(fid,'%f',1);
cf(i)=fscanf(fid,'%f',1);
cp(i)=fscanf(fid,'%f',1);
end
%Loop for panel calculation
%lengths (ds) orintation angle (theta)
for i=1:m
ds(i)=sqrt((x(i+1)-x(i))^2+(y(i+1)-y(i))^2);
theta(i)= atan2((y(i+1)-y(i)),(x(i+1)-x(i)));
end
%CFx and CFy calculations
for i=1:m;
CFy_p=(-1)*cp(i)*cos(theta(i))*ds(i);;
CFx_p=(-1)*cp(i)*sin(theta(i))*ds(i);;
CFy_s=(-1)*cf*sin(theta(i))*ds(i);;
CFx_s=(-1)*cf*cos(theta(i))*ds(i);;
end
%CL and CD calculations
alpha=5
CL=((CFy_p+CFy_s)*cos(alpha))-((CFx_p+CFx_s)*sin(alpha));
CD_pressure=CFy_p*sin(alpha)+CFx_p*cos(alpha);
CD_friction=CFy_s*sin(alpha)+CFx_s*cos(alpha);
CD_total= CD_pressure+CD_friction;
%plots
plot(x,y); axis equal;
title('airfoil');
xlabel('x');
ylabel('y');
plot(x,cp);
set(gca,'ydir','revers');
title('cp distribution');
xlabel('x');
ylabel('cp');
plot(x,cf);
title('cf distribution');
xlabel('x');
ylabel('cf');
I know that the first part and last part of the code is correct but I'm not sure about the middle part.. Final values were given to us and when I compared it's not the same.
this should be thie final results
alpha=5 degrees
Cl=0.636
Cd=0.01011
please help!
data sheet attatched

2 commentaires

Look at this:
for i=1:m;
CFy_p=(-1)*cp(i)*cos(theta(i))*ds(i);;
CFx_p=(-1)*cp(i)*sin(theta(i))*ds(i);;
CFy_s=(-1)*cf*sin(theta(i))*ds(i);;
CFx_s=(-1)*cf*cos(theta(i))*ds(i);;
end
The variables are overwritten in each iteration. This cannot be useful for solving an integral. You need a sum like a = a + b.
TF
TF le 31 Jan 2019
Still, I don't really know how to solve this problem.. Is there a way to stop the overwrite of the iteration and still use the for loop?

Connectez-vous pour commenter.

 Réponse acceptée

Geoff Hayes
Geoff Hayes le 31 Jan 2019
Modifié(e) : Geoff Hayes le 31 Jan 2019

0 votes

TF - your alpha is in degrees but you are using the cos and sin functions whose inputs are in radians. I recommend converting alpha intio radians and trying again
alpha = 5 * pi / 180.0;

5 commentaires

TF
TF le 31 Jan 2019
That doesn't seem to be the problem!! CL and CD shoud be a single value but instead this is what I get when I run the code
p2.JPG
what is this code
for i=1:m;
CFy_p=(-1)*cp(i)*cos(theta(i))*ds(i);;
CFx_p=(-1)*cp(i)*sin(theta(i))*ds(i);;
CFy_s=(-1)*cf*sin(theta(i))*ds(i);;
CFx_s=(-1)*cf*cos(theta(i))*ds(i);;
end
What is the intent behind it? From your output, it now seems that you have taken Jan's suggestion and created arrays but aren't you supposed to integrate as per your attachment?
TF
TF le 31 Jan 2019
Modifié(e) : TF le 31 Jan 2019
The CFy_p is the force coefficient due to pressure in the y direction, CFy_s is the force coefficient due to shear in the y direction... you probably got that. These should be single values. I'm new to matlab and I thought this is the way to do it
hmmm...perhaps
CFy_p = 0;
CFx_p = 0;
CFy_s = 0;
CFx_s = 0;
for i=1:m;
CFy_p = CFy_p + cp(i)*cos(theta(i))*ds(i);
CFx_p = CFx_p + cp(i)*sin(theta(i))*ds(i);
CFy_s = CFy_s + cf(i)*sin(theta(i))*ds(i);
CFx_s = CFx_s + cf(i)*cos(theta(i))*ds(i);
end
CFy_p = -CFy_p;
CFx_p = -CFx_p;
CFy_s = -CFy_s;
Or something very similar to the above. Note how we keep a running total for each of the coefficients. Note also how we use cf(i) instead of just cf. Finally, we negate the first three (as per the attachment - the fourth remains as is).
TF
TF le 31 Jan 2019
I was just thinking of defining the variables before the loop and adding them then I saw your answer!!!
Thank you so much!!! it is working wonderfully now!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by