필터 지우기
필터 지우기

Plotting problem

조회 수: 1 (최근 30일)
J
J 2012년 2월 22일
편집: sony 2013년 10월 20일
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%

채택된 답변

Matt Tearle
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.
  댓글 수: 1
J
J 2012년 2월 22일
Great, thank you very much! I'm new to Matlab so this style was really helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

J
J 2012년 2월 22일
Ok, I've tried to edit the code and get rid of loops.
rs = 0.05;
Cl = 2*pi;
pa = [10*(pi/180) 5*(pi/180) -0.1*(pi/180) -2*(pi/180) -5*(pi/180)];
lmda=1:0.1:20;
for v = 1:5
for i=1:length(lmda)
a(i) = (((rs*lmda(i)*(Cl))/16)+0.5)-sqrt((((rs*lmda(i)*(Cl))/16)+0.5).^2)-((rs*lmda(i)*(Cl).*(1-lmda(i).*pa(v)))/8);
Ct(i) = ((rs*lmda(i).^2*Cl)/2).*(((1-a(i))./1-pa(v)));
end
Z = imag(Ct);
for d = 1:1:201
if Z(v,d) ~= 0
Ct(v,d) = NaN;
end
end
end
plot (lmda,Ct)
legend('10','5','-0.1','-2','-5')
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}')

J
J 2012년 2월 22일
I changed it again, thanks so much for your help!
clear all
clc
rs=0.05;
Cl=2*pi;
pa=[(pi/18) (pi/36) -(pi/1800) -(pi/90) -(pi/36)];
for v=1:5
lmda=0:0.1:20;
Ct(v,:) = ((rs*lmda.^2*Cl)/2).*(((1-((rs*lmda*Cl/16+1/2)-...
((rs*lmda*Cl/16+1/2).^2-(rs*lmda*Cl.*(1-lmda*pa(v)))/8).^(1/2))))./lmda-pa(v));
Z= imag(Ct);
for d = 1:1:201
if Z(v,d) ~= 0
Ct(v,d) = NaN;
end
end
end
figure,plot(lmda,Ct)
grid on
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}');
  댓글 수: 3
J
J 2012년 2월 24일
<3 haha teach me more! Why couldn't you be my lecturer!
Matt Tearle
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by