belal jafar
belal jafar

Reputation: 131

Plotting complex functions

How to plot complex functions in Matlab? For example:

Y[e^jx] = 1 / (1 - cosx + j4)

I tried some code, but I think the right way is by plotting real and imaginary part separately.

Upvotes: 3

Views: 35622

Answers (7)

Nico Schlömer
Nico Schlömer

Reputation: 58951

As an alternative for Python, you can use a tool I wrote: cplot. It uses domain coloring for complex plotting, i.e., it associated the absolute value of f(z) with the brightness and the hue with the complex angle.

Your function:

import cplot
import numpy as np

plt = cplot.plot(lambda z: 1 / (1 - np.cos(z) + 4j), (-10, +10, 400), (-10, +10, 400))
plt.show()

enter image description here

Upvotes: 0

Mikaël Mayer
Mikaël Mayer

Reputation: 10710

Maybe not for you, but for other people looking to draw complex functions. We set up a website where you can render them quickly and download them (reflex4you.com, reflex = representation of complex function)

I can display complex functions in 2D in a colorful way. Your function can be visible here and below:

Note that the black is zero, the white infinite, and it covers the complex plane with colors associated to complex numbers, such as red = 1, cyan = -1, i = greenish, -i = purplish.

Reflex of the function he asked.

Upvotes: 1

jmbr
jmbr

Reputation: 3348

There are some MATLAB functions that are specific to plotting complex maps:

z = cplxgrid(60);
cplxmap(z, 1./(1 - cos(z) + 4*i));

See also Functions of Complex Variables in MATLAB's documentation.

Upvotes: 2

0x90
0x90

Reputation: 41022

You can use one of the following:

  • plot(real(Y))
  • plot(imag(Y))
  • plot(real(Y),imag(Y))
  • plot(abs(Y))

Upvotes: 0

Serg
Serg

Reputation: 14118

By default, plot(X) will plot real vs imaginary, so it's equal to plot(real(X), imag(X)) For example, try:

>> r = sort(rand(10, 1)) + 1i * rand(10, 1);
>> figure, plot(r)

If you need them both on y-axis, use:

plot([real(X), imag(X)])

Upvotes: 0

Fredrik
Fredrik

Reputation: 2317

plot(re(Y),im(Y))

but remember there is a domain associated with a complex function in which it is valid, in your case: cos(x)-4j < 1

Upvotes: 0

plesiv
plesiv

Reputation: 7028

x = linspace(-pi, pi, 1e3);
y = 1./(1 - cos(x) + i*4);

% Plot absolute value and phase
figure;
subplot(2,1,1); plot(x, abs(y));
subplot(2,1,2); plot(x, angle(y));

% Plot real and imaginary parts
figure;
subplot(2,1,1); plot(x, real(y));
subplot(2,1,2); plot(x, imag(y));

Upvotes: 2

Related Questions