habe mit der Programmierung einer Lagrange Multipli. Schwierigkeiten. Das Problem ist die Definition der "Variablen" a wie ich vermute, die Python nicht erkennen will. Es soll eine Funktion auf Grundlage von zwei Input Vektoren ausgegeben werden. Eigentlich relativ einfach...
Kann mir bitte jemand weiterhelfen? Würde eigentlich ungern viel am Code ändern müssen...
Code: Alles auswählen
# Lagrange Interpolation NA
import matplotlib.pyplot as plt
import numpy as np
#import sympy as sp
from sympy import *
#from numpy import arange
x = (-2.5, -1., 0.5, 2., 3.5)
y = (0.,3., 3.5, 0., -2.5)
L = np.zeros(len(x)-1)
p = 0.0
a = Symbol('a')
print a
print L
for i in range (0, len(x)-1):
L[i] = 1
print L
for j in range (0, len(x)):
if i != j:
L[i] = L[i]*((a-x[j])/(x[i]-x[j]))
print L
p = p + y[i]*L[i]
print p
Code: Alles auswählen
%run "C:\Users\FabSel\..."
a
[ 0. 0. 0. 0.]
[ 1. 0. 0. 0.]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\FabSel... in <module>()
21 for j in range (0, len(x)):
22 if i != j:
---> 23 L[i] = L[i]*((a-x[j])/(x[i]-x[j]))
24 print L
25 p = p + y[i]*L[i]
C:\Users\FabSel\AppData\Local\Enthought\Canopy\User\lib\site-packages\sympy\core\expr.pyc in __float__(self)
223 if result.is_number and result.as_real_imag()[1]:
224 raise TypeError("can't convert complex to float")
--> 225 raise TypeError("can't convert expression to float")
226
227 def __complex__(self):
TypeError: can't convert expression to float