Help with Basic 2D Contour Plot

조회 수: 1 (최근 30일)
James
James 2013년 5월 10일
I am trying to get this contour plot to work. This is a simple example so I can figure out how to do it before applying it to trickier equations.
I have this function saved in a separate .m file:
function a = triarea(x,y)
a = x.^3-2.*x-5+y.^2;
end
The main script calls this function:
for s=[0:1:3],
for t=[0:1:3],
q=triarea(s,t)
end
end
This displays the range of values for q (one after the other, ideally I'd like them as one matrix upon completion but don't know how to do that).
What I want to do is a contour plot where the x-axis is the s variable and the y-axis is the t variable and q is represented as the contour i.e. varying in colour depending on it's value at each s,t point.
How do I do this?
It says that the contour variable Z must be at least a 2x2 matrix but I have only one value at each s,t point.

채택된 답변

Friedrich
Friedrich 2013년 5월 10일
편집: Friedrich 2013년 5월 10일
Hi,
don't use a loop. Use meshgrid:
s=[0:1:3]
t=[0:1:3]
[X,Y] = meshgrid(s,t)
Z =triarea(X,Y);
contour(X,Y,Z)
This works as long the function triarea accepts matrices as input like it does in your example. Also you don't need to care about s and t (they dont need the same length and the values also doesn't matter).
s=-pi:0.1:pi;
t = 10:0.1:20;
[X,Y] = meshgrid(s,t)
Z =triarea(X,Y);
contour(X,Y,Z)

추가 답변 (4개)

John Doe
John Doe 2013년 5월 10일
편집: John Doe 2013년 5월 10일
Will this work for you?
q = zeros(4);
for s=[0:1:3],
for t=[0:1:3],
q(t+1,s+1)=triarea(s,t)
end
end
Note that this will only work if t and s contain only integers. If they do not:
s = [0:0.1:3];
t = [0:0.1:3];
q = zeros(length(s));
for i = 1:length(s)
for j = 1:length(t)
q(i,j) = triarea(s(i),t(j));
end
end
- Rob

James
James 2013년 5월 10일
Thanks for your answers. Huge leap forward in my work.
I now get the values of q displayed as a matrix:
Z =
-5 -6 -1 16
-4 -5 0 17
-1 -2 3 20
4 3 8 25
I also have a contour plot of q in terms of s,t which looks like this:
So all seems good. Apart from that contour plot (the colours) don't seem to correspond to the matrix values (i.e. I thought top left be most blue and bottom left most red, and that doesn't seem to be the case).

John Doe
John Doe 2013년 5월 10일
편집: John Doe 2013년 5월 10일
Follow the instructions from Friedrich, but flip the matrix around before you plot it.
Now, Z(1,1) is in the origin, Z(1,2) is in (0,1), Z(3,3) is in (2,2) etc. As you go downwards in Z, you move upwards on the Y-axis (in the plot).
ZZ = flipud(Z);

James
James 2013년 5월 10일
I have used Friedrich's code and got that contour plot.
If I flip Z before I plot it:
[X,Y] = meshgrid(s,t);
Z =triarea(X,Y);
ZZ=flipud(Z)
contourf(X,Y,ZZ);
This displays the matrix as it should be but also flips the contour plot!
If I plot is as before i.e. plot Z then put 'ZZ=flipud(Z)' after I can see the matrix that corresponds correctly to the contour plot.
I could live with remembering that one of either the plot or the matrix is flipped but sure there must be a tweak to fix it. Quite an odd problem.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by