![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175329/image.jpeg)
Plotting a polynomial problem
조회 수: 2 (최근 30일)
이전 댓글 표시
Trying to plot this polynomial for different values of A but no luck so far.
M=linspace(-10,10,100);
g=10;
for A=0.1:0.1:0.9
D=-(A^2-A+1/3)*(((A-1/2)*M*g-0.25*A^2)/(2*M*((A-1/2)^2+1)+0.5*(A^2-A+1/3)))-0.5*A^2
plot(M,D)
hold all
end
legend('A=0.1','A=0.2','A=0.3','A=0.4','A=0.5','A=0.6','A=0.7','A=0.8','A=0.9')
댓글 수: 0
채택된 답변
Image Analyst
2014년 7월 31일
You forgot to use .* instead of *.
Corrected code:
workspace;
clc;
M=linspace(-10,10,100);
g=10;
lineColors = lines(9);
counter = 1;
for A=0.1:0.1:0.9
D = -(A^2-A+1/3) .* (((A-1/2)*M*g-0.25.*A^2) ./ (2*M*((A-1/2)^2+1)+0.5*(A^2-A+1/3)))-0.5*A^2
plot(M, D, 'LineWidth', 2, 'Color', lineColors(counter, :));
hold on
counter = counter + 1;
end
legend('A=0.1','A=0.2','A=0.3','A=0.4','A=0.5','A=0.6','A=0.7','A=0.8','A=0.9')
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175329/image.jpeg)
댓글 수: 0
추가 답변 (1개)
Star Strider
2014년 7월 31일
I don’t understand what ‘no luck so far’ means (I can only assume that a plot of several parallel lines of dots wasn’t your desired result), but after ‘vectorising’ your code (the line that assigns ‘D’) it produces a much different plot than the one your code originally produced:
M=linspace(-10,10,100);
g=10;
for A=0.1:0.1:0.9
D=-(A^2-A+1/3).*(((A-1/2).*M.*g-0.25.*A.^2)./(2*M.*((A-1/2).^2+1)+0.5.*(A.^2-A+1/3)))-0.5*A.^2;
plot(M,D)
hold all
end
legend('A=0.1','A=0.2','A=0.3','A=0.4','A=0.5','A=0.6','A=0.7','A=0.8','A=0.9')
‘Vectorising’ produces element-by-element operations, rather than MATLAB’s default matrix operations. It requires using the ‘dot operator’ in front of multiplication, division, and exponentiation operators: (.*), (./), (.^).
(The only exception is the transpose operator ('), where (.') produces a simple transpose rather than the default complex conjugate transpose. I mention that for completeness. It doesn’t relate to your code.)
댓글 수: 0
참고 항목
카테고리
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!