ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 23 Jul 2014 05:58:36 +0200Absolute value of complex numbers in 3D plothttps://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/When I try to make a 3d plot with complex numbers in it, such as this one:
plot3d(1+x+abs(i*omega), (x,0,5), (omega,0,5), viewer='tachyon')
I get errors saying
TypeError: unable to coerce to a real number
On another one I got
TypeError: Unable to convert 0.0345000000000000*I to float
Why is this happening?
---
EDIT:
Thanks for *slelievre*, lambda makes minimal example work. However, the original problem (I wanted to plot frequency responses) gives another error. Here is the original problem:
G_do_ideal_qn = -31.6656111462951*(0.0345000000000000*I*omega - 359.196869250000)/(-0.000389160000000000*omega^2 + 1.03531167486000*I*omega + 180.342274500000)
Tried to plot with
plot3d(lambda omega, V_C: 20*log(abs(G_do_ideal_qn), 10), (omega, 10, 100e3), (V_C, 0, 400))
Gives error
TypeError: unable to simplify to float approximationTue, 22 Jul 2014 23:43:09 +0200https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/Answer by slelievre for <p>When I try to make a 3d plot with complex numbers in it, such as this one:</p>
<pre><code>plot3d(1+x+abs(i*omega), (x,0,5), (omega,0,5), viewer='tachyon')
</code></pre>
<p>I get errors saying</p>
<pre><code>TypeError: unable to coerce to a real number
</code></pre>
<p>On another one I got</p>
<pre><code>TypeError: Unable to convert 0.0345000000000000*I to float
</code></pre>
<p>Why is this happening?</p>
<hr/>
<p>EDIT:
Thanks for <em>slelievre</em>, lambda makes minimal example work. However, the original problem (I wanted to plot frequency responses) gives another error. Here is the original problem:</p>
<pre><code>G_do_ideal_qn = -31.6656111462951*(0.0345000000000000*I*omega - 359.196869250000)/(-0.000389160000000000*omega^2 + 1.03531167486000*I*omega + 180.342274500000)
</code></pre>
<p>Tried to plot with </p>
<pre><code>plot3d(lambda omega, V_C: 20*log(abs(G_do_ideal_qn), 10), (omega, 10, 100e3), (V_C, 0, 400))
</code></pre>
<p>Gives error</p>
<pre><code>TypeError: unable to simplify to float approximation
</code></pre>
https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?answer=23538#post-id-23538(In your example, you could just replace `abs(i*omega)` by `abs(omega)`, but I'm sure you gave a simpler example than the examples you really care about.)
It often helps to use a lambda function for plots, as in:
plot3d(lambda x, omega: 1+x+abs(i*omega), (x,0,5), (omega,0,5), viewer='tachyon')
I think the explanation is that functions applied to symbolic expressions often misbehave,
while the lambda trick makes sure the functions only get applied to numerical input for plotting.
----------
EDIT:
In the new problem described in your edit, you ask for a 3d plot of a function of omega and V_C, but this function depends only on omega, and not at all on V_C, so a 2d plot would suffice. But maybe that's again just because you used a simplified version of your input, for the sake of the question.
One way to make things work is to make 'G_do_ideal_qn' a function rather than just an expression.
Below I'm shortening a tiny bit the names and the values just to make things fit on one line.
sage: G_do(om,V) = -31.*(0.03*I*om - 359.)/(-0.0003*om^2 + 1.03*I*om + 180.)
sage: plot3d(lambda om, V: 20*log(abs(G_do(om,V)), 10), (om, 10, 100e3), (V, 0, 400))
Tue, 22 Jul 2014 23:53:09 +0200https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?answer=23538#post-id-23538Comment by Rongcui Dong for <p>(In your example, you could just replace <code>abs(i*omega)</code> by <code>abs(omega)</code>, but I'm sure you gave a simpler example than the examples you really care about.)</p>
<p>It often helps to use a lambda function for plots, as in:</p>
<pre><code>plot3d(lambda x, omega: 1+x+abs(i*omega), (x,0,5), (omega,0,5), viewer='tachyon')
</code></pre>
<p>I think the explanation is that functions applied to symbolic expressions often misbehave,
while the lambda trick makes sure the functions only get applied to numerical input for plotting.</p>
<hr/>
<p>EDIT:
In the new problem described in your edit, you ask for a 3d plot of a function of omega and V_C, but this function depends only on omega, and not at all on V_C, so a 2d plot would suffice. But maybe that's again just because you used a simplified version of your input, for the sake of the question.</p>
<p>One way to make things work is to make 'G_do_ideal_qn' a function rather than just an expression.
Below I'm shortening a tiny bit the names and the values just to make things fit on one line.</p>
<pre><code>sage: G_do(om,V) = -31.*(0.03*I*om - 359.)/(-0.0003*om^2 + 1.03*I*om + 180.)
sage: plot3d(lambda om, V: 20*log(abs(G_do(om,V)), 10), (om, 10, 100e3), (V, 0, 400))
</code></pre>
https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?comment=23539#post-id-23539OK, the minimal example works... Then I moved forward to approach the original problem (I wanted to plot frequency response) with 1+x+log(abs(i*omega), 10) and another error came up. I will update on original question.Wed, 23 Jul 2014 00:37:23 +0200https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?comment=23539#post-id-23539Comment by slelievre for <p>(In your example, you could just replace <code>abs(i*omega)</code> by <code>abs(omega)</code>, but I'm sure you gave a simpler example than the examples you really care about.)</p>
<p>It often helps to use a lambda function for plots, as in:</p>
<pre><code>plot3d(lambda x, omega: 1+x+abs(i*omega), (x,0,5), (omega,0,5), viewer='tachyon')
</code></pre>
<p>I think the explanation is that functions applied to symbolic expressions often misbehave,
while the lambda trick makes sure the functions only get applied to numerical input for plotting.</p>
<hr/>
<p>EDIT:
In the new problem described in your edit, you ask for a 3d plot of a function of omega and V_C, but this function depends only on omega, and not at all on V_C, so a 2d plot would suffice. But maybe that's again just because you used a simplified version of your input, for the sake of the question.</p>
<p>One way to make things work is to make 'G_do_ideal_qn' a function rather than just an expression.
Below I'm shortening a tiny bit the names and the values just to make things fit on one line.</p>
<pre><code>sage: G_do(om,V) = -31.*(0.03*I*om - 359.)/(-0.0003*om^2 + 1.03*I*om + 180.)
sage: plot3d(lambda om, V: 20*log(abs(G_do(om,V)), 10), (om, 10, 100e3), (V, 0, 400))
</code></pre>
https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?comment=23541#post-id-23541I edited my answer to address your updated question.Wed, 23 Jul 2014 05:58:36 +0200https://ask.sagemath.org/question/23537/absolute-value-of-complex-numbers-in-3d-plot/?comment=23541#post-id-23541