Plotting problem
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi. I've written this code to plot the graph it produces. However, the top three lines have complex parts to them. How can I plot only the real parts? Code follows:
%Matlab code for "MATLAB
%Question 1
%
%13/02/12
function [lmda, Ct] = problem
%QUESTION 1%
rs=0.05; %rotor solidity
Cl=2*pi; %specific lift coefficient
pa1=0.1745329250; %pitch angle 1
pa2=0.0872664625; %pitch angle 2
pa3=-0.0017453292; %pitch angle 3
pa4=-0.0349065850; %pitch angle 4
pa5=-0.0872664625; %pitch angle 5
%--------------------%
i=1;
for lmda=0:0.1:20;
a2(i)=(((((rs*lmda*Cl)/16)+0.5)^2)-((rs*lmda*Cl*(1-(lmda*pa1)))/8));
if a2(i) < 0
break
end
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
a1(i)=(((rs*lmda*Cl)/16)+0.5);
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
at(i)=a1(i)-sqrt(a2(i));
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
Ct1(i)=((rs*(lmda^2)*Cl)/2)*(((1-at(i))/lmda)-pa1);
i=i+1;
end
%----------------------------------------------------------------%
i=1;
for lmda=0:0.1:20;
a2(i)=(((((rs*lmda*Cl)/16)+0.5)^2)-((rs*lmda*Cl*(1-(lmda*pa2)))/8));
if a2(i) < 0
break
end
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
a1(i)=(((rs*lmda*Cl)/16)+0.5);
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
at(i)=a1(i)-sqrt(a2(i));
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
Ct2(i)=((rs*(lmda^2)*Cl)/2)*(((1-at(i))/lmda)-pa2);
i=i+1;
end
%----------------------------------------------------------------%
i=1;
for lmda=0:0.1:20;
a2(i)=(((((rs*lmda*Cl)/16)+0.5)^2)-((rs*lmda*Cl*(1-(lmda*pa3)))/8));
if a2(i) < 0
break
end
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
a1(i)=(((rs*lmda*Cl)/16)+0.5);
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
at(i)=a1(i)-sqrt(a2(i));
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
Ct3(i)=((rs*(lmda^2)*Cl)/2)*(((1-at(i))/lmda)-pa3);
i=i+1;
end
%----------------------------------------------------------------%
i=1;
for lmda=0:0.1:20;
a2(i)=(((((rs*lmda*Cl)/16)+0.5)^2)-((rs*lmda*Cl*(1-(lmda*pa4)))/8));
if a2(i) < 0
break
end
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
a1(i)=(((rs*lmda*Cl)/16)+0.5);
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
at(i)=a1(i)-sqrt(a2(i));
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
Ct4(i)=((rs*(lmda^2)*Cl)/2)*(((1-at(i))/lmda)-pa4);
i=i+1;
end
%----------------------------------------------------------------%
i=1;
for lmda=0:0.1:20;
a2(i)=(((((rs*lmda*Cl)/16)+0.5)^2)-((rs*lmda*Cl*(1-(lmda*pa5)))/8));
if a2(i) < 0
break
end
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
a1(i)=(((rs*lmda*Cl)/16)+0.5);
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
at(i)=a1(i)-sqrt(a2(i));
i=i+1;
end
%--------------------%
i=1;
for lmda=(0:0.1:20);
Ct5(i)=((rs*(lmda^2)*Cl)/2)*(((1-at(i))/lmda)-pa5);
i=i+1;
end
%----------------------------------------------------------------%
lmda=(0:0.1:20);
figure,plot(lmda,real(Ct1),'k-','Linewidth',2),grid on, hold on
plot(lmda,real(Ct2),'k--','Linewidth',2)
plot(lmda,real(Ct3),'k-.','Linewidth',2)
plot(lmda,real(Ct4),'r-','Linewidth',2)
plot(lmda,real(Ct5),'r--','Linewidth',2)
axis([0 20 0 1])
xlabel('\lambda');ylabel('C_t');title('Thrust coefficient against tip speed ratio.');
legend('\theta_T=10^{o}','\theta_T=5^{o}','\theta_T=-0.1^{o}','\theta_T=-2^{o}','\theta_T=-5^{o}');
end
%QUESTION 2%
댓글 수: 0
채택된 답변
Matt Tearle
2012년 2월 22일
You are plotting only the real parts. I think the problem is coming with how you're calculating these functions. You have break statements to terminate your for loops when a2 goes negative. However, the last value of a2 (the negative value) is still there.
Furthermore, you're reusing a2, but you stop calculating after a2 goes negative. This means that the rest of a2 is still there from the previous calculation. Do you really want that?
If you zoom out, so the y-axis goes higher, you will see that the function carries on doing something else. My suspicion is that this comes from calculating at based on old a2 values.
Finally, you really don't need these loops. This code is really nasty to read and debug.
lmda=0:0.1:20;
a1=(((rs*lmda*Cl)/16)+0.5);
Nice and easy. Just remember to use .* and ./ when multiplying/dividing arrays.
추가 답변 (2개)
J
2012년 2월 22일
댓글 수: 3
Matt Tearle
2012년 2월 24일
Because MathWorks pays more :)
Maybe some of these suggestions might help you:
http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!