简体   繁体   中英

Matlab plot function defined on a complex coordinate

I would like to plot some figures like this one:

-axis being real and imag part of some complex valued vector(usually either pure real or imag)

-have some 3D visualization like in the given case

在此输入图像描述

First, define your complex function as a function of (Re(x), Im(x)) . In complex analysis, you can decompose any complex function into its real parts and imaginary parts. In other words:

F(x) = Re(x) + i*Im(x) 

In the case of a two-dimensional grid, you can obviously extend to defining the function in terms of (x,y) . In other words:

F(x,y) = Re(x,y) + i*Im(x,y) 

In your case, I'm assuming you'd want the 2D approach. As such, let's use I and J to represent the real parts and imaginary parts separately. Also, let's start off with a simple example, like cos(x) + i*sin(y) which is based on the very popular Euler exponential function . It isn't exact, but I modified it slightly as the plot looks nice.

Here are the steps you would do in MATLAB:

  1. Define your function in terms of I and J
  2. Make a set of points in both domains - something like meshgrid will work
  3. Use a 3D visualization plot - You can plot the individual points, or plot it on a surface (like surf , or mesh ).

NB: Because this is a complex valued function, let's plot the magnitude of the output. You were pretty ambiguous with your details, so let's assume we are plotting the magnitude.

Let's do this in code line by line:

% // Step #1
F = @(I,J) cos(I) + i*sin(J);

% // Step #2
[I,J] = meshgrid(-4:0.01:4, -4:0.01:4);

% // Step #3
K = F(I,J);

% // Let's make it look nice!
mesh(I,J,abs(K));
xlabel('Real');
ylabel('Imaginary');
zlabel('Magnitude');
colorbar;

This is the resultant plot that you get:

在此输入图像描述

Let's step through this code slowly. Step #1 is an anonymous function that is defined in terms of I and J . Step #2 defines I and J as matrices where each location in I and J gives you the real and imaginary co-ordinates at their matching spatial locations to be evaluated in the complex function. I have defined both of the domains to be between [-4,4]. The first parameter spans the real axis while the second parameter spans the imaginary axis. Obviously change the limits as you see fit. Make sure the step size is small enough so that the plot is smooth. Step #3 will take each complex value and evaluate what the resultant is. After, you create a 3D mesh plot that will plot the real and imaginary axis in the first two dimensions and the magnitude of the complex number in the third dimension. abs() takes the absolute value in MATLAB. If the contents within the matrix are real, then it simply returns the positive of the number. If the contents within the matrix are complex, then it returns the magnitude / length of the complex value.

I have labeled the axes as well as placed a colorbar on the side to visualize the heights of the surface plot as colours. It also gives you an idea of how high and how long the values are in a more pleasing and visual way.

As a gentle push in your direction, let's take a slice out of this complex function. Let's make the real component equal to 0, while the imaginary components span between [-4,4] . Instead of using mesh or surf , you can use plot3 to plot your points. As such, try something like this:

F = @(I,J) cos(I) + i*sin(J);

J = -4:0.01:4;
I = zeros(1,length(J));

K = F(I,J);
plot3(I, J, abs(K));
xlabel('Real');
ylabel('Imaginary');
zlabel('Magnitude');
grid;

plot3 does not provide a grid by default, which is why the grid command is there. This is what I get:

在此输入图像描述

As expected, if the function is purely imaginary, there should only be a sinusoidal contribution ( i*sin(y) ).

You can play around with this and add more traces if you need to.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM