MATLAB Answers

0

Lorenz Equation || Vector lengths should be the same

Hetvi Vora さんによって質問されました 2019 年 1 月 20 日
最新アクティビティ Hetvi Vora さんによって コメントされました 2019 年 1 月 21 日
Hi, im trying to plot the lorenz butterfly using the equations. I can't get past the error "vectors must be of equal length." I understand that this means that one of my vairables has more/lesser points compared to the other two, but I can't figure out how. I can tell you that it is variable z because on plotting 2-D Plot(x,y) works but plot(y,z) and plot(x,z) dont work for the same error. Can someone tell me how to fix this?
This is my code:
x(1) = normrnd(0,1);
y(1) = normrnd(0,1);
z(1) = normrnd(0,1);
sigma= 10;
rho=28;
time = 0.05;
beta= 8/3;
for i = 2:1000
x(i) = x(i-1) + time*sigma*(y(i-1)-x(i-1));
y(i) = y(i-1) + time*(x(i-1)*(rho-z(i-1))-y(i-1));
z(i) = z(i-1) + time*(x(i-1)*y(i-1)-beta*z(i-1));
end
figure
plot3(x,y,z)

  0 件のコメント

サインイン to comment.

1 件の回答

John D'Errico
回答者: John D'Errico
2019 年 1 月 20 日
 採用された回答

First, you need to learn how to preallocate your vectors, and why. That code will be inefficient, because x, y, and z are all being grown dynamically. Yet you know the final size of those arrays will be 1x1000.
help zeros
Now, why does your code fail? We cannot know this, because the code actually works for me. My guess is that one or more of x,y, or z already existed as some unknown size arrays when you ran this code.
What does that mean? When you get an error, LOOK at the variables involved. What size are they? Learn to use whos when you see something confusing.
whos x y z
Name Size Bytes Class Attributes
x 1x1000 8000 double
y 1x1000 8000 double
z 1x1000 8000 double

  4 件のコメント

Hetvi Vora 2019 年 1 月 20 日
okay I fixed this! Yaaay!
But, can you tell me, what output do you get? Is it just a straight line? or do you get the butterfly?
John D'Errico
2019 年 1 月 20 日
z had a smaller size because x and y and z already existed before. They already had those sizes, probably from different runs that you made.
You may need to get used to starting your scripts with a clear, at least until you learn how to use functions. Best of course is to just get used to knowing what is in your workspace.
Next, for vectors of length 100000, you REALLY need to learn about preallocation. Do this:
x = zeros(1,100000);
for x,y, and z. Now you can assign values to those vectors at will as x(i), with no penalty in speed.
As for the result of that code, I did not actually plot anything. Here is what it produced for me:
x =
Columns 1 through 11
-0.063055 0.32584 0.45796 0.73883 1.176 1.8734 2.9819 4.7394 7.5064 11.785 18.094
Columns 12 through 22
26.181 31.928 19.221 -39.153 -96.16 -162.81 432.11 6092.8 86594 -3.7605e+07 -1.1072e+11
Columns 23 through 33
-3.0645e+17 1.1523e+27 2.5995e+43 1.0172e+69 -1.9466e+111 -6.7249e+178 -9.6366e+288 Inf NaN NaN NaN
Columns 34 through 44
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
which is clearly a divergent process. So my guess is you need to check your equations and the parameters you used in them.
Hetvi Vora 2019 年 1 月 21 日
Thank you so much!
I figured it out with your help and realised that if I adjust the constant to a particular value I get the butterfly - which is what the equation is all about! and I understand now what went wrong.
Thank you again!

サインイン to comment.



Translated by