Ask Your Question

How do I plot the magnitude of a complex-valued function?

asked 2015-10-04 20:41:07 +0200

Aditya gravatar image

updated 2015-10-05 01:26:12 +0200

My script is like this

v = var('v')


G = matrix([[0,1,0,0],[-v,1-v,0,0],[0,-v,1-v,0],[0,0,1,0]])

eigs = G.eigenvalues()

f1(v) = abs(eigs[0])


The first eigenvalue is complex in a certain region and real elsewhere. But it does not plot those portions of the plot where the eigenvalue becomes complex. What gives?

My plot is broken where the eigenvalue is complex, with the error:

verbose 0 (2717:, generate_plot_points) WARNING: When plotting, failed to evaluate function at 140 points. verbose 0 (2717:, generate_plot_points) Last error message: 'math domain error'

What I want to do is observe the behavior of the magnitude of the eigenvalue with respect to v, irrespective of whether the eigenvalue is real or complex.

If I try G = matrix(CC, [[0,1,0,0],[-v,1-v,0,0],[0,-v,1-v,0],[0,0,1,0]]) , I get an error.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-10-04 22:59:51 +0200

fidbc gravatar image

updated 2015-10-05 17:36:26 +0200

Perhaps this is what you want to use.

complex_plot(lambda v: abs(matrix(CC, [[0,1,0,0],[-v,1-v,0,0],[0,-v,1-v,0],[0,0,1,0]]).eigenvalues()[0]),(-10,10),(-10,10))

Sample output:

Output 1

Although, then you may want to drop the abs to get:

output 2

If you are just interested in the case where $v\in \mathbb{R}$ and want to plot the module of the eigenvalue then

plot(lambda v: abs(matrix(CC, [[0,1,0,0],[-v,1-v,0,0],[0,-v,1-v,0],[0,0,1,0]]).eigenvalues()[0]),(x,-20,10))

will do the job.

output 3

Update: Added case where $v\in\mathbb{R}$.

edit flag offensive delete link more


For further details about complex_plot you can consult the manual.

fidbc gravatar imagefidbc ( 2015-10-04 23:01:49 +0200 )edit

Thanks for the reply! You're right, I don't want v to be complex, but I thought that might force the eigenvalues to be interpreted as complex, or something. What I need is - how the magnitude of the eigenvalue behaves w.r.t. v, irrespective of whether the eigenvalue is real or complex.

Aditya gravatar imageAditya ( 2015-10-05 01:02:02 +0200 )edit

answered 2015-10-05 10:25:47 +0200

kcrisman gravatar image

You may need to use a lambda function.

lambda eg: abs(eg[0])

Haven't tested but some variant on this should work.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-10-04 20:41:07 +0200

Seen: 1,299 times

Last updated: Oct 05 '15