function NonLinReg() close all global P N I=[50; 150; 250; 350]; f=[-1845; -456; 1277; 2047]; DIM = 4; B = 20; P = zeros(DIM,2); P(:,1) = I'; %P(:,2) = 2^B*sin(P(:,1)*pi/2/DIM); P(:,2) = f; N = 3; c0 = ones(1,N+1); %c0 = [.01 .01 .001 .001 .1 .1]; [c, res] = lsqnonlin(@Residuum,c0); fprintf('res=%.6e\n',res); fprintf('c=%.6e\n',c); %h = (max(P(:,1))-min(P(:,1)))/(DIM-1); %xx=min(P(:,1)):h:max(P(:,1)); yy=Ansatzfunktion(P(:,1),c); plot(P(:,1),yy,'k-',P(:,1),P(:,2),'ro'); figure(2) a=10; L=length(yy)-a; %plot(P(a:L,1),abs(yy(a:L)-P(a:L,2))./P(a:L,2),'ro-'); plot(P(a:L,1),(yy(a:L)-P(a:L,2))/2^B,'ro-'); grid on end function y=Residuum(c) global P y = P(:,2) - Ansatzfunktion(P(:,1),c); %y = sqrt((c(1)-P(:,1)).^2+(c(2)-P(:,2)).^2)-c(3); end function y=Ansatzfunktion(x,c) % y=c(1)*x.^5+c(2)*x.^4+c(3)*x.^3+c(4)*x.^2 +c(5).*x +c(6); % y = c(1)*exp(c(2)*x); %y = c(1)*x+c(2); %y = c(1)*x.^2+c(2)*x; % y = c(3)*x.^2+c(2)*x+c(1); % y = c(1)*exp(-c(2)*x)+c(3); % y = c(1)*exp(c(2)*x)+c(3)*x.*exp(c(4)*x); % y = c(1)*exp(-c(2)*x).*sin(c(3)*x+c(4)); global N y=c(1); for i=1:N y = y + c(i+1)*x.^i; end end