I need help plotting using this while loop

조회 수: 9 (최근 30일)
Daabir Momin
Daabir Momin 2023년 3월 27일
댓글: Daabir Momin 2023년 3월 27일
this is how my code looks like currently, I am generating the correct munbers for the fuction but it is not plotting anything. Please Help.
xx = linspace(-5,9,85);
w=0
w = 0
while w<85
w=w+1;
fxx = ((xx(w)+1)^2)./(2+5*xx(w));
end
plot(xx,fxx)

채택된 답변

VBBV
VBBV 2023년 3월 27일
xx = linspace(-5,9,85);
w=0
w = 0
while w<85
w=w+1;
fxx(w) = ((xx(w)+1)^2)./(2+5*xx(w));
end
plot(xx,fxx)
  댓글 수: 2
VBBV
VBBV 2023년 3월 27일
Use an counter to create array for the variable you want to plot. if you use
xx = linspace(-5,9,85)
xx = 1×85
-5.0000 -4.8333 -4.6667 -4.5000 -4.3333 -4.1667 -4.0000 -3.8333 -3.6667 -3.5000 -3.3333 -3.1667 -3.0000 -2.8333 -2.6667 -2.5000 -2.3333 -2.1667 -2.0000 -1.8333 -1.6667 -1.5000 -1.3333 -1.1667 -1.0000 -0.8333 -0.6667 -0.5000 -0.3333 -0.1667
w = 1; %e.g.
fxx = ((xx(w)+1)^2)./(2+5*xx(w)) % a single value
fxx = -0.6957
it will of course evaluate the equation inside the while loop but does not create an array of values but only a scalar value. Read more about plot function in This link
Daabir Momin
Daabir Momin 2023년 3월 27일
Neat! That worked, thank you for the explanation.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by