Ich hab folgendes Problem:
Unser Uni Überprüfungsprogramm sagt bei meinen Code für die Methode der kleinsten Quadrate immer "No function found" bzw auch "No results returned", da es bei uns auf der Uni generel grad ein paar Probleme gibt würd ich nur von euch gern hören ob ihr es als richtig anseht was ich gemacht habe und das Uni-tool spinnt oder ob der Fehler bei mir liegt,
hier mal der Code:
Code: Alles auswählen
# add more modules if necessary
import numpy
## Function reads the x-y-Data from a file.
# @param filename : name of the file including path and file type
# @return xData, yData : (n,1) matrix of x values and (n,1) matrix of y values
def readXYData(filename):
infile = open(filename,'r')
x = []
y = []
for line in infile:
s = line.split()
x.append(float(s[0]))
try:
y.append(float(s[1]))
except:
y.append(0.0)
continue
infile.close()
nData = len(x)
xData = numpy.zeros((nData,1))
yData = numpy.zeros((nData,1))
for i in range(0,nData,1):
xData[i,0] = x[i]
yData[i,0] = y[i]
return xData, yData
## Functions computes the coefficients for the least-squares method applied to
# abitrary functions.
# @param xData : x-coordinates of the data points, (n,1) matrix
# @param yData : y-values of the data points, (n,1) matrix
# @param functions : list of given functions, function name as string, (n)-list
# functions = ['x**0','x**3','math.sin(x)']
# @return a : coefficients of the lsq-function, (n,1) matrix or error str.
def lsCoeffs(xData,yData,functions):
nx,mx=xData.shape
ny,my=yData.shape
if nx!=ny :
return 'ERROR INPUT_DIMENSIONS'
nf=functions.shape[0]
S=numpy.zeros((nf,nf),numpy.float)
c=numpy.zeros((nf,1),numpy.float)
for j in range(nf):
for k in range(nf):
for i in range(nx):
x=xData[i,0]
phi_j=functions[j] #functions = ['x**0','x**3','math.sin(x)']
phi_k=functions[k]
S[j,k] = S[j,k]+eval(phi_j)*eval(phi_k)
for j in range(nf):
for i in range(nx):
x=xData[i,0]
y=yData[i,0]
phi_j=functions[j]
c[j,0]=c[j,0]+eval(phi_j)*y
a=numpy.linalg.solve(S,c)
return a
## Function evaluates the lsq-function for a given x-value
# @param coeffs : coefficients of the lsq-function, (n,1) matrix
# @param functions : list of given functios, function name as string, (n)-list
# @param x : value at which the lsq-function is evaluated, scalar
# @return y : result of evaluation, scalar or error string
def evalLS(coeffs,functions,x): #diesen Teil könnte ich noch nicht testen
nf=functions.shape[0]
nc,mc=coeffs.shape
if nc!=nf:
return 'ERROR INPUT_DIMENSIONS'
y=0
for j in range(nf):
phi_j=functions[j]
y+=coeffs[j,0]*eval(phi_j)
return y
Mfg dugbug