Help with For-Loop question?

I keep getting the same error, about not being any real or logical answer.
here is the question; Create a time vector x = 0:2*pi. Using a for-loop, plot ten different curves on the same graph where the first curve is x1 = sin(x), the second curve is x2 = sin(x1), the third curve is x3 = sin(x2) and so on. Note that by using a for-loop it is not necessary to create ten separate variables to solve this problem.
Here is what I've been entering
x=0
>> for i=[pi/5:pi/5:2*pi]
x(i)=sin(x(i-(pi/5)))
end
??? Attempted to access x(0); index must be a positive integer or logical.
Any help would be appreciated

1 Kommentar

Walter Roberson
Walter Roberson am 5 Okt. 2011
Duplicate is at http://www.mathworks.com/matlabcentral/answers/17480-help-with-for-loop-question

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Robert
Robert am 5 Okt. 2011

0 Stimmen

The answer above doesn't achieve what you were asking for. Try this:
N=10;
x=zeros(N,numel(pi/5:pi/5:2*pi));
x(1,:)=pi/5:pi/5:2*pi;
for j=2:N
x(j,:)=sin(x(j-1,:));
end
plot(x(1,:),x')

4 Kommentare

Robert
Robert am 5 Okt. 2011
But if you're doing this for a class, make sure you understand what the code above does, otherwise you're learning nothing.
Abdullah Tamimi
Abdullah Tamimi am 5 Okt. 2011
alright, so far it all makes sense, but what does the colon in x(1,:) and x(j,:) mean?
Fangjun Jiang
Fangjun Jiang am 5 Okt. 2011
x(1,:) is all the data in the 1st row of the matrix x.
x(j,:) is all the data in the jth row of the matrix x.
This link might be helpful for you. It's also available in your local copy of the MATLAB documentation. Type doc to start browsing.
http://www.mathworks.com/help/techdoc/math/f1-85462.html
Walter Roberson
Walter Roberson am 5 Okt. 2011
We, the adherents of Computational Satreism, believe that MATLAB colons are "things in themselves" whose meaning can never be understood by the reflective consciousness. The colons just *are*, and can be used, but no-one can ever understand their true nature.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 5 Okt. 2011

1 Stimme

When you use i=[pi/5:pi/5:2*pi], i is a vector with increment as pi/5, hardly any of the element is integer, you can not use i as the index of a vector as x(i). Try something like,
N=10;
x=zeros(N,1);
for k=1:N
a=k*pi/5;
x(k)=sin(a);
end
i is the square root of -1 so avoid using i. Try this:
clear i
i

4 Kommentare

Abdullah Tamimi
Abdullah Tamimi am 5 Okt. 2011
why does x=zeros(N,1)
sorry, my class hasn't gone this far into matlab yet, we are in reallly basic functions right now.
Sean de Wolski
Sean de Wolski am 5 Okt. 2011
it's preallocating for speed. You create the full vector at once, the size you need it, and then populate it as the for loop runs.
Sean de Wolski
Sean de Wolski am 5 Okt. 2011
for your own knowledge, you should just type it at the command line and see what it creates
Fangjun Jiang
Fangjun Jiang am 5 Okt. 2011
Without the x=zeros(N,1) ahead of the for-loop, the code still works. It's just the vector x will increase size at every loop. This is not efficient especially when N is large, as MATLAB likes to store vector or matrix in a continuous memory location.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by