Creating a contour plot of data.

3 views (last 30 days)
Edward Steen
Edward Steen on 26 May 2017
Commented: Edward Steen on 26 May 2017
I am using a double loop, and want to create a contour (or 3d) plot from the data. I would like the variable related to the j index to be x, the variable related to the i index to be y, and the output of each loop iteration to be z. The surface plot would be of the (x,y,z) ordered triples. The code is below. So far, I'm not getting anything that makes sense. Thanks!
Ted
for j=1:50:1000 a4=j/1000; for i=1:50:1000 a3=i/1000;
initial=[1;0;0;0;0;0]; [t,x]=ode45('ModelSolver2',[0,10],initial);
[r,s]=size(x); B1e=x(r,1); Bne=x(r,2); B1pe=x(r,3); Bnpe=x(r,4); %Bme=p(5); %Bmpe=p(6);
B=[-(n^2*a2*B1e^(n-1)+a3),1,n*a1,0,0,0; a3,-(n^2*b2*B1pe^(n-1)+1),0,n*b1,0,0; n*a2*B1e^(n-1),0,-(a1+a4+(m/n)^2*b3*Bne^((m/n)-1)),b4,(m/n)*a5,0; 0,n*b2*B1pe^(n-1),a4,-(b1+(m/n)^2*a6*Bnpe^((m/n)-1)+b4),0,(m/n)*b5; 0,0,(m/n)*b3*Bne^((m/n)-1),0,-a5,0; 0,0,0,(m/n)*a6*Bnpe^((m/n)-1),0,-b5];
E=eig(B);
z(j,i)=E(1); s(i)=a3; y(j)=a4;
end end
Z=[s,y,z]; surf(Z)

Answers (1)

John D'Errico
John D'Errico on 26 May 2017
I see no function called ModelSolver2. So there is no way to test what you are doing.
which ModelSolver2
'ModelSolver2' not found.
I do see that you are assuming the first eigenvalue returned from eig is consistently the one that you want. NEVER make that assumption. Eigenvalues need not be generated in any specific order. So even when you do resolve the next problem I've point out, you will be coming back here soon asking how to fix eig. And sorting the eigenvalues is probably insufficient, since eigenvalues can change order when you change a parameter. So unless you explicitly want only the LARGEST eigenvalue, you will probably have a problem.
Next, you are creating a matrix that is 1000x1000, but only filling in every 50'th element. The rest of those elements are zero. Then trying to use surf on that mess will be meaningless. At best you will see a surface that is almost everywhere zero, except for a few sparse spikes. By the way, 1:50:1000 stops at 951.
By the way, learn to preallocate your arrays, instead of growing them one element at a time. Because otherwise, you will at some point be coming back to this forum to ask why your code is so slow.
  1 Comment
Edward Steen
Edward Steen on 26 May 2017
I had assumed that if the index moves in increments of 50, the array would have the same number of elements. That's incorrect I guess. Assuming I do want the largest eigenvalue, how do I create the surface plot? From what you say, am I correct in assuming that I need to preallocate my arrays, make my index increment 1 instead of 50, and then combine my arrays to form a 3 by n which I can then use surf on? The Modelsolver2 is just used to solve the system of diff eqs.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!