How we can find equation of contour line?

34 views (last 30 days)
Hi there
I used the contour function and I need contour line 's equations, Can any one help me to How I can find equation of contour lines?
  1 Comment
Matt J
Matt J on 5 Jul 2023
How was the input data that you used for the contour function generated, if you didn't start with an equation?

Sign in to comment.

Accepted Answer

Shubham Dhanda
Shubham Dhanda on 5 Jul 2023
Edited: Shubham Dhanda on 5 Jul 2023
Hi,
I understand that you want to find equation of contour line. To find the equation of contour lines, you need to extract the coordinates of the contour lines from the contour plot and then fit a mathematical function to these coordinates.
Here's an example of how you can do this in MATLAB:
% Create a sample matrix
Z = peaks(50);
% Plot the contour lines
contour(Z);
% Extract the contour line data
contourData = contourc(Z);
% Check the number of contour lines
numLines = contourData(2, 1);
if numLines < 2
disp('Insufficient contour lines.');
return;
end
% Plot additional lines
hold on;
% Extract the x and y coordinates from the contour line data
x1 = contourData(1, 1:numLines);
y1 = contourData(2, 1:numLines);
% Fit a polynomial function to the first contour line
degree1 = 2; % Choose the degree of the polynomial
p1 = polyfit(x1, y1, degree1);
% Get the equation of the first contour line
equation1 = poly2str(p1, 'x');
disp(['Equation of the first contour line: ', equation1]);
Equation of the first contour line: 0.0050903 x^2 - 0.19604 x + 13.6414
% Plot the first contour line
x1_fit = linspace(min(x1), max(x1), 100);
y1_fit = polyval(p1, x1_fit);
plot(x1_fit, y1_fit, 'r', 'LineWidth', 2);
% Check if there is a second contour line
if numLines >= 4
% Extract the x and y coordinates from the contour line data
x2 = contourData(1, numLines+1:end);
y2 = contourData(2, numLines+1:end);
% Fit a polynomial function to the second contour line
degree2 = 3; % Choose the degree of the polynomial
p2 = polyfit(x2, y2, degree2);
% Get the equation of the second contour line
equation2 = poly2str(p2, 'x');
disp(['Equation of the second contour line: ', equation2]);
% Plot the second contour line
x2_fit = linspace(min(x2), max(x2), 100);
y2_fit = polyval(p2, x2_fit);
plot(x2_fit, y2_fit, 'g', 'LineWidth', 2);
end
Equation of the second contour line: -0.0013694 x^3 + 0.091492 x^2 - 1.8779 x + 37.5143
% Add labels and legend
xlabel('x');
ylabel('y');
legend('Contour Lines', 'Contour Line 1', 'Contour Line 2');
hold off;

More Answers (1)

John D'Errico
John D'Errico on 5 Jul 2023
Edited: John D'Errico on 5 Jul 2023
It would be rarely true that you could find an "equation" for any given contour line, at least on any problem that involved real world data. Contour lines are typically created as piecewise linear things, thus connect-the-dots, with straight line segments. If there is any noise in the data, then the contour lines will have no simple equation form.
But COULD you find an "equation for a simple problem? First, what does a contour line mean in the first place?
A contour line is the name used to describe what is sometimes described as a level surface. These things are often best understood using an example. I'll just make up a simple function here, where I have not a clue what the thing looks like as I type:
syms x y
z(x,y) = (2*x - y)/5 + sin(x + y)
z(x, y) = 
fsurf(z)
Ok. That makes sense. An inclined surface, linearly increasing in one direction, but with sinusoidal ripples acrsoss it. Now, what does a contour mean? We pick SOME value for z. I'll call it z0. Then we find the set of all pairs (x,y), such that z(x,y) == z0. For example, I'll choose z0=1.
z0 = 1;
H = fcontour(z)
H =
FunctionContour with properties: Function: [1×1 symfun] LineColor: 'flat' LineStyle: '-' LineWidth: 0.5000 Fill: off LevelList: [-3 -2 -1 0 1 2 3] Show all properties
H.LevelList = 1;
xlabel X
ylabel Y
So that is the 1-contour of z, the locus of all pairs (x,y), such that z(x,y) == 1. In this case, of course, we have the function z itself. So the "equation" of that line is given as
z(x,y) == z0
ans = 
Is that equation of any useful value? Not really. It is what is known as an implicit function. And sadly, there is no simple way we can write down a function of the form y(x). This relationship, for any value of x, typically has multiple values of y. It is not a single-valued function.
Next, consider a far simpler problem, where a solution does exist.
w(x,y) = x + 3*y
w(x, y) = 
fcontour(w)
Here of course, the problem is a simple one. The contour line for w(x,y) = w0 is just
syms w0
w(x,y) == w0
ans = 
And clearly that is the equation of a line. At least I hope you recognize that as a line. The contours of w are parallel and straight lines that stretch to infinity in either direction.
Another one, that is slightly less trivial...
u(x,y) = (x-2)^2 + 2*(y+1)^2
u(x, y) = 
fcontour(u)
And if I think back to some vague high school class where I learned about conic surfaces, this surface is a paraboloid. Horizontal planar slices through that surface will generate ellipses.
syms u0
u(x,y) == u0
ans = 
And here we should recognize that as the equation of an ellipse, with center at (2,-1). So, essentially, IF you have the function used to generate a contour plot, then it is trivial to find an equation of a contour line, though it may not always be of value. It may be just an implicit function, where no simple direct single valued functional form exists. In other cases, finding the equation of that contour line may be impossible. For example, consider the surface:
V = cumsum(rand(20),1) + cumsum(rand(20),2);
surf(V)
While we can compute contours, they will be squiggly things.
contour(V)
And, yes, we could look back at how I generated that "surface", and find a way to predict the general shape of that surface and the shape of those contour lines, if we could smooth them sufficiently.
So, CAN you find the equation of a contour line? Sometimes yes, sometimes not at all, and often, on real world problems, that equation would be of no value at all.

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!