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!