I am getting an error for python3 can't convert complex tofloat.
My code (excludes imports due to it already being done in thewebsite)
The results of testing:
Why am I getting this error? I really can't tell. Pleasehelp.
Question 16 Incorrect Mark 0.00 out of 4.00 Flag question Let's use Sympy to make a grapher for any (Sympy) expression! Write a function sympy_grapher (expression, granularity=0.1, limits-[-10,10,-10,10]) which takes a string containing a sympy expression, an optional parameter granularity which represents how far apart each x-axis point is that you should graph, and an optional parameter limits, a tuple representing your (x-min, x-max, y- min and y-max) axis limits. Note that limits are inclusive (ie your minimum and maximum points should also be calculated and graphed). The function should returns a pyplot object (i.e., plt) that graphs the expression (ie a graph of y = expression) to the granularity and limits variables specified. All expressions should contain only one symbol. You may want to look into the attribute free_symbols of sympy expressions in order for your function to be able to determine what this symbol is. For this question sympy has been imported as sp, numpy as np and matplotlib.pyplot as plt. parse_expr() is also available as in previous questions. Using the example case values, your graph should look something like this: 10.0 7.5 5.0 0.0 -2.5 -5.0- -7.5 - -10.0- -10.0 For example: Test -5.0 -2.5 0.0 expr = "x ** 2" plt1 = sympy_grapher (expr) save_plt(plt1, "output.png") plt2 = ans (expr) save_plt (plt2, "ans.png") val= main("output.png", "ans.png") if val <= 0.005: print("Pass!") else: 2.5 5.0 7.5 10.0 print("Your graph is different to the expected output, difference: {:.4f}" .format(val)) Result Pass!
Answer: (penalty regime: 0, 0, 10, 20, ... %) 1 def sympy_grapher (expr, gran-0.1, limits=[-10, 10, -10,10]): """graph any sympy expression""" 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 xmin=limits [0] xmax=limits [1] ymin=limits [2] ymax=limits [3] x_val=np.arange(xmin+1, xmax, gran) plt.ylim (ymin, ymax) plt.xlim(xmin,xmax) pol=parse_expr (expr) y=[] for val in x_val: evaluated_value=pol.subs("x", val) y.append(evaluated_value) plt.plot(x_val,y) return plt Precheck Check li
✓ ✓ X Test expr = "x ** 2" plt1 = sympy_grapher (expr) save_plt(plt1, "output.png") plt2 = ans (expr) save_plt(plt2, "ans.png") val main("output.png", "ans.png") if val < 0.005: print("Pass!") else: print("Your graph is different to the expected output, difference: {:.4f}".format(val)) expr "sin(x) ** 2" gran 1 plt1 sympy_grapher (expr, gran) save_plt(plt1, "output.png") plt2 = ans (expr, gran) save_plt (plt2, "ans.png") val - main("output.png", "ans.png") if val < 0.005: print("Pass!") else: print("Your graph is different to the expected output, difference: {:.4f}".format(val)) expr = "1/x" limits_list = [-1, 1, -1, 1] plt1= sympy_grapher (expr, limits-limits_list) save_plt(plt1, "output.png") plt2 ans (expr, limits-limits_list) save_plt(plt2, "ans.png") val main("output.png", "ans.png") if val < 0.005: print("Pass!") else: print("Your graph is different to the expected output, difference: {:.4f}".format(val)) Testing was aborted due to error. Your code must pass all tests to earn any marks. Try again Expected Got Pass! Pass! Pass! Pass! Pass! ***Error*** Traceback (most recent call last): File "_tester_.python3", line 97, ir plt1 sympy_grapher (expr, limits-li File "_tester_.python3", line 68, ir plt.plot(x_val,y) File "/usr/local/lib/python3.9/dist-pa return gca().plot( File "/usr/local/lib/python3.9/dist-pa self.add_line (line) File "/usr/local/lib/python3.9/dist-pi self._update_line_limits(line) File "/usr/local/lib/python3.9/dist-pa path line.get_path() File "/usr/local/lib/python3.9/dist-pa self.recache() File "/usr/local/lib/python3.9/dist-pi y = _to_unmasked_float_array(yconv). File "/usr/local/lib/python3.9/dist-pa return np.asarray(x, float) File "/usr/local/lib/python3.9/dist-p return array(a, dtype, copy-False, File "/usr/local/lib/python3.9/dist-pa raise TypeError("can't convert compl TypeError: can't convert complex to flo
Expected Got Pass! Pass! Pass! Pass! Pass! ***Error*** Traceback (most recent call last): File " _tester_.python3", line 97, in <module> plt1 = sympy_grapher (expr, limits-limits_list) File "_tester_.python3", line 68, in sympy_grapher plt.plot(x_val,y) File "/usr/local/lib/python3.9/dist-packages/matplotlib/pyplot.py", line 2988, in plot return gca().plot( File "/usr/local/lib/python3.9/dist-packages/matplotlib/axes/_axes.py", line 1607, in plot self.add_line (line) File "/usr/local/lib/python3.9/dist-packages/matplotlib/axes/_base.py", line 2105, in add_line self._update_line_limits(line) File "/usr/local/lib/python3.9/dist-packages/matplotlib/axes/_base.py", line 2127, in _update_line_limits path = line.get_path() File "/usr/local/lib/python3.9/dist-packages/matplotlib/lines.py", line 1022, in get_path self.recache() File "/usr/local/lib/python3.9/dist-packages/matplotlib/lines.py", line 668, in recache y = _to_unmasked_float_array(yconv).ravel() File "/usr/local/lib/python3.9/dist-packages/matplotlib/cbook/__init__.py", line 1333, in _to_unmasked_float_array return np.asarray(x, float) File "/usr/local/lib/python3.9/dist-packages/numpy/core/_asarray.py", line 102, in asarray return array(a, dtype, copy-False, order-order) File "/usr/local/lib/python3.9/dist-packages/sympy/core/expr.py", line 357, in _float_ raise TypeError("can't convert complex to float") TypeError: can't convert complex to float < X
I am getting an error for python3 can't convert complex to float. My code (excludes imports due to it already being done
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am