Matlab plots only one point

조회 수: 24 (최근 30일)
positron96
positron96 2017년 3월 30일
댓글: Guillaume 2017년 3월 30일
I want to plot the function Y = X^m/(5^m + X^m) on a Y vs. X graph for different values of m, and I used the code below but MATLAB plots only one point. Can someone help me?
for m = 1:10
X = linspace(1, 100, 1001);
Y = (X.^m) / (5^m + X.^m);
plot(X, Y);
end
Thanks!

채택된 답변

Guillaume
Guillaume 2017년 3월 30일
편집: Guillaume 2017년 3월 30일
You're performing matrix division instead of elementwise division. Use ./ instead of / in your Y calculation.
Once that's fixed, you'll get one plot and only one, because by default plot erases previous plots. Use hold on to prevent that.
Also note that since X doesn't change with m, it'll be faster to only calculate it once, outside the loop. So:
figure;
hold on;
X = linspace(1, 100, 1001);
for m = 1:10
Y = (X.^m) ./ (5^m + X.^m);
plot(X, Y);
end
edit: also note that you can avoid the loop entirely (and the need for hold on):
figure;
X = linspace(1, 100, 1001);
m = (1:10)'; %as a vector in a different direction than X
Y = (X.^m) ./ (5.^m + X.^m); %requires R2016b or later
plot(X, Y);
  댓글 수: 3
positron96
positron96 2017년 3월 30일
Can you tell me what the apostrophe is for in m = (1:10)' ? Thanks in advance!
Guillaume
Guillaume 2017년 3월 30일
As the comment says, it's to ensure that m is in a different direction than X. It makes m as a column vector, whereas X is a row vector.
That's what makes the next line work.

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

추가 답변 (1개)

KSSV
KSSV 2017년 3월 30일
X = linspace(1, 100, 1001) ;
figure
hold on
for m = 1:10
Y = (X.^m). / (5^m + X.^m);
plot(X, Y);
end

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by