Python Forum
Missing 1 required positional argument in python code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Missing 1 required positional argument in python code (/thread-32056.html)



Missing 1 required positional argument in python code - edwinostby - Jan-18-2021

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'



RE: Missing 1 required positional argument in python code - Axel_Erfurt - Jan-18-2021

f[i + 1] = f[i] + derived_f(t[i])

has only one argument for derived_f

t[i]


RE: Missing 1 required positional argument in python code - jefsummers - Jan-18-2021

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.


RE: Missing 1 required positional argument in python code - edwinostby - Jan-18-2021

(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!


RE: Missing 1 required positional argument in python code - Axel_Erfurt - Jan-18-2021

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]


RE: Missing 1 required positional argument in python code - edwinostby - Jan-18-2021

(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!


RE: Missing 1 required positional argument in python code - jefsummers - Jan-18-2021

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

?


RE: Missing 1 required positional argument in python code - Serafim - Jan-19-2021

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).