# _______________________________________________________
# Kommenttirivi: Octave ohjelmani Opns1.m
# - Pienimman neliosumman sovitus
# _______________________________________________________
clear ; clc    # Poistetaan ... Tyhjennetaan ...
# _______________________________________________________
function y=ffun(x,beta) ; #
y=beta(1)+beta(2)*x     ; # Ilman ";" tulostaa!
endfunction             ; #
# _______________________________________________________
pkg load optim          ; # Added spring 2017
# _______________________________________________________
filename='OPpns1.dat'; fid=fopen(filename,'r'); # Data
[z]=fscanf(fid,'%g%g',[2,Inf]);                 # -"-
x=z(1,:);            y=z(2,:) ;                 # -"-
beta0=[1 1]; printf("beta0=%6.2f%6.2f\n",beta0) # Alkuarvo
[yfit beta1] = leasqr(x,y,beta0,"ffun");        # PNS
printf("beta1=%6.2f%6.2f\n",beta1)              # Loppuarvo
axis([0,4,0,9]);                                # xy rajat
hold on; plot(x,y,'or'); xx=0.5+((0:21)/20.)*3; # Mallin xx
g =beta1(1)+beta1(2)*xx; plot(xx,g);            # Malli g(xx)
hold off; print('Opns1.jpg','-djpg')            # Kuva
