How do i do a two dimensional linear regression fit
Show older comments
Hey,
I'm working with winddata and for a model i need to do a two-dimensional linear regression fit of the form
[y1;y2]=[a1;a2]+[b1,b2;b3,b4]*[x1;x2]
(x1,X2) and (y1,y2) are know and i want to determine to a and b coefficients.
Thanks in advance!
Answers (3)
the cyclist
on 24 Jul 2012
0 votes
Do you have the Statistics Toolbox? If so, I believe you can use the mvregress() function to do this.
2 Comments
jdm_dm
on 24 Jul 2012
the cyclist
on 24 Jul 2012
Edited: the cyclist
on 24 Jul 2012
I suggest you look at the "flu" example here:
to help you debug your syntax.
Image Analyst
on 24 Jul 2012
0 votes
I use John D'Errico's polyfitn: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A679
Bahloul Derradji
on 2 Jul 2020
0 votes
use the following ready to use example code:
this is the mainfile.m
clear all
clc
clf
close all
xdata =( -1:0.1:+1);
ydata=(-1:0.2:1)';
nx=numel(xdata);
ny=numel(ydata);
ax=0.8;
ay=0.4;
A=5;
[X,Y]= meshgrid(xdata,ydata);
Z=A*exp(-((X/ax).^2+(Y/ay).^2))+ 0.05*rand(ny,nx);
surf(X,Y,Z);
s = surf(X,Y,Z,'FaceAlpha',0.4);
s.EdgeColor = 'none';
s.FaceColor = 'red';
xlabel('x')
ylabel('y')
zlabel('z')
x = reshape(X,[],1);
y = reshape(Y,[],1);
z = reshape(Z,[],1);
%cftool
[fitresult, gof] = createFit(x, y, z);
disp(fitresult)
Here is the createFit.m script;
function [fitresult, gof] = createFit(x, y, z)
%% Fit: 'Gaussian fit 1'.
[xData, yData, zData] = prepareSurfaceData( x, y, z );
% Set up fittype and options.
ft = fittype( 'a*exp(-(x/wx)^2-(y/wy)^2)', 'independent', {'x', 'y'}, 'dependent', 'z' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.890036233228213 0.330202242514021 0.22970119787112];
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Gaussian fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'Gaussian fit 1', 'z vs. x, y', 'Location', 'NorthEast' );
% Label axes
xlabel x
ylabel y
zlabel z
grid on
% enjoy.
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!