The intent of the assignment is to create vectors x and y, not just single values of x and y as your code is currently doing. So you need to use indexing on those x and y values in your loop. The starting point will be x(1) and y(1). The next point will be x(2) and y(2) which will depend on x(1) and y(1) per the random formula. In general, each successive x(n+1) and y(n+1) will depend on x(n) and y(n). E.g., an outline:
N = 30000;
x = zeros(N,1);
y = zeros(N,1);
x(1) = 0.5;
y(1) = 0;
for n = 1:N-1
r = rand();
if r < 0.02
x(n+1) = 0.5;
y(n+1) = 0.27 * y(n);
elseif r < 0.02 + 0.15
x(n+1) = x -0.139 * r + 0.246 * r;
y(n+1) = y + 0.263 * r + 0.224 * r - 0.036;
elseif r < 0.02 + 0.15 + 0.13
x(n+1) = x + 0.17 * r - 0.032 * r;
y(n+1) = y + 0.034 * r + 0.739 * r + 0.27;
else
x(n+1) = x + 0.781 * r - 0.032 * r;
y(n+1) = y + 0.034 * r + 0.1075 + 0.739 * r + 0.27;
end
end
I changed the loop counter to match the formula above so it would be easier for you to see the correlation between the code and the formula. I fixed up the 2% case. You need to fix up all of the other cases.
Best Answer