Help with For-Loop question?

1 次查看(过去 30 天)
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

采纳的回答

Robert
Robert 2011-10-5
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')

更多回答(1 个)

Walter Roberson
Walter Roberson 2011-10-5
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 个评论
Robert
Robert 2011-10-5
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 2011-10-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by