Help with For-Loop question?

1 Ansicht (letzte 30 Tage)
Abdullah Tamimi
Abdullah Tamimi am 5 Okt. 2011
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

Akzeptierte Antwort

Robert
Robert am 5 Okt. 2011
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')

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 5 Okt. 2011
t = 0:pi/5:2*pi;
curvenums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for K = 1 : numel(curvenums)./size(curvenums,1)
if ~curvenum(K)
x(:,curvenum(K+1)) = sin(t);
else
x(:,curvenum(K+1)) = sin(x(:,curvenum(K)));
end
end
plot(t,x)
  2 Kommentare
Robert
Robert am 5 Okt. 2011
That seems needlessly complicated. Why do you keep track of a "curvenum" array that is just 0:9? See my reply below.
Walter Roberson
Walter Roberson am 5 Okt. 2011
The posting is clearly a homework assignment. The code I provided is functional but would get no marks from any marginally alert marker as it would obviously not be written by the student. It is code that can be learned from but not profitably cloned as the assignment answer.
Straight forward and readily understood solutions, on the other hand, are fodder for student copying without any real understanding.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by