How do i do a two dimensional linear regression fit

26 views (last 30 days)
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.
Can anybody help me with this? The x en y coordinates are both 4392x2.
Thanks in advance!

Answers (3)

the cyclist
the cyclist on 24 Jul 2012
Do you have the Statistics Toolbox? If so, I believe you can use the mvregress() function to do this.
  2 Comments
jdm_dm
jdm_dm on 24 Jul 2012
Hey,
Thanks for the quick response! But it doesn't seems to work. I've put the x and y coordinates in separate arrays respectively X and Y (both size 4392x2). When i do p=mvregress(Y,X)I get an error message: X must be a cell array if Y has multiple columns.
When I try to put X in a cell array (I call him mycell) and then try p=mvregress(Y,mycell) I get the following error: Undefined function 'isnan' for input arguments of type 'cell'.
Thanks,
Jdm
the cyclist
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.

Sign in to comment.


Image Analyst
Image Analyst on 24 Jul 2012

Bahloul Derradji
Bahloul Derradji on 2 Jul 2020
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!