Fitting data to a surface in Octave

Need to fit some data to a surface (function of two independent variables) in Octave?

Matlab has a function to do it directly but Octave needs some help.

The following example demonstrates one possible method using the optimisation package.

# Perform surface fitting to data in octave using optimisation package
# In principle I can see no reason why this technique will not work for a 
# higher number of dimensions of observed data.

# Example runs in Octave 5.2.0

# J E Green, Bear Instruments LTD © 2020.
# www.bearinstruments.co.uk
# Power Electronics, Embedded Systems & Instrumentation
  
clear

# required optimisation package
pkg load optim

# for demonstration, use the communications package to add noise to the 
# contrived data
pkg load communications
 
# independents and observations
# Controlled variables (things you have adjusted to get your data)
x = [-5 -4 -3 -2 -1 0 1 2 3 4 5];
y = [-3 -2 -1 0 1 2 3]';

# dependent variable (i.e. your observed values, I made up in this case)
obs = [
   42   31   22   15   10    7    6    7   10   15   22
   33   23   15    9    5    3    3    5    9   15   23
   26   17   10    5    2    1    2    5   10   17   26
   21   13    7    3    1    1    3    7   13   21   31
   18   11    6    3    2    3    6   11   18   27   38
   17   11    7    5    5    7   11   17   25   35   47
   18   13   10    9   10   13   18   25   34   45   58];

# add noise to the "observations" to give a signal to noise ratio of 0 dB.
obs = awgn(obs,0); 

# Function you hope that the data will fit.
# p is the weights (coefficients of the function). In this case it is a 
# quadratic in two variables. To generate the obs[] above I have used 
# p(n) = 1 for n = 1 to 6.
# In other words all the weights are unity in the example.   
function retval = myfunc (p,x,y)
  retval = p(1).*x.^2 + p(2).*x.*y + p(3).*y.^2 + p(4).*x + p(5).*y + p(6);
endfunction
  
# initial values, just to throw it off I've made them negative and ten times too
# small.
init = [-0.1; -0.1; -0.1; -0.1; -0.1; -0.1;];
  
# Set some parameters of the optimisation problem  
settings = optimset ("MaxIter", 100, "TolFun", 1e-6);
  
# start optimization
[p, residuals, cvg, outp] = nonlin_residmin( @(p) (myfunc(p,x,y) - obs), init, settings)

# Generate the 'ideal' data for plotting. This will be the same as obs[] 
# because this problem is contrived to demonstrate the method.
for i=1:size(x)(2)
  for j = 1:size(y)(1)
    model_values(j,i) = myfunc(p,x(i),y(j));
  endfor
endfor
 
# plot the fitted data
figure
surf(x,y,model_values)
title ("Fitted Values");
  
# plot the "experimental" data
figure
mesh(x,y,obs)
title ("Experimental Values");
 
# Dump the matrices to the command window
model_values
obs

# plot the residuals.
figure
surf(residuals)
title ("Residuals");

# total sum of squares
SST = sum(sum((obs - mean(mean(obs))).^2));

# sum of squared residuals
SSR = sum(sum(residuals.^2));

# R^2
Rsq = (1 - (SSR./SST))

# output the vector of coefficients (they should all be close to 1).
p