matlabfunction-approximation

How do you find an equation of a plane that best approximates 3 dimensional data in MATLAB?


I have the following 3 dimensional data in MATLAB:

tau = [6e-9 30e-12 6e-9 30e-12];
E=[1e-3 50e-6 .01 1e-3];
k=[6.93774E-08 1.23666E-08 4.45261E-08 1.90789E-08];
plot3(tau, E, k,'*'); xlabel('tau (s)'); ylabel('Energy (J)'); zlabel('k'); 

You can see the plot looks like this:

enter image description here

How do you find the equation of the plane that approximates this data (i.e. k is a function of tau and E, so I'm looking for a formula for k(tau,E) = that best fits the data in least squares sense.).

Is there an easy way to do this in MATLAB?


Solution

  • You can do a simple least squares solution:

    tau = [6e-9 30e-12 6e-9 30e-12];
    E = [1e-3 50e-6 .01 1e-3];
    k = [6.93774E-08 1.23666E-08 4.45261E-08 1.90789E-08];
    
    A = [tau; E; ones(size(E))]';
    b = k';
    
    beta = A\b;
    
    [X, Y] = meshgrid(linspace(min(tau), max(tau), 20),...
        linspace(min(E), max(E), 20));
    
    Z = beta(1)*X + beta(2)*Y + beta(3);
    
    plot3(tau, E, k,'o', 'markerfacecolor', 'b'); 
    xlabel('tau (s)'); ylabel('Energy (J)'); zlabel('k'); hold on;
    mesh(X, Y, Z, 'edgecolor', 'k');
    
    

    enter image description here