Python Forum
Missing 1 required positional argument in python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Missing 1 required positional argument in python code
#1
Hey, I have an assignment due this week. The given task is to solve a differential equation by using Euler's method, the task is;

Solve the differential equation, f'(t) = 4 + 3 * f(t), with t ∈ [0,2], by using Euler's method. Thereafter plot the solution. Use f(0) = 0 as initial condition. Use 10 000 steps.

This is the code that ive come up with so far, but i cant find a solution to the error given. Currently on windows 10, 64-bit using python 3.8.5.

import numpy as np
from matplotlib import pyplot as plt

def derived_f(t, ft):
    return(4 + 3 * f(t))

t0 = 0        
f0 = 0         
t_end = 2     

N = 10000      
h = (t_end - t0) / (N - 1)           

t = np.linspace(t0, t_end, N)
f = np.zeros(N)
f[0] = f0

for i in range(N - 1):
    f[i + 1] = f[i] + derived_f(t[i]) * h
    
plt.plot(t, f)
plt.title("Title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
Error:
TypeError: derived_f() missing 1 required positional argument: 'ft'
Reply
#2
f[i + 1] = f[i] + derived_f(t[i])

has only one argument for derived_f

t[i]
edwinostby likes this post
Reply
#3
Several issues.
In line 4 you define a function with 2 arguments but in line 19 you call it with one. The function does not use the second argument, so eliminating that would help. However, in line 5 you make a call to a function called f that is not defined.
Reply
#4
(Jan-18-2021, 03:00 PM)jefsummers Wrote: Several issues.
In line 4 you define a function with 2 arguments but in line 19 you call it with one. The function does not use the second argument, so eliminating that would help. However, in line 5 you make a call to a function called f that is not defined.

Thanks for the reply!

I am struggling to use Euler's method to solve this equation "f'(t) = 4 + 3 *f(t)". As in line 4, i tried to do this;

def derived_f(t, ft):
return(4 + 3 *ft)
And then back in line 19 add this:

derived_f(f[t])

into the call function making it look like this;

f[i + 1] = f[i] + derived_f(t[i]) + derived_f(f[t]) * h
But i am still getting the same error:
Error:
f[i + 1] = f[i] + derived_f(t[i]) + derived_f(f[t]) * h TypeError: derived_f() missing 1 required positional argument: 'ft'
Really new at python so I might be missing something obvious here, but how should i add the argument 'ft' to remove the error in the code?

Thanks in advance!
Reply
#5
I don't know what you want to do, but this gives me a result

import numpy as np
from matplotlib import pyplot as plt

def derived_f(ft):
    return(4 + 3 * ft)
 
t0 = 0        
f0 = 0         
t_end = 2     
 
N = 10000      
h = (t_end - t0) / (N - 1)           
 
t = np.linspace(t0, t_end, N)
f = np.zeros(N)
f[0] = f0
 
for i in range(N - 1):
    f[i + 1] = f[i] + derived_f(t[i]) * h
     
plt.plot(t, f)
plt.title("Title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.show()
[Image: Figure_1.png?raw=1]
Reply
#6
(Jan-18-2021, 07:04 PM)Axel_Erfurt Wrote: I don't know what you want to do, but this gives me a result

import numpy as np
from matplotlib import pyplot as plt

def derived_f(ft):
    return(4 + 3 * ft)
 
t0 = 0        
f0 = 0         
t_end = 2     
 
N = 10000      
h = (t_end - t0) / (N - 1)           
 
t = np.linspace(t0, t_end, N)
f = np.zeros(N)
f[0] = f0
 
for i in range(N - 1):
    f[i + 1] = f[i] + derived_f(t[i]) * h
     
plt.plot(t, f)
plt.title("Title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.show()
[Image: Figure_1.png?raw=1]

Thanks, may have been unclear in the description but this is what I wanted to get. Thanks for the help, appreciate it!
Reply
#7
Or, are you looking at successive iterations?
One challenge in translating math expressions to programs is that math expressions commonly use 1 letter for functions, variables, etc. Programs are hard to read that way.
So, if ft actually f(t)? That would explain why you are passing t.
And is what you are returning supposed to be f_prime?

And is line 19 supposed to reflect f(i+1) = f(i) + derived_f(f(t[i]),t[i]) * h

?
Reply
#8
One step to a solution is understanding the Euler method.
The idea is to numerically calculate a decent approximation of something that otherwise can be quite difficult. You want to approximate as closely as possible the function f by only knowing the relation between the function and its first derivative. You have a starting point, in this case 0 and an end point (here 2). The idea is that this is enough if you can find an accurate point on the function close to the starting point in order to approximate the slope of the curve with that of a secant and thus be able to find another point of the curve. Then use that point to recalculate the slope in that point and so on until you reach the endpoint.
(The image is just to show the idea, it is not closely related to the function in the given problem).
[Image: 345px-Derivative.svg.png]
It is obvious that the approximation gets better if h is very small. Euler showed that if it is small enough then you can stepwise approximate the derivative and thus the whole function (and in many cases find the function f very accurately, especially in the case of first order differential equations). In the current case I think that the problem is a suitable introductionary problem to illustrate the Euler method.

The normal case for the Euler method is in general terms (I use "g" to avoid confusion with the given problem. I could use any letter to denote "some function")

y' = g(x,y)

Translated to the given problem it gives us

f'(t) = g(t, f(t)) => f'(t) = 4 + 3*f(t)

As there is no other dependency on "t" apart from "f(t)", which is replaced by a stepwise calculated constant the rule becomes quite simple when we translate it into the Euler formula

yn+1 = yn + f'(yn) * (tn+1 - tn)

which gives us

f(t1) = f(t0) + (4 + 3*f(t0)) * h

The start values are given: t0 = 0 and f(t0) = 0 (implicitly from the interval given as t ∈ [0,2] and f(0) = 0. Use 10 000 steps tells us that h = 2 / 9999 = 0.00020002...
Take two steps (to test it, I approximate h to 0.0002):

f[0] = 0
f[1] = f[0] + derived_f(f[0]) * h = 0 + derived_f(0) * 0.0002 = 0 + 4 * 0.0002 = 0.0008
f[2] = 0.0008 + derived_f(0.0008) * h = 0.0008 + (4 + 3 * 0.0008) * 0.0002 = 0.00160048
...

So, the absence of t in f'(t) simplifies the problem and only one parameter is needed in "derived_f" ("df" would be a better name in my opinion).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,292 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing positional arguments error?? hhydration 2 2,128 Oct-01-2020, 05:33 AM
Last Post: buran
  TypeError: Missing required positional arguments liaisa 7 28,860 Sep-25-2020, 08:16 PM
Last Post: deanhystad
  How to Use Python for solving the following physics question. Python Code required ishahid 8 3,600 Dec-18-2019, 06:59 AM
Last Post: akashraj128
  missing positional argument error programmert 1 2,805 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument jedmond2 4 6,651 Sep-19-2019, 12:00 PM
Last Post: jefsummers
  missing 1 required positional argument mcgrim 10 19,721 May-07-2019, 09:02 PM
Last Post: Yoriz
  TypeError: __init__() missing 3 required positional arguments Pythonhelp82 6 23,068 Jan-24-2019, 04:25 AM
Last Post: Pythonhelp82
  TypeError: method missing 1 positional argument koolinka 4 5,012 Nov-18-2018, 04:53 PM
Last Post: ichabod801
  another positional argument error (...and executing objects stored in a list) itmustbebunnies 7 4,195 Nov-16-2018, 07:18 PM
Last Post: itmustbebunnies

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020