Plotting problem
Afficher commentaires plus anciens
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%
Réponse acceptée
Plus de réponses (2)
J
le 22 Fév 2012
J
le 22 Fév 2012
3 commentaires
Matt Tearle
le 22 Fév 2012
And that's why I love MATLAB. Actually, one more thing I love about MATLAB that would help you right now: logical indexing.
All that stuff to NaN out anything with a nonzero imaginary part? Yeah, try this: Ct(imag(Ct)~=0) = NaN; (Do it after the loop, so you have to do it only once.)
And speaking of doing stuff outside the loop, move your lmda = ... before the loop -- no need to do the same thing 5 times. And notice the orange warning on the line Ct(v,:) = ...? It won't make a whole heap of difference in this case, but get into the habit of preallocating space for your arrays, when you know how big they will be. Ct = zeros(5,length(lmda)); before the loop.
J
le 24 Fév 2012
Matt Tearle
le 24 Fév 2012
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
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!