Creating a contour plot of data.
3 views (last 30 days)
Show older comments
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)
0 Comments
Answers (1)
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.
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!