Why does this code give a blank plot?

조회 수: 2 (최근 30일)
Keerthana R
Keerthana R 2020년 4월 26일
댓글: Keerthana R 2020년 4월 26일
code:
for a=1:2:4
x=linspace(-5,5,300);
if (x>=0)
fx3=(x/a^2).*exp((-1/2)*(x/a).^2);
else
fx3=0;
end
figure(6);
plot(x,fx3);
hold on
end

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 4월 26일
Hi Keerthana,
It is because the if condition placed is wrong, and always fx3 is getting 0, therby making only a single point.
Updated the code to match what you are trying below:
for a=1:2:4
x=linspace(-5,5,300);
fx3 = zeros(1,length(x)); % Intialize the value to zeros
xPositive = x(x>=0); % Find the values of x which are greater than or equal to 0
fx3(x>=0)=(xPositive/a^2).*exp((-1/2)*(xPositive/a).^2); % Update the value of fx3, when x >= 0
figure(6);
plot(x,fx3);
hold on
end
hold off
Hope this helps.
Regards,
Sriram

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by