M HYPE SPLASH
// general

Matlab 'For' loop and line plotting [Beginners question]

By Michael Henderson
$\begingroup$
function question2a(a,x) y=zeros(1000,1); y(1)=x; hold on; for j=2:1000 y(j) = a*y(j-1)*exp(-y(j-1)); plot(j, y); end hold off;
end

Is my code for a certain exercise, however I am not getting a line instead of a line I am getting a lot of dots. Can someone please explain me how I can fix this?

The function should replicate this:

$$y_{n+1}=\alpha \cdot y_{n} \cdot e^{-y_n}$$

$$y_1 = \alpha \cdot y_0 \cdot e^{-y_0}, \qquad y_2 = \alpha \cdot y_1 \cdot e^{-y_1} = \alpha \cdot \alpha \cdot y_0 \cdot e^{-y_0} \cdot e^{-\alpha \cdot y_0 \cdot e^{-y_0}}, \qquad \ldots$$

$\endgroup$ 0

1 Answer

$\begingroup$

Advice Have a look the Matlab documentation center

Hint Run this code in a script and try to understand what is your problem.

z = rand(8,1);
figure
for i = 1:8 plot(i,z(i)); hold on;
end;
hold off;
figure
plot((1:8),z);
figure
plot(z);
$\endgroup$ 1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy