필터 지우기
필터 지우기

Going up from negative in for loop counter

조회 수: 1 (최근 30일)
Crista
Crista 2013년 9월 11일
I'm trying to make this code
for x=-10:10;
y=-3*x^2;
end
plot(x,y)
Plot like this code
x=-10:10;
y=-3*x.^2;
plot(x,y,'--o')
I've tried
for x=-10:10
y(x)=-3*x.^2;
end
plot(x,y,'--o')
which doesn't work because I can't have the negative x numbers as y(x). I'm lost on how to fix this. Thank you.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 11일
편집: Azzi Abdelmalek 2013년 9월 11일
You can avoid using negative index
x=-10:10;
n=numel(x);
y=zeros(1,n) % Pre-allocate
for k=1:numel(x)
y(k)=-3*x(k)^2;
end
plot(x,y,'--o')

추가 답변 (1개)

Mahdi
Mahdi 2013년 9월 11일
When you plot, you`re only plotting the last point because the variable y is inside the for loop (and is not a matrix). Other than what`s been suggested, the way that you can do it is
for x=-10:10;
y=-3*x^2;
plot(x,y)
hold on
end
  댓글 수: 1
Mahdi
Mahdi 2013년 9월 11일
Please note that you should avoid for-loops to plot things like this because they`ll be resource-consuming.

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by