Reputation: 131
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
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()
Upvotes: 0
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.
Upvotes: 1
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
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
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
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
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