function varargout = approximation_punktmenge_3d_2(varargin)
% APPROXIMATION_PUNKTMENGE_3D_2 MATLAB code for approximation_punktmenge_3d_2.fig
%      APPROXIMATION_PUNKTMENGE_3D_2, by itself, creates a new APPROXIMATION_PUNKTMENGE_3D_2 or raises the existing
%      singleton*.
%
%      H = APPROXIMATION_PUNKTMENGE_3D_2 returns the handle to a new APPROXIMATION_PUNKTMENGE_3D_2 or the handle to
%      the existing singleton*.
%
%      APPROXIMATION_PUNKTMENGE_3D_2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in APPROXIMATION_PUNKTMENGE_3D_2.M with the given input arguments.
%
%      APPROXIMATION_PUNKTMENGE_3D_2('Property','Value',...) creates a new APPROXIMATION_PUNKTMENGE_3D_2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before approximation_punktmenge_3d_2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to approximation_punktmenge_3d_2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help approximation_punktmenge_3d_2

% Last Modified by GUIDE v2.5 03-Sep-2013 23:21:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @approximation_punktmenge_3d_2_OpeningFcn, ...
                   'gui_OutputFcn',  @approximation_punktmenge_3d_2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before approximation_punktmenge_3d_2 is made visible.
function approximation_punktmenge_3d_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to approximation_punktmenge_3d_2 (see VARARGIN)

% Choose default command line output for approximation_punktmenge_3d_2
handles.output = hObject;

clc;

% Format für die Polynom-Ausgabe
handles.format = '%10.12f';
% handles.x = '';
% handles.y = '';
% handles.z = '';

% Sliders-Grenzen setzen
max = str2double(get(handles.anz_u, 'String'));
set(handles.slidern,'Value',str2double(get(handles.n,'String')));
set(handles.slidern,'Max', max);
set(handles.slidern,'SliderStep',[1/(max-4) 1/(max-4)]);



max = str2double(get(handles.anz_v, 'String'));
set(handles.sliderm,'Value',str2double(get(handles.m,'String')));
set(handles.sliderm,'Max',max);
set(handles.sliderm,'SliderStep',[1/(max-4) 1/(max-4)]);


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes approximation_punktmenge_3d_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = approximation_punktmenge_3d_2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes on button press in pfad.
function pfad_Callback(hObject, eventdata, handles)
% hObject    handle to pfad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in anzeigen.
function anzeigen_Callback(hObject, eventdata, handles)
% hObject    handle to anzeigen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


function fktvorschrift_Callback(hObject, eventdata, handles)
% hObject    handle to fktvorschrift (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of fktvorschrift as text
%        str2double(get(hObject,'String')) returns contents of fktvorschrift as a double
generieren_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function fktvorschrift_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fktvorschrift (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ulinks_Callback(hObject, eventdata, handles)
% hObject    handle to ulinks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ulinks as text
%        str2double(get(hObject,'String')) returns contents of ulinks as a double
generieren_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function ulinks_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ulinks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function urechts_Callback(hObject, eventdata, handles)
% hObject    handle to urechts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of urechts as text
%        str2double(get(hObject,'String')) returns contents of urechts as a double
generieren_Callback(hObject, eventdata, handles)



% --- Executes during object creation, after setting all properties.
function urechts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to urechts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function anz_u_Callback(hObject, eventdata, handles)
% hObject    handle to anz_u (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of anz_u as text
%        str2double(get(hObject,'String')) returns contents of anz_u as a double
generieren_Callback(hObject, eventdata, handles)



% --- Executes during object creation, after setting all properties.
function anz_u_CreateFcn(hObject, eventdata, handles)
% hObject    handle to anz_u (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function vlinks_Callback(hObject, eventdata, handles)
% hObject    handle to vlinks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of vlinks as text
%        str2double(get(hObject,'String')) returns contents of vlinks as a double
generieren_Callback(hObject, eventdata, handles)



% --- Executes during object creation, after setting all properties.
function vlinks_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vlinks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function vrechts_Callback(hObject, eventdata, handles)
% hObject    handle to vrechts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of vrechts as text
%        str2double(get(hObject,'String')) returns contents of vrechts as a double
generieren_Callback(hObject, eventdata, handles)



% --- Executes during object creation, after setting all properties.
function vrechts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vrechts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function anz_v_Callback(hObject, eventdata, handles)
% hObject    handle to anz_v (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of anz_v as text
%        str2double(get(hObject,'String')) returns contents of anz_v as a double
generieren_Callback(hObject, eventdata, handles)



% --- Executes during object creation, after setting all properties.
function anz_v_CreateFcn(hObject, eventdata, handles)
% hObject    handle to anz_v (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in generieren.
function generieren_Callback(hObject, eventdata, handles)
% hObject    handle to generieren (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Daten aus der Oberfläche holen
fkt = get(handles.fktvorschrift, 'String');
f = str2func(['@(x,y) ',fkt]); 

str1 = get(handles.ulinks, 'String');
handles.u_links = str2double(str1);

str2 = get(handles.urechts, 'String');
handles.u_rechts = str2double(str2);

str3 = get(handles.anz_u, 'String');
handles.anzu = str2double(str3);


str4 = get(handles.vlinks, 'String');
handles.v_links = str2double(str4);

str5 = get(handles.vrechts, 'String');
handles.v_rechts = str2double(str5);

str6 = get(handles.anz_v, 'String');
handles.anzv = str2double(str6);

% Daten erzeugen
xx = linspace(handles.u_links, handles.u_rechts, handles.anzu);
yy = linspace(handles.v_links, handles.v_rechts, handles.anzv);

[handles.x handles.y] = meshgrid(xx,yy);
handles.z             = f(handles.x, handles.y);

% Zu axis1 wechseln und zeichnen
axes(handles.axes1);
surf(handles.x, handles.y, handles.z);

% Werte für Slider setzen
max = str2double(get(handles.anz_u, 'String'));
set(handles.slidern,'Max', max);
set(handles.slidern,'SliderStep',[1/(max-4) 1/(max-4)]);

max = str2double(get(handles.anz_v, 'String'));
set(handles.sliderm,'Max',max);
set(handles.sliderm,'SliderStep',[1/(max-4) 1/(max-4)]);


guidata(hObject, handles); 



% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1
approximieren_Callback(hObject, eventdata, handles);
guidata(hObject, handles); 



function n_Callback(hObject, eventdata, handles)
% hObject    handle to n (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of n as text
%        str2double(get(hObject,'String')) returns contents of n as a double
n = str2double(get(handles.n,'String'));
set(handles.slidern,'Value',n);
approximieren_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function n_CreateFcn(hObject, eventdata, handles)
% hObject    handle to n (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function m_Callback(hObject, eventdata, handles)
% hObject    handle to m (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of m as text
%        str2double(get(hObject,'String')) returns contents of m as a double

m = str2double(get(handles.m,'String'));
set(handles.sliderm,'Value',m);
approximieren_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on slider movement.
function slidern_Callback(hObject, eventdata, handles)
% hObject    handle to slidern (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


addlistener(handles.slidern,'ContinuousValueChange',@move)


function move(hObject, eventdata)


handles = guidata(hObject);
value = round(get(handles.slidern,'Value'));
set(handles.n,'String',num2str(value));

approximieren_Callback(hObject, eventdata, handles);

guidata(hObject, handles);
    
% guidata(hObject, handles); 

% function move(hObject, eventdata, handles)
% 
% value = round(get(handles.slidern,'Value'));
% set(handles.n,'String',num2str(value));
% approximieren_Callback(hObject, eventdata, handles);
% 
% guidata(hObject, handles); 




% --- Executes during object creation, after setting all properties.
function slidern_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slidern (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% set(handles.slidern,'Value',str2double(get(handles.n,'String')));
% set(handles.slidern,'Max',handles.anzu);


% --- Executes on slider movement.
function sliderm_Callback(hObject, eventdata, handles)
% hObject    handle to sliderm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

value = round(get(handles.sliderm,'Value'));
set(handles.m,'String',num2str(value));
approximieren_Callback(hObject, eventdata, handles);

guidata(hObject, handles); 


% --- Executes during object creation, after setting all properties.
function sliderm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sliderm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end




% --- Executes on button press in approximieren.
function approximieren_Callback(hObject, eventdata, handles)
% hObject    handle to approximieren (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Abbruch wenn keine Daten existieren

if(strcmp(handles.x,'') || strcmp(handles.y,'') || strcmp(handles.z,''))
    errordlg('Datenpunkte existieren nicht. Bitte überprüfen!','Daten unvollständig');
    return;
end

anfang_u  = handles.u_links;
ende_u  = handles.u_rechts;

anfang_v  = handles.v_links;
ende_v  = handles.v_rechts;

n = str2double(get(handles.n,'String'));
m = str2double(get(handles.m,'String'));

if (m<=3 || n<=3)
    errordlg('Anzahl der Kontrollpunkte bei B-Splines 3.Ordnung muss mindestens 4 sein!','Zu wenig Konrollpunkte');
    return;
end

if (n > handles.anzu || m > handles.anzv)
    errordlg('Anzahl der Kontrollpunkte größer als Anzahl der Datenpunkte ist nicht sinnvoll','Eingabe nicht sinnvoll');
    return;
end


abstand_u = (ende_u - anfang_u)/(n-3);
abstand_v = (ende_v - anfang_v)/(m-3);

anz_knoten_u = n + 4;                                                       % Anzahl der Knotenpunkte
anz_knoten_v = m + 4;                                                       % Anzahl der Knotenpunkte

% Knotenvektor U
U(1:4) = anfang_u;
U(anz_knoten_u - (1:4) + 1) = ende_u;

i = 5:anz_knoten_u-4;
U(i) = anfang_u + (i - 4).*abstand_u;

% Knotenvektor V
V(1:4) = anfang_v;
V(anz_knoten_v - (1:4) + 1) = ende_v;

i = 5:anz_knoten_v-4;
V(i) = anfang_v + (i-4).*abstand_v;

% Approximation
[handles.cx handles.cy handles.cz max_abw mean_abw] = approx_bsplines_3d(n,m,U,V,handles.x,handles.y,handles.z);


% Approx. Bsplines-Fläche zeichnen
uu = linspace(anfang_u,ende_u,100);
vv = linspace(anfang_v,ende_v,100);

[handles.x2,handles.y2] = meshgrid(uu,vv);

% Matrix A
A = zeros(length(uu),n);
for i=1:length(uu)
    index = indexsuche(uu(i),U,n);
    A(i,:) = bsplinebasis(uu(i),index,U);
end

% Matrix B
B = zeros(length(vv),m);
for i=1:length(vv)
    index = indexsuche(vv(i),V,m);
    B(i,:) = bsplinebasis(vv(i),index,V);
end

handles.fl_wert = (A*handles.cz*B')';

% axes(handles.axes2);
% surf(handles.x2,handles.y2,handles.fl_wert);
surf(handles.x2,handles.y2,handles.fl_wert, 'parent', handles.axes2); 
hold(handles.axes2,'on');

% Kontrollpunktnetz zeichnen wenn checkbox aktiv
if (get(handles.checkbox1,'Value')==1)
    plot3(handles.cx, handles.cy, handles.cz, '-k.','MarkerSize',12,'LineWidth',1, 'parent', handles.axes2);
    plot3(handles.cx', handles.cy', handles.cz', '-k.','MarkerSize',12,'LineWidth',1, 'parent', handles.axes2);
end
axis(handles.axes2,[handles.u_links, handles.u_rechts, handles.v_links, handles.v_rechts]);
hold (handles.axes2,'off');

% Fehlerberechnung und Anzeige

set(handles.maxabw,'String', num2str(max_abw,handles.format));
set(handles.maxabw,'Visible','on');

set(handles.meanabw,'String', num2str(mean_abw,handles.format));
set(handles.meanabw,'Visible','on');

guidata(hObject, handles); 




% --- Executes on button press in zurueck.
function zurueck_Callback(hObject, eventdata, handles)
% hObject    handle to zurueck (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all
hauptmenu

% --- Executes on button press in beenden.
function beenden_Callback(hObject, eventdata, handles)
% hObject    handle to beenden (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all




%==========================================================================
%                         Selbstgeschriebene Funktionen
%==========================================================================


function [ cx cy cz max_abw mean_abw] = approx_bsplines_3d(n, m, U, V, x, y, z)
% n     = Anzahl der Kontrollpunkte in u       
% m     = Anzahl der Kontrollpunkte in v  
% U     = Knotenvektor in u
% V     = Knotenvektor in v
% x,y,z = Koordinaten-Matritzen der zu approx. 3D-Punkte (meshgrid)


% Matrix A
p = size(x,2);
A = zeros(p,n);
for i=1:p
    index = indexsuche(x(1,i),U,n);
    A(i,:) = bsplinebasis(x(1,i),index,U);
end

% Matrix B
q = size(x,1);
B = zeros(q,m);
for i=1:q
    index = indexsuche(y(i,1),V,m);
    B(i,:) = bsplinebasis(y(i,1),index,V);
end

% Kontrollpunkte in y wird approximiert
E2 = A\(y');
cy = E2/(B');

% Kontrollpunkte in x wird approximiert
E3 = A\(x');
cx = E3/(B');

% Kontrollpunkte in z wird approximiert
E = A\(z');
cz = E/(B');

fehler = abs(A*cz*B' - z');
max_abw = max(max(fehler));
mean_abw = mean(mean(fehler));




%-------------------------------------------------------------------------
% Diese Funktion berechnet den Funktionswert einer B-Spline-Basisfkt
function endwert=bsplinebasis(u, index, knoten)
% Funktioniert nur für Grad = 3
% u:			x-wert
% knoten:		Knotenfolge
% index:        index, in dem u liegt.
    
% Berechne N(1,i)
N(1,index) = 1;
for i=1:index-1
    N(1,i) = 0;
end

for i=index+1:length(knoten)-1
    N(1,i) = 0;
end

% Berechne N(2,i) bis N(4,i)
for k=2:4
    for i=1:length(knoten)-k
        if (knoten(i+k-1) - knoten(i)==0)
            term1 = 0;
        else
            term1 = (u - knoten(i))/(knoten(i+k-1) - knoten(i)) * N(k-1,i);
        end

        if (knoten(i+k) - knoten(i+1)==0)
            term2 = 0;
        else
            term2 = (knoten(i+k)-u)/(knoten(i+k) - knoten(i+1)) * N(k-1,i+1);
        end
        N(k,i) = term1 + term2;
    end
end
endwert=N(k,1:length(knoten)-4);


%-------------------------------------------------------------------------
% Diese Fkt berechnet die Funktionswerte einer B-Spline-Kurve
function endwert = deboor(index, grad, u, kontroll, knoten)
% Wenn u außerhalb Knotenfolge, dann Funktionswert=0
d = zeros(grad+1,index);

if (u < knoten(1) || u > knoten(length(knoten)))	
    endwert = 0;
    return;
end


for i=index-grad : index
    d(1,i) = kontroll(i);
end

for k=2:grad+1
    for i=index-grad+k-1 : index

        alpha = (u - knoten(i))/(knoten(i+grad+2-k) - knoten(i));
        d(k,i) = (1-alpha)*d(k-1,i-1) + alpha*d(k-1,i);
    end
end    
endwert = d(k,i);
    

%-------------------------------------------------------------------------
function index=indexsuche(u, knoten, m)
% u:			x-wert
% knoten:		Knotenfolge
% m:			anzahl der Kontrollpunkte

index=4;
for i=4:m
    if u >= knoten(i)
        index=i;
    else
        break;
    end
end
return
                
