Plotting using a for loop

I'm having trouble with plotting a graph when using a for loop to generate the values.
x = 0: 0.1 : 1.4
for x = 0:0.1:1.4
y = iteration2shear(x)
end
plot(x,y)
hold on
The function I'm calling generates the correct values for each value of y, but im struggling to generate a plot to illustrate it.

답변 (2개)

Rik
Rik 2020년 10월 26일

0 개 추천

You are forgetting to index your y variable. In general it is easier to write your code like this in such cases:
x = 0: 0.1 : 1.4;
y=zeros(size(x));
for n=1:numel(x)
y(n) = iteration2shear(x);
end
plot(x,y)
Jon
Jon 2020년 10월 26일

0 개 추천

You need to save your values to a vector you are just replacing a single scalar value

댓글 수: 1

Jon
Jon 2020년 10월 26일
I see that by the time I answered your question Rik had also given you a good answer

이 질문은 마감되었습니다.

태그

질문:

2020년 10월 26일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by