Note that there are some explanatory texts on larger screens.

plurals
  1. POEuler's Method Python Variable Input Type
    primarykey
    data
    text
    <p>I'm having an issue with the following code. It's currently in progress but a large problem that I'm running into is that my input of a function returns an error no matter what type of input I have tried. It either returns with an issue with the error type, or an issue of x not being defined if I enter a function such as x.</p> <pre><code>f = raw_input("Please enter function: y' = ") x0 = float(raw_input("Please enter the initial x value: ")) y0 = float(raw_input("Please enter the initial y value: ")) xmax = float(raw_input("Please enter the value of x at which to approximate the solution: ")) h = float(raw_input("Please enter the step size: ")) showall = int(raw_input("Would you like to see all steps (1) or only the approximate solution (2)? ")) def f(x,y): value = f return (value) def euler(x0,y0,h,xmax): x=x0; y=y0; xd=[x0]; yd=[y0]; while x&lt;xmax: y = y + h*f(x,y) yd.append(y) x=x+h xd.append(x) return(xd,yd) (xvals,yvals) = euler(x0,y0,h,xmax) if showall == 1: print "" print "x_n y_n" for uv in zip(xvals, yvals): print uv[0],uv[1] elif showall == 2: print "" print "x_n y_n" print xvals, yvals else: print "" print "There has been an error with your choice of what to see; showing all steps." print "" print "x_n y_n" for uv in zip(xvals, yvals): print uv[0],uv[1] print " " plotask = int(raw_input("Would you like to see a plot of the data? Yes (1); No (2) ")) if plotask == 1: print "1" elif plotask == 2: pass else: print "" print "Could not understand answer; showing plot." </code></pre> <p>Any help would be appreciated.</p> <p>The error and trace is the following:</p> <pre><code> File "C:\Users\Daniel\Desktop\euler.py", line 25, in &lt;module&gt; (xvals,yvals) = euler(x0,y0,h,xmax) File "C:\Users\Daniel\Desktop\euler.py", line 19, in euler y = y + h*f(x,y) TypeError: unsupported operand type(s) for *: 'float' and 'function' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload