필터 지우기
필터 지우기

Warning: Error updating FunctionLine

조회 수: 6 (최근 30일)
Alicia Durian
Alicia Durian 2020년 4월 18일
댓글: Massimo Ciacci 2020년 4월 18일
I want to create newtons interpolating polynomial with a matlab function and plot this at the end.
Following you can see the code. The polynomial I want to plot is saved in the variable p. (fplot)
If I want to plot it, I get the Warning: Error updating FunctionLine.
What am I doing wrong?
function [c] = Interpolieren(x,y,flag)
% Berechnung des Interpolationspolynoms
% P_n(x) = c0 + c1(x-x0)+c2(x-x0)(x-x1)+...+cn(x-x0)...(x-xn-1)
% x : x-Werte der Messreihe
% y : y-Werte der Messreihe
% P(x/y) sind die Stützstellen der Funktion
% flag = true Plot der Ergebnisse wird erzeugt
if nargin == 2 % Test ob 3. Übergabewert (für Plot) vorhanden ist
flag = false;
end
% Definieren von Variablen
l_2 = length(x); % Länge des x-Vektors
M = zeros(length(x),length(x)); % Definieren einer Matrix zum speichern der Koeffizienten
M(:,1) = y; % Der Matrix in der ersten Spalte die y-Werte zuweisen
c = zeros(length(x)); % Definieren des Vektors für die Koeffizienten
% Berechnung der Koeffizienten
for i_2 = 1:length(x)
l_2 = l_2-1;
for i_3 = 1:l_2
M(i_3,i_2+1) = (M(i_3+1,i_2)-M(i_3,i_2))/(x(i_3+i_2)-x(i_3));
end
end
c = M(1,:); % Koeffizienten im Vektor c speichern
% Definieren von Variablen
k=1; % Variable zur Speicherung der Klammern
d = 1; % Variable zur Speicherung der vorläufigen Funktion
c_i = ones(length(c)); % Vektor der Koeffizienten und Klammern des Polynoms enthält
for i_5 = 1:l_2
k = @(o) k * (o - x(i_5-1)); % Erstellen der Klammern der Funktion
c_i(i_5+1)= @(o) c(i_5+1) * k; % Erstellen der Klammern mit den Koeffizienten
end
for i_6 = 1:length(x) % Erstellen der Funktion ohne den ersten Koeffizenten
d = @(o) d + c_i(i_6);
end
p = @(o) c(1) + d % Erstellen der entgültigen Funktion
% Plotten der Stützstellen(P(x/y)) und des Polynoms
hold on
if flag == true
plot(x,y,'r*')
fplot(p,'b')
xlim([-5 5])
ylim([-5 5])
end
hold off
end
Thanks Alicia

답변 (3개)

Massimo Ciacci
Massimo Ciacci 2020년 4월 18일
Hello Alicia,
@ line 18 you change the for loop variable i_2 inside the loop, this is bad practice.
@ line 29 i_2 has the value 0, loop does not execute
You definition of function pointers (line 30,31,36) is rather cryptic (@(o) etc), I suppose this is the culprit of all the problems. I never saw this usage of function pointers. Should you use instead a default function definition ?
  댓글 수: 3
Massimo Ciacci
Massimo Ciacci 2020년 4월 18일
Now you get that error because you are adding a function handle @(o) with a number c_i(i_6) which does not make sense. Just get rid of the @(o) altogether? If you are accumulating values you do not need @ stuff.
Alicia Durian
Alicia Durian 2020년 4월 18일
But then I have an undefined variable 'o' in line 30. And I need this variable in the function at the end.

댓글을 달려면 로그인하십시오.


Massimo Ciacci
Massimo Ciacci 2020년 4월 18일
function [c] = Interpolieren(x,y,flag)
% Berechnung des Interpolationspolynoms
% P_n(x) = c0 + c1 x + c2 x^2 + ... + c(n-1) x^(n-1)
% x : x-Werte der Messreihe
% y : y-Werte der Messreihe
% P(x/y) sind die Stützstellen der Funktion
% flag = true Plot der Ergebnisse wird erzeugt
if nargin == 2 % Test ob 3. Übergabewert (für Plot) vorhanden ist
flag = false;
end
% Definieren von Variablen
M = vanDerMondeMatrix(x);
if size(y,1)<size(y,2)
y = y.'; %make it a column vector
end
c = linsolve(M,y);
L = length(x);
clear('p');
p{1} = @(z) c(1);
for ii=2:L
p{ii} = @(z) (p{ii-1}(z) + c(ii).*z.^(ii-1));
%p{ii}(x(1))
end
% pFinal = @(z) arrayfun(@(k) p{1}(z), 1:L);
pFinal = p{L};
% Plotten der Stützstellen(P(x/y)) und des Polynoms
if flag == true
figure; hold on
plot(x,y,'r*')
xlim([x(1) x(end)])
fplot(pFinal,'b')
end
end
function M = vanDerMondeMatrix(x)
L = length(x);
if size(x,1)<size(x,2)
x = x.'; %make it a column vector
end
M = zeros(L,L);
for ii=1:L
M(:,ii) = x.^(ii-1);
end
end
  댓글 수: 1
Massimo Ciacci
Massimo Ciacci 2020년 4월 18일
You can copy paste the code above into an m file called Interpolieren.m

댓글을 달려면 로그인하십시오.


Tamir Gazit
Tamir Gazit 2020년 4월 18일
try to set an intervel
fplot(@(x) exp(x),[-3 0],'b')
in your case fplot(p,[0 1])

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by